• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Hibernate POJOs -- association or no-association ?

 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
The government thinks you are too stupid to make your own lightbulb choices. But this tiny ad thinks you are smart:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic