Hibernate Tutorial part 10 - Many to One Mapping in a Hibernate Application in detail

preview_player
Показать описание

( Concept and hands on for ManytoOne mapping - unidirectional in detail using Eclipse IDE )
Рекомендации по теме
Комментарии
Автор

in the next tutorial, we will learn the concept of ManyToOne / OneToMany Bidirectional Mapping in a Hibernate Application

gontuseries
Автор

Very simple and easy language any buddy can understand.
Really helpful.
thanks.

MrDars
Автор

Thanks for the tutorials,

I have a doubt, you have no where given the coloumn name StudentAddress_address_id in student class where you have written the setter getter methods then how it is taking this coloumn name in database in student table and you have not mentioned to map address_id with StudentAddress_address_id then how it is doing the mapping, please assist.

itsagame
Автор

best tutorial, best way to learn easy from this, Thanks sir

raujhkumar
Автор

Amazing tutorial. You explain very well. Thanks mate.

luishenriques
Автор

Amazing Gaurav. Please make some videos on Design Patterns. Your explanation is amazing.

MrSagar
Автор

Your Tutorials are really good . 
private StudentAddress studentAddress; ??
 

amdkamil
Автор

u r doing great job man... hoping for more advance topic videos....

hiteshupreti
Автор

ola.. can you tell me why are students ids and studentAddress ids sharing 1 sequence ? stud1 has id1, stud2 has id 3 and address id is 2 ? :/

erikparso
Автор

Excellent tutorials. really helpfull.Thanks for uploading  this kind of tutorials.

girishgandhi
Автор

let's say i have a student_address and 2 student objects instantiated. Assuming that the student_address already exists in the database, what will happen if i set both of students address to student_address object and perform save? does it create a new student_address data on my db?

jasperbernales
Автор

nice tutorials. really helpfull.Thanks for uploading  this kind of tutorials.

prafullaism
Автор

which is the most used method? XML based or annotation based? I didn't find xml mapping video yet.

madhusudandad
Автор

Thanks, I was trying to figure this out. Passing an entire StudentAddress object into the Student table address_id column was not obvious to me. It would have been helpful to see the data after the commit though.

BreakingValhalla
Автор

Hi Gaurav, Thanks for nice explanation .
I have create Student.java and StudentAddress.java .
when I use
session.save(student1)
session.save(student2)

it gives constarint violation excep : parent key not found in StudentAddress table
but
If i save studentaddress, it works .
session.save(studentaddress )
session.save(student1)
session.save(student2)
,
My question is why studentAdress is not populated when i am doing session.save(student1)
session.save(student2) as shown in your video ...Please help

I am using oracle 10g XE


package

import
import javax.persistence.Column;
import javax.persistence.Entity;
import
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;


@Entity
@Table(name="STUDENT")
public class Student {
@Id
@GeneratedValue
@Column(name="STU_ID")
private int studentId;

@Column(name="STU_NAME")
private String studentName;


private StudentAddress studentAddress;





/**
* @return the studentId
*/
public int getStudentId() {
return studentId;
}
/**
* @param studentId the studentId to set
*/
public void setStudentId(int studentId) {
this.studentId = studentId;
}
/**
* @return the studentName
*/
public String getStudentName() {
return studentName;
}
/**
* @param studentName the studentName to set
*/
public void setStudentName(String studentName) {
this.studentName = studentName;
}
/**
* @return the studentAddress
*/
public StudentAddress getStudentAddress() {
return studentAddress;
}
/**
* @param studentAddress the studentAddress to set
*/
public void studentAddress) {
this.studentAddress = studentAddress;
}


}



package

import javax.persistence.Column;
import javax.persistence.Entity;
import
import javax.persistence.Id;
import javax.persistence.Table;

@Entity

public class StudentAddress {

@Id
@GeneratedValue
@Column(name="ADD_ID")
private int addressId;

@Column(name="ADD_DETAIL")
private String addressDetail;




/**
* @return the addressId
*/
public int getAddressId() {
return addressId;
}
/**
* @param addressId the addressId to set
*/
public void setAddressId(int addressId) {
this.addressId = addressId;
}
/**
* @return the addressDetail
*/
public String getAddressDetail() {
return addressDetail;
}
/**
* @param addressDetail the addressDetail to set
*/
public void setAddressDetail(String addressDetail) {
this.addressDetail = addressDetail;
}



}


package

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import

public class ManyToOneUnidirectionalClient {

public static void main(String[] args) {
Configuration cfg = new Configuration();


SessionFactory sf

StudentAddress studentAddress = new StudentAddress();

, Bihar");

Student student1 = new Student();
student1.setStudentId(1001);



Student student2 = new Student();
student2.setStudentId(1002);




Session session = sf.openSession();


session.save(student1);
session.save(student2);

Transaction tx = session.beginTransaction();

tx.commit();
session.close();
System.out.println("ending -- -- --");
}

}

OutPut :

Hibernate: drop table STUDENT cascade constraints
Hibernate: drop table STUDENT_ADDRESS cascade constraints
Hibernate: drop sequence hibernate_sequence
Hibernate: create table STUDENT (STU_ID number(10, 0) not null, STU_NAME varchar2(255 char), studentAddress_ADD_ID number(10, 0), primary key (STU_ID))
Hibernate: create table STUDENT_ADDRESS (ADD_ID number(10, 0) not null, ADD_DETAIL varchar2(255 char), primary key (ADD_ID))
Hibernate: alter table STUDENT add constraint FK_9vmc4l3dk624e0hxkosegx0ip foreign key (studentAddress_ADD_ID) references STUDENT_ADDRESS
Hibernate: create sequence hibernate_sequence
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into STUDENT (studentAddress_ADD_ID, STU_NAME, STU_ID) values (?, ?, ?)
Exception in thread "main" could not execute statement
at
at
at
at
at
at
at
at
at
at
at
at
at
at
at
at
at
at
Caused by: java.sql.SQLException: ORA-02291: integrity constraint violated - parent key not found

at
at
at
at
at
at
at
at
at
at
... 13 more

deshdeepak
Автор

Great Explanation. Thanks a lot. Please share your link for Spring.

saiganesh
Автор

A big thank you for you. Great explanations.

meriemkaroun
Автор

hi, i tried this but in the student table id's are 1 & 3 and studentaddress table id are 2 and mapping address id 2 for both student id's fro student table..tell me why

karanivenkatrao
Автор

thanks ! this tutorial really helps. God bless.

Love all, serve all
Regards, Bhargav

bhargavchoksi
Автор

Nice tutorial but I have 1 doubt. Is this a one to many example because no 2 student will have same address than how many to one relation is establish.I think STUDENT and CLASS can be good example where many student can be in 1 class and 1 class can have many student.please correct me if I am wrong

irfniks