aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate POJOs -- association or no-association ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate POJOs -- association or no-association ?" Watch "Hibernate POJOs -- association or no-association ?" New topic
Author

Hibernate POJOs -- association or no-association ?

ben oliver
Ranch Hand

Joined: Mar 28, 2006
Posts: 375
Suppose I have two database tables

Course { Course_no(PK), course_name, student_id (FK refers to "Student") }
Student { id (PK), name }

each student can take many courses so it is a many-to-one relation from "Course" to "Student". Suppose we don't want any delete cascade function being implemented. I am thinking about two ways to use hibernate to implement but not sure if both work.

***************************************
approach 1) Use no direction association in POJO

Class Course {
private int course_no;
private String course_title;
private int student_id;
}

class Student {
private int id;
private String name;
}

In the <hibernate-mapping> we don't have any <many-to-one> xml.

approach 2) Use association in POJO
class course {
private int course_no;
private String course_title;
private Studetn student;
}
class Student {
private int id;
private String name;
}

In the <hibernate-mapping> we have

<many-to-one
name="student" column="student_id" class="Student" foreign-key="fk_student"/>

*********************************************

It seems most books use the 2nd approach. But I found that when I use the 1st approach without having that <many-to-one> xml section in <hibernate-mapping>, it works fine for me. I can insert and update "course" and "student" tables separately. The database itself has its foreign constraint set up so I don't need to worry about the the constraint. Plus, if I try to insert a "course" object, I don't have to create a "student" object first (like in the 2nd approach) before I can save(course).. As long as I know the "student_id", just simply create the course object itself and save it to the table.

Does the 1st approach work ?

Thank you.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Not quite sure what the two options mean, I am sorry but I didn't read it to deeply. But in Hibernate and other ORMs, just because you define an association doesn't mean it is required. Meaning I can map a Many-To-One and map the one side to be nullable and allow nulls, and therefore create a course without having to create a student. And even the other way around. The whole cascade is based on what cascade options you set. So it is all configurable and open to any design you want.


btw, student to courses is usually a many to many, as students can take many courses, and each course can have many students in it.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate POJOs -- association or no-association ?