• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Persistence Example

 
Ranch Hand
Posts: 393
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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?
 
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try using "mappedBy" in the Course object, specifying the ManyToOne relationship.
Otherwise, you should use a JoinedTable in Professor class.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 393
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic