One-to-One mapping
Concept of Primary key and Foreign Key
Create the tables in you database
ONE TO ONE UNI-DIRECTIONAL
Cascade Save Example
--------------------------------------------------------------------------------------------
Instructor.java
--------------------------------------------------------------------------------------------
package com.demo.spring.entitiy;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name="instructor")
public class Instructor {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private int id;
@Column(name="first_name")
private String first_name;
@Column(name="last_name")
private String last_name;
@Column(name="email")
private String email;
@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name="instructor_detail_id")
private InstructorDetail instructorDetail;
public Instructor(){}
public Instructor(String first_name, String last_name, String email) {
super();
this.first_name = first_name;
this.last_name = last_name;
this.email = email;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public InstructorDetail getInstructorDetail() {
return instructorDetail;
}
public void setInstructorDetail(InstructorDetail instructorDetail) {
this.instructorDetail = instructorDetail;
}
@Override
public String toString() {
return "Instructor [id=" + id + ", first_name=" + first_name
+ ", last_name=" + last_name + ", email=" + email
+ ", instructorDetail=" + instructorDetail + "]";
}
}
--------------------------------------------------------------------------------------------
InstructorDetail.java
--------------------------------------------------------------------------------------------
package com.demo.spring.entitiy; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="instructor_detail") public class InstructorDetail { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name="id") private int id; @Column(name="youtube_channel") private String youtubeChannel; @Column(name="hobby") private String hobby; public InstructorDetail(){ } public InstructorDetail(String youtubeChannel, String hobby) { super(); this.youtubeChannel = youtubeChannel; this.hobby = hobby; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getYoutubeChannel() { return youtubeChannel; } public void setYoutubeChannel(String youtubeChannel) { this.youtubeChannel = youtubeChannel; } public String getHobby() { return hobby; } public void setHobby(String hobby) { this.hobby = hobby; } @Override public String toString() { return "InstructorDetail [id=" + id + ", youtubeChannel=" + youtubeChannel + ", hobby=" + hobby + "]"; } }
--------------------------------------------------------------------------------------------
CreateDemo.java
--------------------------------------------------------------------------------------------
package com.demo.spring.test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import com.demo.spring.entitiy.Instructor; import com.demo.spring.entitiy.InstructorDetail; public class CreateDemo { public static void main(String[] args) { SessionFactory factory=new Configuration() .configure("hibernate.cfg.xml") .addAnnotatedClass(Instructor.class) .addAnnotatedClass(InstructorDetail.class) .buildSessionFactory(); Session session=factory.getCurrentSession(); try{ session=factory.getCurrentSession(); Instructor tempInstructor=new Instructor("Maurya","Sudarshan","sudarshan.maurya@tiwari.com"); InstructorDetail tempInstructorDetail=new InstructorDetail("http://www.sudarshan.maurya.com","Luv 2 sleep"); tempInstructor.setInstructorDetail(tempInstructorDetail); System.out.println("saving : " +tempInstructor); session.beginTransaction(); session.save(tempInstructor); session.getTransaction().commit(); } finally{ session.close(); } } }
--------------------------------------------------------------------------------------------
OUTPUT
--------------------------------------------------------------------------------------------
Mar 06, 2018 4:13:33 AM org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {5.2.14.Final} Mar 06, 2018 4:13:33 AM org.hibernate.cfg.Environment <clinit> INFO: HHH000206: hibernate.properties not found Mar 06, 2018 4:13:34 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit> INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final} Mar 06, 2018 4:13:34 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!) Mar 06, 2018 4:13:34 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl
buildCreator INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/bikesh?useSSL=false] Mar 06, 2018 4:13:34 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl
buildCreator INFO: HHH10001001: Connection properties: {user=root, password=****} Mar 06, 2018 4:13:34 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl
buildCreator INFO: HHH10001003: Autocommit mode: false Mar 06, 2018 4:13:34 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl
$PooledConnections <init> INFO: HHH000115: Hibernate connection pool size: 1 (min=1) Mar 06, 2018 4:13:34 AM org.hibernate.dialect.Dialect <init> INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect saving : Instructor [id=0, first_name=Maurya, last_name=Sudarshan, email=sudarshan.maurya@tiwari.com,
instructorDetail=InstructorDetail
[id=0, youtubeChannel=http://www.sudarshan.maurya.com, hobby=Luv 2 sleep]] Hibernate: insert into instructor_detail (hobby, youtube_channel) values (?, ?) Hibernate: insert into instructor (email, first_name, instructor_detail_id, last_name) values (?, ?, ?, ?)
Cascade Delete Example
--------------------------------------------------------------------------------------------
DeleteDemo.java
--------------------------------------------------------------------------------------------
package com.demo.spring.test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import com.demo.spring.entitiy.Instructor; import com.demo.spring.entitiy.InstructorDetail; public class DeleteDemo { public static void main(String[] args) { SessionFactory factory=new Configuration() .configure("hibernate.cfg.xml") .addAnnotatedClass(Instructor.class) .addAnnotatedClass(InstructorDetail.class) .buildSessionFactory(); Session session=factory.getCurrentSession(); try{ session=factory.getCurrentSession(); session.beginTransaction(); int theId=1; Instructor tmpInstructor=session.get(Instructor.class, theId); System.out.println("Found instuctor : "+ tmpInstructor); if(tmpInstructor !=null){ System.out.println("deleting : " + tmpInstructor); session.delete(tmpInstructor); } session.getTransaction().commit(); } finally{ session.close(); } } }
--------------------------------------------------------------------------------------------
OUTPUT
--------------------------------------------------------------------------------------------
Mar 06, 2018 4:11:08 AM org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {5.2.14.Final} Mar 06, 2018 4:11:08 AM org.hibernate.cfg.Environment <clinit> INFO: HHH000206: hibernate.properties not found Mar 06, 2018 4:11:08 AM org.hibernate.annotations.common.reflection.java
.JavaReflectionManager <clinit> INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final} Mar 06, 2018 4:11:08 AM org.hibernate.engine.jdbc.connections.internal
.DriverManagerConnectionProviderImpl configure WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!) Mar 06, 2018 4:11:08 AM org.hibernate.engine.jdbc.connections.internal
.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL
[jdbc:mysql://localhost:3306/bikesh?useSSL=false] Mar 06, 2018 4:11:08 AM org.hibernate.engine.jdbc.connections.internal
.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001001: Connection properties: {user=root, password=****} Mar 06, 2018 4:11:08 AM org.hibernate.engine.jdbc.connections.internal
.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001003: Autocommit mode: false Mar 06, 2018 4:11:08 AM org.hibernate.engine.jdbc.connections.internal
.DriverManagerConnectionProviderImpl$PooledConnections <init> INFO: HHH000115: Hibernate connection pool size: 1 (min=1) Mar 06, 2018 4:11:09 AM org.hibernate.dialect.Dialect <init> INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect Hibernate: select instructor0_.id as id1_0_0_, instructor0_.email as email2_0_0_,
instructor0_.first_name as first_na3_0_0_, instructor0_.instructor_detail_id
as instruct5_0_0_, instructor0_.last_name as last_nam4_0_0_, instructor1_.id
as id1_1_1_, instructor1_.hobby as hobby2_1_1_, instructor1_.youtube_channel
as youtube_3_1_1_ from instructor instructor0_ left outer join instructor_detail
instructor1_ on instructor0_.instructor_detail_id=instructor1_.id where instructor0_.id=? Found instuctor : Instructor [id=1, first_name=Bikesh, last_name=Kumar,
email=bikesh.kumar@tiwari.com, instructorDetail=InstructorDetail [id=1,
youtubeChannel=http://www.bikesh.kumar.com, hobby=Luv 2 code]] deleting : Instructor [id=1, first_name=Bikesh, last_name=Kumar,
email=bikesh.kumar@tiwari.com, instructorDetail=InstructorDetail [id=1,
youtubeChannel=http://www.bikesh.kumar.com, hobby=Luv 2 code]] Hibernate: delete from instructor where id=? Hibernate: delete from instructor_detail where id=?
ONE TO ONE BI-DIRECTIONAL
Cascade Read Example
--------------------------------------------------------------------------------------------
Instructor.java
--------------------------------------------------------------------------------------------
package com.demo.spring.entitiy; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToOne; import javax.persistence.Table; @Entity @Table(name="instructor") public class Instructor { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name="id") private int id; @Column(name="first_name") private String first_name; @Column(name="last_name") private String last_name; @Column(name="email") private String email; @OneToOne(cascade=CascadeType.ALL) @JoinColumn(name="instructor_detail_id") private InstructorDetail instructorDetail; public Instructor(){} public Instructor(String first_name, String last_name, String email) { super(); this.first_name = first_name; this.last_name = last_name; this.email = email; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getFirst_name() { return first_name; } public void setFirst_name(String first_name) { this.first_name = first_name; } public String getLast_name() { return last_name; } public void setLast_name(String last_name) { this.last_name = last_name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public InstructorDetail getInstructorDetail() { return instructorDetail; } public void setInstructorDetail(InstructorDetail instructorDetail) { this.instructorDetail = instructorDetail; } @Override public String toString() { return "Instructor [id=" + id + ", first_name=" + first_name + ", last_name=" + last_name + ", email=" + email + ", instructorDetail=" + instructorDetail + "]"; } }
--------------------------------------------------------------------------------------------
Instructordetail.java
--------------------------------------------------------------------------------------------
package com.demo.spring.entitiy;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name="instructor_detail")
public class InstructorDetail {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private int id;
@Column(name="youtube_channel")
private String youtubeChannel;
@Column(name="hobby")
private String hobby;
@OneToOne(mappedBy="instructorDetail",cascade=CascadeType.ALL)
private Instructor instructor;
public Instructor getInstructor() {
return instructor;
}
public void setInstructor(Instructor instructor) {
this.instructor = instructor;
}
public InstructorDetail(){
}
public InstructorDetail(String youtubeChannel, String hobby) {
super();
this.youtubeChannel = youtubeChannel;
this.hobby = hobby;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getYoutubeChannel() {
return youtubeChannel;
}
public void setYoutubeChannel(String youtubeChannel) {
this.youtubeChannel = youtubeChannel;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
@Override
public String toString() {
return "InstructorDetail [id=" + id + ", youtubeChannel="
+ youtubeChannel + ", hobby=" + hobby + "]";
}
}
--------------------------------------------------------------------------------------------
GetInstructordetaildemo.java
--------------------------------------------------------------------------------------------
package com.demo.spring.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.demo.spring.entitiy.Instructor;
import com.demo.spring.entitiy.InstructorDetail;
public class GetInstructorDetaildemo {
public static void main(String[] args) {
SessionFactory factory=new Configuration()
.configure("hibernate.cfg.xml")
.addAnnotatedClass(Instructor.class)
.addAnnotatedClass(InstructorDetail.class)
.buildSessionFactory();
Session session=factory.getCurrentSession();
try{
session=factory.getCurrentSession();
session.beginTransaction();
int theId=2;
InstructorDetail tempInstructorDetail=session.get(InstructorDetail.class, theId);
System.out.println("tempInstructorDetail : "+tempInstructorDetail);
System.out.println("the associated instructor : "+tempInstructorDetail.getInstructor());
session.getTransaction().commit();
}
finally{
session.close();
}
}
}
--------------------------------------------------------------------------------------------
OUTPUT
--------------------------------------------------------------------------------------------
Mar 07, 2018 12:53:04 AM org.hibernate.Version logVersionINFO: HHH000412: Hibernate Core {5.2.14.Final}
Mar 07, 2018 12:53:04 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Mar 07, 2018 12:53:04 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Mar 07, 2018 12:53:04 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Mar 07, 2018 12:53:04 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/bikesh?useSSL=false]
Mar 07, 2018 12:53:04 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
Mar 07, 2018 12:53:04 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Mar 07, 2018 12:53:04 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
Mar 07, 2018 12:53:05 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Hibernate: select instructor0_.id as id1_1_0_, instructor0_.hobby as hobby2_1_0_, instructor0_.youtube_channel as youtube_3_1_0_, instructor1_.id as id1_0_1_, instructor1_.email as email2_0_1_, instructor1_.first_name as first_na3_0_1_, instructor1_.instructor_detail_id as instruct5_0_1_, instructor1_.last_name as last_nam4_0_1_ from instructor_detail instructor0_ left outer join instructor instructor1_ on instructor0_.id=instructor1_.instructor_detail_id where instructor0_.id=?
tempInstructorDetail : InstructorDetail [id=2, youtubeChannel=http://www.amitesh.kumar.com, hobby=Luv 2 eat]
the associated instructor : Instructor [id=2, first_name=Amitesh, last_name=Kumar, email=amitesh.kumar@tiwari.com, instructorDetail=InstructorDetail [id=2, youtubeChannel=http://www.amitesh.kumar.com, hobby=Luv 2 eat]]
Cascade Delete
--------------------------------------------------------------------------------------------
DeleteinstructorDetailDemo.java
--------------------------------------------------------------------------------------------
package com.demo.spring.test;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.demo.spring.entitiy.Instructor;
import com.demo.spring.entitiy.InstructorDetail;
public class DeleteInstructorDetailDemo {
public static void main(String[] args) {
SessionFactory factory=new Configuration()
.configure("hibernate.cfg.xml")
.addAnnotatedClass(Instructor.class)
.addAnnotatedClass(InstructorDetail.class)
.buildSessionFactory();
Session session=factory.getCurrentSession();
try{
session=factory.getCurrentSession();
session.beginTransaction();
int theId=2;
InstructorDetail tempInstructordetail=session.get(InstructorDetail.class, theId);
System.out.println("Instructordetail : "+tempInstructordetail);
System.out.println("The associated instructor : "+tempInstructordetail.getInstructor());
System.out.println("Deleting tempInstructorDetail : "+tempInstructordetail);
session.delete(tempInstructordetail);
session.getTransaction().commit();
}
catch(Exception e)
{
e.printStackTrace();
}
finally{
factory.close();
session.close();
}
}
}
--------------------------------------------------------------------------------------------
OUTPUT
--------------------------------------------------------------------------------------------
Mar 07, 2018 2:00:56 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.14.Final}
Mar 07, 2018 2:00:56 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Mar 07, 2018 2:00:56 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Mar 07, 2018 2:00:56 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Mar 07, 2018 2:00:56 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/bikesh?useSSL=false]
Mar 07, 2018 2:00:56 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
Mar 07, 2018 2:00:56 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Mar 07, 2018 2:00:56 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
Mar 07, 2018 2:00:56 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Hibernate: select instructor0_.id as id1_1_0_, instructor0_.hobby as hobby2_1_0_, instructor0_.youtube_channel as youtube_3_1_0_, instructor1_.id as id1_0_1_, instructor1_.email as email2_0_1_, instructor1_.first_name as first_na3_0_1_, instructor1_.instructor_detail_id as instruct5_0_1_, instructor1_.last_name as last_nam4_0_1_ from instructor_detail instructor0_ left outer join instructor instructor1_ on instructor0_.id=instructor1_.instructor_detail_id where instructor0_.id=?
Instructordetail : InstructorDetail [id=2, youtubeChannel=http://www.amitesh.kumar.com, hobby=Luv 2 eat]
The associated instructor : Instructor [id=2, first_name=Amitesh, last_name=Kumar, email=amitesh.kumar@tiwari.com, instructorDetail=InstructorDetail [id=2, youtubeChannel=http://www.amitesh.kumar.com, hobby=Luv 2 eat]]
Deleting tempInstructorDetail : InstructorDetail [id=2, youtubeChannel=http://www.amitesh.kumar.com, hobby=Luv 2 eat]
Hibernate: delete from instructor where id=?
Hibernate: delete from instructor_detail where id=?
Mar 07, 2018 2:00:58 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH10001008: Cleaning up connection pool [jdbc:mysql://localhost:3306/bikesh?useSSL=false]
Delete without Cascade
--------------------------------------------------------------------------------------------
InstructorDetail.java
--------------------------------------------------------------------------------------------
package com.demo.spring.entitiy;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name="instructor_detail")
public class InstructorDetail {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private int id;
@Column(name="youtube_channel")
private String youtubeChannel;
@Column(name="hobby")
private String hobby;
@OneToOne(mappedBy="instructorDetail",cascade={CascadeType.DETACH,CascadeType.MERGE,
CascadeType.PERSIST,CascadeType.PERSIST,CascadeType.REFRESH})
private Instructor instructor;
public Instructor getInstructor() {
return instructor;
}
public void setInstructor(Instructor instructor) {
this.instructor = instructor;
}
public InstructorDetail(){
}
public InstructorDetail(String youtubeChannel, String hobby) {
super();
this.youtubeChannel = youtubeChannel;
this.hobby = hobby;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getYoutubeChannel() {
return youtubeChannel;
}
public void setYoutubeChannel(String youtubeChannel) {
this.youtubeChannel = youtubeChannel;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
@Override
public String toString() {
return "InstructorDetail [id=" + id + ", youtubeChannel="
+ youtubeChannel + ", hobby=" + hobby + "]";
}
}
--------------------------------------------------------------------------------------------
DeleteInstructorDetailDemo.java
--------------------------------------------------------------------------------------------
package com.demo.spring.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.demo.spring.entitiy.Instructor;
import com.demo.spring.entitiy.InstructorDetail;
public class DeleteInstructorDetailDemo {
public static void main(String[] args) {
SessionFactory factory=new Configuration()
.configure("hibernate.cfg.xml")
.addAnnotatedClass(Instructor.class)
.addAnnotatedClass(InstructorDetail.class)
.buildSessionFactory();
Session session=factory.getCurrentSession();
try{
session=factory.getCurrentSession();
session.beginTransaction();
int theId=6;
InstructorDetail tempInstructordetail=session.get(InstructorDetail.class, theId);
System.out.println("Instructordetail : "+tempInstructordetail);
tempInstructordetail.getInstructor().setInstructorDetail(null);;
System.out.println("Deleting tempInstructorDetail : "+tempInstructordetail);
session.delete(tempInstructordetail);
session.getTransaction().commit();
}
catch(Exception e)
{
e.printStackTrace();
}
finally{
session.close();
}
}
}
--------------------------------------------------------------------------------------------
OUTPUT
--------------------------------------------------------------------------------------------
Mar 07, 2018 2:19:51 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.14.Final}
Mar 07, 2018 2:19:51 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Mar 07, 2018 2:19:52 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Mar 07, 2018 2:19:52 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Mar 07, 2018 2:19:52 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/bikesh?useSSL=false]
Mar 07, 2018 2:19:52 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
Mar 07, 2018 2:19:52 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Mar 07, 2018 2:19:52 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
Mar 07, 2018 2:19:52 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Hibernate: select instructor0_.id as id1_1_0_, instructor0_.hobby as hobby2_1_0_, instructor0_.youtube_channel as youtube_3_1_0_, instructor1_.id as id1_0_1_, instructor1_.email as email2_0_1_, instructor1_.first_name as first_na3_0_1_, instructor1_.instructor_detail_id as instruct5_0_1_, instructor1_.last_name as last_nam4_0_1_ from instructor_detail instructor0_ left outer join instructor instructor1_ on instructor0_.id=instructor1_.instructor_detail_id where instructor0_.id=?
Instructordetail : InstructorDetail [id=6, youtubeChannel=http://www.sudarshan.maurya.com, hobby=Luv 2 sleep]
Deleting tempInstructorDetail : InstructorDetail [id=6, youtubeChannel=http://www.sudarshan.maurya.com, hobby=Luv 2 sleep]
Hibernate: update instructor set email=?, first_name=?, instructor_detail_id=?, last_name=? where id=?
Hibernate: delete from instructor_detail where id=?
No comments:
Post a Comment