I have spent a lot of time trying to solve this. It has to do with the way composite id's get mapped. I have RTFM more than enough times. So I 'm going to post the 3 tables I want to get mapped and if anyone cares to help by drawing up the corresponding hibernate xml mappings (ejb annotations work fine), please do. Hibernate is version 3.2.6GA, Hibernate entity manager is 3.3.2GA and AS is GlassFish V2U2.
Table 1
Name table_1
Column name t1_a, type Integer, Primary Key
Column name t1_b, type String
Table 2
Name table_2
Column name t2_a, type Integer, Primary Key, Foreign Key referencing t1_a
Column name t2_b, type Date, Primary Key
Column name t2_c, type String
Column name t2_d, type String, Foreign Key referencing t3_a
Table 3
Name table_3
Column name t3_a, type String, Primary Key
Entity classes are table1, table2 plus table2PK and table3
Table 1 should have a one to many collection of Table 2
Table 3 should have a one to many collection of Table 2 too
Table 2 therefore must have a field for the composite (embedded) key, a field for column t2_c and another one for column t2_d plus the opposite end (many to one) for the collections.
The ejb-annotated (working) entity for table 2 looks like this:
Code:
@Entity
@Table(name = "table_2")
public class Table2 implements Serializable {
@EmbeddedId
protected Table2PK table2PK;
@Column(name = "t2_c", nullable = false)
private String t2C;
@Column(name = "t2_d", nullable = false)
private String t2D;
@JoinColumn(name = "t2_a", referencedColumnName = "t1_a", insertable = false, updatable = false)
@ManyToOne
private Table_1 table1;
@JoinColumn(name = "t2_d", referencedColumnName = "t3_a", insertable = false, updatable = false)
@ManyToOne
private Table_3 table3;
...
@Embeddable
public class Table2PK implements Serializable {
@Column(name = "t2_a", nullable = false)
private int t2A;
@Column(name = "t2_b", nullable = false)
@Temporal(TemporalType.DATE)
private Date t2B;
...
I 'm going to get a beer now and watch Lost.
Cheers,
Andreas