-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Annotation : One To many Problem
PostPosted: Wed Apr 06, 2011 9:26 am 
Newbie

Joined: Fri Apr 01, 2011 4:59 am
Posts: 6
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+-+Hibernate

Now 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)?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.