Search code examples
javaoracle-databasejpaopenjpa

Open JPA - @TableGenerator with composite primary key for auxiliary table


I have below entity which uses TABLE strategy for its id generation and works perfectly fine.

@Entity
@Table(name = "Test_Table")
public class SomeEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    @TableGenerator(name = "TABLE_GEN", table = "AUX_TABLE", pkColumnName = "KEY_NAME", valueColumnName = "KEY_VALUE", pkColumnValue = "someValue", allocationSize = 10)
    @Id
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "TABLE_GEN")
    @Column(unique = true, nullable = false, precision = 20)
    private long someId;

    // few other properties and getter / setter
}

Below is table structure of AUX_TABLE used to fetch the id values

DOMAIN varchar2(4) NOT NULL,
KEY_NAME varchar2(100) NOT NULL,
KEY_VALUE decimal(2) NOT NULL,
CONSTRAINT AUX_TABLE_PK PRIMARY KEY (DOMAIN,KEY_NAME)

As AUX_TABLE has composite primary key comprising of DOMAIN and KEY_NAME; I am unable to figure how to specify them in pkColumnName and their respective value in pkColumnValue attribute of @TableGenerator.

I tried pkColumnName = "DOMAIN,KEY_NAME" but it failed with following exception

Caused by: org.apache.openjpa.lib.jdbc.ReportingSQLException: ORA-00904: "DOMAIN,KEY_NAME": invalid identifier {prepstmnt 9864699 SELECT KEY_VALUE FROM AUX_TABLE WHERE "DOMAIN,KEY_NAME" = ? FOR UPDATE [params=?]} [code=904, state=42000] at org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator.wrap(LoggingConnectionDecorator.java:219) at org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator.wrap(LoggingConnectionDecorator.java:203) at org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator.access$700(LoggingConnectionDecorator.java:59) at org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator$LoggingConnection$LoggingPreparedStatement.executeQuery(LoggingConnectionDecorator.java:1118)

The java docs for @TableGenerator has no mention for this particular case.

I am using Open JPA 2.3.0 with JDK 1.7.0_45


Solution

  • You cannot. TableGenerator has a simple key/value structure.