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.ManyToMany;
import javax.persistence.Table;
@Entity
@Table (name ="USER_DETAILS")
public class User_Details {
@Id
@GeneratedValue
@Column (name = "USER_ID")
private int id;
@Column (name = "First_Name")
private String firstName;
@Column (name = "Last_Name")
private String lastName;
@ManyToMany(cascade = {CascadeType.ALL}, mappedBy ="userCollection")
private List mob= new ArrayList();
public List getMob()
{
return mob;
}
public void setMob(List 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 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.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
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;
private int mobile_number;
@ManyToMany(cascade=CascadeType.ALL)
@JoinTable(name="USER_MOBILE", joinColumns = @JoinColumn(name = "USER_ID"),
inverseJoinColumns = @JoinColumn(name="MOBILE_ID"))
private List<User_Details> userCollection = new ArrayList<User_Details>();
public List<User_Details> getUserCollection() {
return userCollection;
}
public void setUserCollection(List<User_Details> userCollection)
{
this.userCollection = userCollection;
}
public int getId() {
return id;
}
public void setId(int id)
{
this.id = id;
}
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.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 second User details
User_Details user2 = new User_Details();
user2.setFirstName("Second");
user2.setLastName("User");
//Adding the users to a collection
List userCollection = new ArrayList();
userCollection.add(user1);
userCollection.add(user2);
//Creating the contact info for the user
Mobile_Num mob1 = new Mobile_Num();
mob1.setMobile_number(1111111);
//Setting the collection of user to the first mobile
mob1.setUserCollection(userCollection);
//Creating the contact info for the user
Mobile_Num mob2 = new Mobile_Num();
mob2.setMobile_number(2222222);
//Setting the collection of user to the second mobile
mob2.setUserCollection(userCollection);
List<Mobile_Num> mobileCollection = new ArrayList<Mobile_Num>();
mobileCollection.add(mob1);
mobileCollection.add(mob2);
//Setting the collection of mobiles to a user
user1.setMob(mobileCollection);
user2.setMob(mobileCollection);
//Saving the first User_Details object in database and its associated
//mobile information is automatically saved plus the second user associated with it.
session.beginTransaction();
session.save(user1);
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");
mobileCollection = (List<Mobile_Num>) user1.getMob();
mob1 = mobileCollection.get(0);
//Accessing the user from the first mobile object to display mobile's association with the user and bidirectional cccess many-to-many mapping
user1 = mob1.getUserCollection().get(0);
System.out.println("Information about the user of mobile1");
System.out.println("User Id : " + mob1.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 and bidirectional access and many-to-many mapping
user2 = mob1.getUserCollection().get(1);
System.out.println("Information about the user of mobile2 ");
System.out.println("User Id : " + mob2.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());
session.getTransaction().commit();
session.close();
}
}
<?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 table MOBILE_NUM (MOBILE_ID number(10,0) not null, Mobile_Number 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: create table USER_MOBILE (USER_ID number(10,0), MOBILE_ID number(10,0) not null)
Hibernate: alter table USER_MOBILE add constraint FKga6skjoe36qus060f10uikex0 foreign key (MOBILE_ID) references MOBILE_NUM
Hibernate: alter table USER_MOBILE add constraint FKq5m71fs4yt5mlb5s6p9t30kye foreign key (USER_ID) references USER_DETAILS
Hibernate: select max(USER_ID) from USER_DETAILS
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.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 USER_DETAILS (First_Name, Last_Name, USER_ID) values (?, ?, ?)
Hibernate: insert into MOBILE_NUM (Mobile_Number, MOBILE_ID) values (?, ?)
Hibernate: insert into USER_MOBILE (USER_ID, MOBILE_ID) values (?, ?)
Hibernate: insert into USER_MOBILE (USER_ID, MOBILE_ID) values (?, ?)
Hibernate: insert into USER_MOBILE (USER_ID, MOBILE_ID) values (?, ?)
Hibernate: insert into USER_MOBILE (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_ID1_2_0_, mob0_.MOBILE_ID as MOBILE_ID2_2_0_, mobile_num1_.MOBILE_ID as MOBILE_ID1_0_1_, mobile_num1_.Mobile_Number as Mobile_Number2_0_1_ from USER_MOBILE mob0_ inner join MOBILE_NUM mobile_num1_ on mob0_.MOBILE_ID=mobile_num1_.MOBILE_ID where mob0_.USER_ID=?
Hibernate: select usercollec0_.MOBILE_ID as MOBILE_ID2_2_0_, usercollec0_.USER_ID as USER_ID1_2_0_, user_detai1_.USER_ID as USER_ID1_1_1_, user_detai1_.First_Name as First_Name2_1_1_, user_detai1_.Last_Name as Last_Name3_1_1_ from USER_MOBILE usercollec0_ inner join USER_DETAILS user_detai1_ on usercollec0_.USER_ID=user_detai1_.USER_ID where usercollec0_.MOBILE_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 : 2
First Name : First
Last Name : User
Mobile id : 2
Mobile number : 2222222
select * from USER_DETAILS;
select * from MOBILE_NUM;
select * from USER_MOBILE;
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement