Advertisement
cascade = {CascadeType.ALL}
package decodejava;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table (name ="USER_DETAILS")
public class User_Details1 {
@Id
@GeneratedValue
@Column (name = "USER_ID")
private int id;
@Column (name = "First_Name")
private String firstName;
@Column (name = "Last_Name")
private String lastName;
@OneToMany(cascade = {CascadeType.ALL})
@JoinColumn(name = "USER_ID")
private List<Mobile_Num> mob= new ArrayList<Mobile_Num>();
public List<Mobile_Num> getMob()
{
return mob;
}
public void setMob(List<Mobile_Num> mob)
{
this.mob = mob;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getFirstName()
{
return firstName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public String getLastName()
{
return lastName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
}
Advertisement
package decodejava;
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.SequenceGenerator;
import javax.persistence.Table;
@Entity
@Table (name ="MOBILE_NUM")
public class Mobile_Num
{
@Id
@Column(name="MOBILE_ID")
@SequenceGenerator(name = "id_generator", sequenceName = "id_seq")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_generator")
private int id;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
private int mobile_number;
public int getMobile_number()
{
return mobile_number;
}
public void setMobile_number(int mobile_number)
{
this.mobile_number = mobile_number;
}
}
package decodejava;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Hiber
{
public static void main(String... ar)
{
Configuration config = new Configuration();
SessionFactory sf = config.configure().buildSessionFactory();
Session session = sf.openSession();
//Creating the first User details
User_Details1 user1 = new User_Details1();
user1.setFirstName("First");
user1.setLastName("User");
//Creating the contact info for the user
Mobile_Num mob1 = new Mobile_Num();
mob1.setMobile_number(1111111);
//Creating the contact info for the user
Mobile_Num mob2 = new Mobile_Num();
mob2.setMobile_number(2222222);
List<Mobile_Num> collection = new ArrayList<Mobile_Num>();
collection.add(mob1);
collection.add(mob2);
//Setting the collection of mobiles to a user
user1.setMob(collection);
session.beginTransaction();
session.save(user1); //Saving the first User_Details object
session.getTransaction().commit();
session.close();
//Retrieve and displaying the user details.a
session = sf.openSession();
session.beginTransaction();
user1 = (User_Details1)session.get(User_Details1.class,1);
System.out.println("Retrieving the saved objects");
//Retrieving details of the first user.
System.out.println("First User");
System.out.println("User Id : " + user1.getId());
System.out.println("First Name : " + user1.getFirstName());
System.out.println("Last Name : " +user1.getLastName());
collection = (List<Mobile_Num>) user1.getMob();
mob1 = collection.get(0);
System.out.println("Mobile id : " + mob1.getId());
System.out.println("Mobile number : " + mob1.getMobile_number());
mob2 = collection.get(1);
System.out.println("Mobile id : " + mob1.getId());
System.out.println("Mobile number : " + mob1.getMobile_number());
}
}
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
<property name="connection.username">scott</property>
<property name="connection.password">tiger</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<!-- Names the mapping entity class -->
<mapping class ="decodejava.User_Details"/>
<mapping class ="decodejava.Mobile_Num"/>
</session-factory>
</hibernate-configuration>
Hibernate: create sequence hibernate_sequence start with 1 increment by 1
Hibernate: create sequence id_seq start with 1 increment by 1
Hibernate: create table MOBILE_NUM (MOBILE_ID number(10,0) not null, mobile_number number(10,0) not null, USER_ID number(10,0), primary key (MOBILE_ID))
Hibernate: create table USER_DETAILS (USER_ID number(10,0) not null, First_Name varchar2(255 char), Last_Name varchar2(255 char), primary key (USER_ID))
Hibernate: alter table MOBILE_NUM add constraint FKq26cahdr6ogqve5cli5lvy09o foreign key (USER_ID) references USER_DETAILS
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select id_seq.nextval from dual
Hibernate: select id_seq.nextval from dual
Hibernate: insert into USER_DETAILS (First_Name, Last_Name, USER_ID) values (?, ?, ?)
Hibernate: insert into MOBILE_NUM (mobile_number, MOBILE_ID) values (?, ?)
Hibernate: insert into MOBILE_NUM (mobile_number, MOBILE_ID) values (?, ?)
Hibernate: update MOBILE_NUM set USER_ID=? where MOBILE_ID=?
Hibernate: update MOBILE_NUM set USER_ID=? where MOBILE_ID=?
Hibernate: select user_detai0_.USER_ID as USER_ID1_1_0_, user_detai0_.First_Name as First_Name2_1_0_, user_detai0_.Last_Name as Last_Name3_1_0_ from USER_DETAILS user_detai0_ where user_detai0_.USER_ID=?
Retrieving the saved objects
First User
User Id : 1
First Name : First
Last Name : User
Hibernate: select mob0_.USER_ID as USER_ID3_0_0_, mob0_.MOBILE_ID as MOBILE_ID1_0_0_, mob0_.MOBILE_ID as MOBILE_ID1_0_1_, mob0_.mobile_number as mobile_number2_0_1_ from MOBILE_NUM mob0_ where mob0_.USER_ID=?
Mobile id : 1
Mobile number : 1111111
Mobile id : 2
Mobile number : 2222222
select * from USER_DETAILS;
select * from MOBILE_NUM;
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement