| Author |
Persistence Example
|
Nikhil Jain
Ranch Hand
Joined: May 15, 2005
Posts: 383
|
|
I get an error when I execute [Cannot add or update a child row: a foreign key constraint fails] My table Professor pid name age cid[FKey to Course] Course cid name Probably its trying to add professor before adding course. What should be the correct way to doing this?
|
SCJP 1.4, SCWCD 1.4, SCBCD 1.5
|
 |
Mirko Bonasorte
Ranch Hand
Joined: May 14, 2007
Posts: 244
|
|
Try using "mappedBy" in the Course object, specifying the ManyToOne relationship. Otherwise, you should use a JoinedTable in Professor class.
|
SCJP<br />SCWCD 1.4 Upgrade (Remember: me stupid)<br />SCWCD 1.4<br /><a href="http://jcp.org/aboutJava/communityprocess/final/jsr220/index.html" target="_blank" rel="nofollow">SCBCD 5.0</a><br /><a href="http://www.enthuware.com" target="_blank" rel="nofollow">SCBCD 5.0 mock exam</a> <br /> <br />SCEA 5 Part1: Preparing...
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14685
|
|
Like Mirko says. If you think about it, how a single cid in Professor could refer to many courses ? If you want to keep the unidirectional relationship, you'll need to use a join table.
|
[My Blog]
All roads lead to JavaRanch
|
 |
nitin pai
Ranch Hand
Joined: May 30, 2006
Posts: 185
|
|
Anu - You would need either a bidirectional or undirectional relationship amongst the entities. Unidirectional in the form of JoinTable OR bidirectional using "mappedBy". I presume that one Professor has one to many relationship with Course. Since Course is the many side it is the owner of the relationship. Hence it would not contain "mappedBy" Since Christopher has already given the unidirectional example, I am modifying your code to use bidirectional as follows:
@Entity public class Professor { @Id @GeneratedValue private int pid; private String name; private String age; @OneToMany(mappedBy="professor") private List<Course> courses= new ArrayList<Course>(); } @Entity public class Course { @Id @GeneratedValue private int cid; private String courseName; @ManyToOne private Professor professor; }
Try this and see if it works.
|
|
 |
Nikhil Jain
Ranch Hand
Joined: May 15, 2005
Posts: 383
|
|
But a similar example was given in O'Rel EJB 3.0 Entity public class Customer implements java.io.Serializable { ... private Collection<Phone> phoneNumbers = new ArrayList<Phone>( ); ... @OneToMany(cascade={CascadeType.ALL}) @JoinColumn (name="CUSTOMER_ID") ?? how is single Customer_Id referring to multiple phone nubers? public Collection<Phone> getPhoneNumbers( ) { return phoneNumbers; } public void setPhoneNumbers(Collection<Phone> phones) { this.phoneNumbers = phones; } }
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14685
|
|
how is single Customer_Id referring to multiple phone nubers?
I was wrong to say "how a single cid in Professor could refer to many courses ?". Your first example would work without a join table if the foreign key was in Course. So you should have a Course table looking like this : CID INT, COURSENAME CHAR(255), PID INT
|
 |
Sergio Tridente
Ranch Hand
Joined: Mar 22, 2007
Posts: 329
|
|
In the book example, the @JoinColumn annotation references the CUSTOMER_ID column in the PHONE table not in the CUSTOMER table. This example is using what the books calls reverse pointers. However, if we take a look at the JPA specs, we'll find the following in section 9.1.24 (OneToMany Annotation):
The default schema-level mapping for unidirectional one-to-many relationships uses a join table, as described in Section 2.1.8.5. Unidirectional one-to-many relationships may be implemented using one-to-many foreign key mappings, however, such support is not required in this release. Applications that want to use a foreign key mapping strategy for one-to-many relationships should make these relationships bidirectional to ensure portability.
There's an example in the book and it seems to be working fine. I guess this is an Hibernate extension. Nevertheless, as far as the exam goes this is not mandatory and the recommended design would be to use one-to-many bidirectional relationships (exactly like in nitin's code snippet). [ June 24, 2008: Message edited by: Sergio Tridente ]
|
SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14685
|
|
Yes, I think that is what "Pro EJB3 : JPA" also refers to : "Having a foreign key in a table for which there is no association in the corresponding entity object model is not in keeping with the data model and not supported by the API". So I should have said "Your first example might work without a join table" instead of "should work". Thanks for the feedback Sergio.
|
 |
 |
|
|
subject: Persistence Example
|
|
|