I'm having an issue with JPA generating column names containing Java
class names, with some mix of
@Inheritance(strategy=InheritanceType.SINGLE_TABLE and
@SecondaryTables. This results in an error because of a non existent
column:
<openjpa-1.2.2-SNAPSHOT-r422266:821449 nonfatal general error>
org.apache.openjpa.persistence.PersistenceException: DB2 SQL error:
SQLCODE: -206, SQLSTATE: 42703, SQLERRMC: T1.VALUEDCUSTOMER_ID
1607819221 SELECT ... FROM CUSTOMERRATING t0 INNER JOIN CUSTOMER t1 on
t0.ID = t1.VALUEDCUSTOMER_ID INNER JOIN GREATCUSTOMERDATA t2 ON t0.id
= t2.id ...
ValuedCustomer is the class of one of the Java entities on the table.
Why would JPA prefix the column name with the class name
(VALUEDCUSTOMER_ID)? The column name is just ID. The error goes away
when removing the name field from ValuedCustomer class. It can be
undesirable to have to define all columns in the root class. I did try
copying down the @SecondaryTables annotation but it didnt help.
This entity is being migrated from TOPLink (11g) which does not have
this issue. (side problem: why does JPA insist the discriminator
column be in the primary table and has this been addressed in later
versions? I had to swap CUSTOMERRATING and CUSTOMER to make JPA happy
(arguably, bad legacy table design to begin with).
The configuration follows. The above error occurs for a find() for a
row that maps to GREATCUSTOMER (not VALUEDCUSTOMER, which makes it
even more insteresting).
Hopefully i haven't mangled the example while substituting fake names...
//CLASS 1
@Entity
@Table(name="CUSTOMERRATING")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
// in the real code, pkJoinColumns needed...
@SecondaryTables({
@SecondaryTable(name="CUSTOMER",pkJoinColumns = @PrimaryKeyJoinColumn
(name="ID" , referencedColumnName="ID"))
,@SecondaryTable(name="MORECUSTOMERDATA",pkJoinColumns = @PrimaryKeyJoinColumn(
name="ID" , referencedColumnName="ID"))
@DiscriminatorColumn(name="RATING")
public class Customer extends EntityImpl implements Serializable {
@Column(name="ID")
@Id
protected String id;
@Column(table="CUSTOMERRATING",name="RATING")
private String rating;
@Column(table="CUSTOMER",name="NAME")
private String name;
//Class 2
@Entity
@DiscriminatorValue(value="2")
public class ValuedCustomer extends Customer implements Serializable {
@Column(table="CUSTOMER",name="NAME")
private String name;}
// Class 3
@Entity
@SecondaryTable(
name="GREATCUSTOMERDATA"
,pkJoinColumns = @PrimaryKeyJoinColumn(
name="ID"
, referencedColumnName="ID"))
@DiscriminatorValue(value="9")
public class GreatCustomer extends ValuedCustomer implements Serializable {
@Column(table="GREATCUSTOMERDATA",name="ID")
private String greatCustomerdataId;