0

I have two entities, TestCase and TestCaseStep, defined as follows:

TestCase

@Entity
@Table(name = "TEST_CASE")
public class TestCase implements Serializable, TestCase
{  
    @Id
    @Column(name = "name")
    private String name;

    @OneToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "Testcase_Step_Association", joinColumns = { @JoinColumn(name = "TC_NAME", referencedColumnName = "NAME") }, inverseJoinColumns = {
        @JoinColumn(name = "STEP_NAME", referencedColumnName = "NAME") })
    @OrderColumn(name = "STEP_NUMBER", nullable = false)
    private List<TestCaseStep> testCaseSteps;

    [...]
}

TestCaseStep

@Entity
@Table(name = "TEST_CASE_STEPS")
public class TestCaseStep implements Serializable, TestCaseStep
{
    @Id
    @Column(name = "name")
    private String name;

    [...]
}

This works fine as long as I do not try to insert the same object into the list of test case steps more than once. As soon as I try that, I get a primary key violation:

Caused by: org.h2.jdbc.JdbcSQLException: Eindeutiger Index oder Prim�rschl�ssel verletzt: "PRIMARY_KEY_9 ON PUBLIC.TESTCASE_STEP_ASSOCIATION(TC_NAME, TESTCASESTEPS_NAME) VALUES ('TESTCASE_1', 'OUT_STEP', 395)"
Unique index or primary key violation: "PRIMARY_KEY_9 ON PUBLIC.TESTCASE_STEP_ASSOCIATION(TC_NAME, TESTCASESTEPS_NAME) VALUES ('TESTCASE_1', 'OUT_STEP', 395)"; SQL statement:
INSERT INTO Testcase_Step_Association (testCaseSteps_NAME, TC_NAME, STEP_NUMBER) VALUES (?, ?, ?) [23505-191]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
at org.h2.message.DbException.get(DbException.java:179)
at org.h2.message.DbException.get(DbException.java:155)
at org.h2.index.BaseIndex.getDuplicateKeyException(BaseIndex.java:107)
at org.h2.mvstore.db.MVSecondaryIndex.checkUnique(MVSecondaryIndex.java:230)
at org.h2.mvstore.db.MVSecondaryIndex.add(MVSecondaryIndex.java:189)
at org.h2.mvstore.db.MVTable.addRow(MVTable.java:704)
at org.h2.command.dml.Insert.insertRows(Insert.java:156)
at org.h2.command.dml.Insert.update(Insert.java:114)
at org.h2.command.CommandContainer.update(CommandContainer.java:98)
at org.h2.command.Command.executeUpdate(Command.java:258)
at org.h2.jdbc.JdbcPreparedStatement.executeUpdateInternal(JdbcPreparedStatement.java:160)
at org.h2.jdbc.JdbcPreparedStatement.executeUpdate(JdbcPreparedStatement.java:146)
at com.sun.gjc.spi.base.PreparedStatementWrapper.executeUpdate(PreparedStatementWrapper.java:125)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeDirectNoSelect(DatabaseAccessor.java:892)
... 193 more

I need to allow my users to repeat the same test case step multiple times. How can I accomplish this without using a separate entity to map the relationship by myself?

Kubus
  • 23
  • 6
  • Change the DDL script so that the order column is part of the pk for the join table, though EclipseLink doesn't support duplicates as per http://stackoverflow.com/questions/15690347/persist-a-list-with-order-and-duplicates-in-jpa-eclipselink – Chris May 12 '16 at 16:47
  • Well, that is embarrassing. I did quite some research before posting, but never stumbled upon this post. Thank you! I will up vote the bug at once. – Kubus May 12 '16 at 18:45

1 Answers1

0

Instead of defining name as a primary key in both TestCase and TestCaseSteps Entity,try to define a Surrogate Key as a primary key wherein you keep the Generation Strategy of that Surrogate Key to AUTO

In this way ,you always make a new Insert when you are rerunning test cases.

shankarsh15
  • 1,947
  • 1
  • 11
  • 16
  • This would not help. The issue is caused by the join table, not the way the keys are generated for the entities themselves. This is actually an age-old bug in EclipseLink, as suggested by the post linked in Chris' comment. – Kubus May 12 '16 at 18:48