File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate POJOs -- association or no-association ?" Watch "Hibernate POJOs -- association or no-association ?" New topic

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

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

Joined: Feb 05, 2001
Posts: 17276

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.


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
I agree. Here's the link:
subject: Hibernate POJOs -- association or no-association ?
It's not a secret anymore!