In
this tutorial we are providing an example about CascadeType that if we are storing any object and we want to store all related objects as well then we need to use CascadeType and if you are not using CascadeType then you need to save all objects one by one as we have done in our previous OneToMany mapping example.
Project View After Completion
Hibernate Environment Setting
Right click over project on Project Explorer and goto properties->Java Build Path->Libraries
Add Required Hibernate jars, JRE system Library, Apache Tomcat server by click on Add Library and Add External Jar ojdbc14.jar by click on Add External Jars.
For Details Read How to set Hibernate Environment
Student.java
This is pojo class where we are using @OneToMany annotation for one to Many mapping between Student and Books class.
Saving all related object with the help of @OneToMany(cascade = {CascadeType.ALL}) Annotation.
Books.java
ExeMain.java
It is working as Persistent class for Hibernate and the same java class we are using to store object of Student and Books class using Hibernate Framework.
Here we are using session.persist(Object) to save all related objects.
hibernate.cfg.xml
This is a file that contains information about Oracle database(or any other database you want to use) Driver and connection information and mapping class information.
Download Code(6 KB)
To Run this Project Right Click inside ExeMain.java, select Run As -> Java Application
All CascadeTypes:
ALL: Cascades all but not the deletion of orphan members
MERGE: session.merge()
PERSIST: session.persist()
REFRESH: session.refresh()
REMOVE: session.delete()
Output:
Project View After Completion
Hibernate Environment Setting
Right click over project on Project Explorer and goto properties->Java Build Path->Libraries
Add Required Hibernate jars, JRE system Library, Apache Tomcat server by click on Add Library and Add External Jar ojdbc14.jar by click on Add External Jars.
For Details Read How to set Hibernate Environment
Student.java
This is pojo class where we are using @OneToMany annotation for one to Many mapping between Student and Books class.
Saving all related object with the help of @OneToMany(cascade = {CascadeType.ALL}) Annotation.
package blog.webideaworld.in;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/*import javax.persistence.JoinTable;
import javax.persistence.JoinColumn;*/
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="MyStudent")
public class Student {
@Id
@GeneratedValue
private int id;
private String name;
private int rollno;
@OneToMany(cascade={CascadeType.PERSIST})
/*@JoinTable(name="Stu_Books",joinColumns=@JoinColumn(name="Stu_id"),
inverseJoinColumns=@JoinColumn(name="book_id"))*/
private Collection<Books> books=new ArrayList<Books>();
public Collection<Books> getBooks() {
return books;
}
public void setBooks(Collection<Books> books) {
this.books = books;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRollno() {
return rollno;
}
public void setRollno(int rollno) {
this.rollno = rollno;
}
}
Books.java
package blog.webideaworld.in;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Books {
@Id
@GeneratedValue
private int bookid;
private String bookname;
public int getBookid() {
return bookid;
}
public void setBookid(int bookid) {
this.bookid = bookid;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
}
ExeMain.java
It is working as Persistent class for Hibernate and the same java class we are using to store object of Student and Books class using Hibernate Framework.
Here we are using session.persist(Object) to save all related objects.
package blog.webideaworld.in;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class ExeMain {
public static void main(String args[]){
Student st =new Student();
st.setName("Suresh");
st.setRollno(44);
Books bk1=new Books();
bk1.setBookname("Let us C");
Books bk2=new Books();
bk2.setBookname("Let us C++");
st.getBooks().add(bk1);
st.getBooks().add(bk2);
@SuppressWarnings("deprecation")
SessionFactory sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
Session session=sessionFactory.openSession();
session.beginTransaction();
session.persist(st);
session.getTransaction().commit();
}
}
hibernate.cfg.xml
This is a file that contains information about Oracle database(or any other database you want to use) Driver and connection information and mapping class information.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="hibernate.connection.username">hr</property>
<property name="hibernate.connection.password">hr</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping class="blog.webideaworld.in.Student"/>
<mapping class="blog.webideaworld.in.Books"/>
</session-factory>
</hibernate-configuration>
Download Code(6 KB)
To Run this Project Right Click inside ExeMain.java, select Run As -> Java Application
All CascadeTypes:
ALL: Cascades all but not the deletion of orphan members
MERGE: session.merge()
PERSIST: session.persist()
REFRESH: session.refresh()
REMOVE: session.delete()
Output:
No comments:
Post a Comment