im having a slight problem with the @OneToMany relationship.
According to the docs this approached is not recommended
Code:
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinColumn(name="CUSTOMER_ADDRESS_ID")
But it works! However it creates 2 sql statements instead of one as i found out myself when I turned on showsql property good article here:
http://i-proving.ca/space/Technologies/Hibernate/Hibernate+Annotation+Examples/Unidirectional+one-to-many+using+a+Join+Column+-+HibernateNow I created the following entry:
Code:
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinTable(
name="CUSTOMER_ADDRESS",
joinColumns = @JoinColumn( name="CUSTOMER_ADDRESS_ID"),
inverseJoinColumns = @JoinColumn( name="CUSTOMERID")
)
private List<Address> list;
CUSTOMERID is the PK of CUSTOMER table
CUSTOMER_ADDRESS_ID is on teh ADDRESS Table and is the FK for CUSTOMER.CUSTOMERID
This doesn't work saying invalid identifier for "CUSTOMERID" - which i can only assume because it looks on the ADDRESS table instead of CUSTOMER table, which doesn't make sense with the word inverseJoinColumns....
So I tried this:
Code:
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinTable(
name="CUSTOMER_ADDRESS",
joinColumns = @JoinColumn( name="CUSTOMER_ADDRESS_ID"),
inverseJoinColumns = @JoinColumn( name="ID")
)
private List<Address> list;
which creates 2 sql inserts for list of size 1 with 1 address.
Code:
....
insert into CUSTOMER_ADDRESS (CUSTOMER_ADDRESS_ID, SORTORDER, ADDRESS_LINE, ADDRESS_LINE2, CITY, STATE, id) values (?, ?, ?, ?, ?, ?, ?)
insert into CUSTOMER_ADDRESS (CUSTOMER_ADDRESS_ID, ID) values (?, ?)
so 1 is the actual data inserted correctly but with no CUSTOMER_ADDRESS_ID set and the other is just nulls??
so what i want to know is how do I correctly map what i want above using the @JoinTable using the primary key(CUSTOMERID) of Customer as the id to link the Address table(CUSTOMER_ADDRESS_ID)?