Advertisement
cascade = {CascadeType.ALL}
package decodejava;
import java.util.ArrayList;
import java.util.List;
public class User_Details
{
private int id;
private String firstName;
private String lastName;
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;
public class Mobile_Num
{
private int id;
private int mobile_number;
private User_Details user;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public User_Details getUser()
{
return user;
}
public void setUser(User_Details user)
{
this.user = user;
}
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_Details user1 = new User_Details();
user1.setFirstName("First");
user1.setLastName("User");
//Creating the first contact info for the user
Mobile_Num mob1 = new Mobile_Num();
mob1.setMobile_number(1111111);
//Setting the user with the second mobile number
mob1.setUser(user1);
//Creating the second contact info for the user
Mobile_Num mob2 = new Mobile_Num();
mob2.setMobile_number(2222222);
//Setting the user with the second mobile number
mob2.setUser(user1);
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
session = sf.openSession();
session.beginTransaction();
user1 = (User_Details)session.get(User_Details.class,1);
System.out.println("Retrieving the saved objects");
collection = (List<Mobile_Num>) user1.getMob();
mob1 = collection.get(0);
//Accessing the user from the first mobile object to display mobile's association with the user.
user1 = mob1.getUser();
System.out.println("Information about the user of mobile1");
System.out.println("User Id : " + user1.getId());
System.out.println("First Name : " + user1.getFirstName());
System.out.println("Last Name : " +user1.getLastName());
System.out.println("Mobile id : " + mob1.getId());
System.out.println("Mobile number : " + mob1.getMobile_number());
//Accessing the user from the second mobile object to display mobile's association with the user.
mob2 = collection.get(1);
user1 = mob2.getUser();
System.out.println("Information about the user of mobile2 ");
System.out.println("User Id : " + user1.getId());
System.out.println("First Name : " + user1.getFirstName());
System.out.println("Last Name : " +user1.getLastName());
System.out.println("Mobile id : " + mob2.getId());
System.out.println("Mobile number : " + mob2.getMobile_number());
}
}
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name = "decodejava.User_Details1" table = "USER_DETAILS">
<id name = "id" column = "USER_ID" type = "int">
<generator class="increment"/>
</id>
<property name = "firstName" column = "First_Name" type = "string"/>
<property name = "lastName" column = "Last_Name" type = "string"/>
<list name ="mob" cascade="all">
<key> <column name="USER_ID"> </column> </key>
<list-index column="MOB_ORDER" base="1"/>
<one-to-many class="decodejava.Mobile_Num"/>
</list>
</class>
</hibernate-mapping>
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name = "decodejava.Mobile_Num" table = "MOBILE_NUM">
<id name = "id" column = "MOBILE_ID" type = "int">
<generator class="sequence">
</generator>
</id>
<property name = "mobile_number" column = "Mobile_Number" type = "int"/>
</class>
</hibernate-mapping>
<?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">system</property>
<property name="connection.password">promila21</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 FKinkg6o8fm7jwhao8slsjqnaxa 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, user_id, MOBILE_ID) values (?, ?, ?)
Hibernate: insert into MOBILE_NUM (mobile_number, user_id, MOBILE_ID) values (?, ?, ?)
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
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_, mob0_.user_id as user_id3_0_1_ from MOBILE_NUM mob0_ where mob0_.user_id=?
Information about the user of mobile1
User Id : 1
First Name : First
Last Name : User
Mobile id : 1
Mobile number : 1111111
Information about the user of mobile2
User Id : 1
First Name : First
Last Name : User
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