Advertisement
package decodejava;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table (name ="USER_INFO")
public class UserInfo {
@Id
@GeneratedValue
@Column (name = "ID")
private int id;
@Column (name = "First_Name")
private String firstName;
@Column (name = "Last_Name")
private String lastName;
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;
}
}
package decodejava;
import java.util.Date;
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;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
@Entity
@Table (name ="Backup_Database")
public class BackupDatabase
{
@Id
@Column(name="ID")
@SequenceGenerator(name = "id_generator", sequenceName = "id_seq")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_generator")
private int id;
@Column (name = "First_Name")
private String firstName;
@Column (name = "Last_Name")
private String lastName;
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.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
public class Hiber
{
public static void main(String[] args)
{
SessionFactory sf= new Configuration().configure().buildSessionFactory();
Session session = sf.openSession();
//Creating the backup database for the first user
BackupDatabase backupUser1 = new BackupDatabase();
backupUser1.setFirstName("Fist");
backupUser1.setLastName("User");
//Creating the backup database for the second user
BackupDatabase backupUser2 = new BackupDatabase();
backupUser2.setFirstName("Second");
backupUser2.setLastName("User");
//Creating the backup database for the third user
BackupDatabase backupUser3 = new BackupDatabase();
backupUser3.setFirstName("Third");
backupUser3.setLastName("User");
//Creating the backup database for the fourth user
BackupDatabase backupUser4 = new BackupDatabase();
backupUser2.setFirstName("Fourth");
backupUser2.setLastName("User");
//Creating the first object of UserInfo
UserInfo ob1 = new UserInfo();
ob1.setFirstName("First");
ob1.setLastName("User");
//Creating the second object of UserInfo
UserInfo ob2 = new UserInfo();
ob2.setFirstName("Second");
ob2.setLastName("User");
session.beginTransaction();
session.save(backupUser1); //Saving the first BackupDatabase object
session.save(backupUser2); //Saving the second BackupDatabase object
session.save(backupUser3); //Saving the third BackupDatabase object
session.save(backupUser4); //Saving the fourth BackupDatabase object
session.save(ob1); //Saving the first UserInfoobject
session.save(ob2); //Saving the second UserInfoobject
session.getTransaction().commit();
//Retrieving all the records of UserInfo
Query query1 = session.createQuery("from UserInfo");
List<UserInfo> collection1 = query1.getResultList();
System.out.println("Retrieving and displaying details of all the users");
for(UserInfo object : collection1)
{
System.out.println("Id : " + object.getId());
System.out.println("First Name : " + object.getFirstName());
System.out.println("Last Name : " + object.getLastName());
System.out.println();
}
//Inserting multiple user record of BackupDatabase, between 'id' 3 and 4, into the UserInfo mapped database table.
session = sf.openSession();
session.beginTransaction();
Query query2 = session.createQuery("insert into UserInfo(id, firstName, lastName) select id, firstName, lastName from BackupDatabase where id between 3 and 4");
System.out.println("Inserting a new record");
query2.executeUpdate();
session.getTransaction().commit();
//Retrieve and displaying all the updated records of UserInfo
session = sf.openSession();
session.beginTransaction();
Query query2 = session.createQuery("from UserInfo");
List<UserInfo> collection2 = query2.getResultList();
System.out.println("Retrieving and displaying the updated details of users");
for(UserInfo object : collection2)
{
System.out.println("Id : " + object.getId());
System.out.println("First Name : " + object.getFirstName());
System.out.println("Last Name : " + object.getLastName());
System.out.println();
}
session.getTransaction().commit();
//closing the session
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.UserInfo"/>
<mapping class ="decodejava.BackupDatabase"/>
</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 50
Hibernate: create table Backup_Database (ID number(10,0) not null, First_Name varchar2(255 char), Last_Name varchar2(255 char), primary key (ID))
Hibernate: create table USER_INFO (ID number(10,0) not null, First_Name varchar2(255 char), Last_Name varchar2(255 char), primary key (ID))
Hibernate: select hibernate_sequence.nextval from dual
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_INFO (First_Name, Last_Name, ID) values (?, ?, ?)
Hibernate: insert into USER_INFO (First_Name, Last_Name, ID) values (?, ?, ?)
Hibernate: insert into Backup_Database (First_Name, Last_Name, ID) values (?, ?, ?)
Hibernate: insert into Backup_Database (First_Name, Last_Name, ID) values (?, ?, ?)
Hibernate: insert into Backup_Database (First_Name, Last_Name, ID) values (?, ?, ?)
Hibernate: insert into Backup_Database (First_Name, Last_Name, ID) values (?, ?, ?)
Hibernate: select userinfo0_.ID as ID1_1_, userinfo0_.First_Name as First_Name2_1_, userinfo0_.Last_Name as Last_Name3_1_ from USER_INFO userinfo0_
Retrieving and displaying details of all the users in User_Details
Id : 1
First Name : First
Last Name : User
Id : 2
First Name : Second
Last Name : User
Inserting a new record in User_Details from Backup_Database
Hibernate: insert into USER_INFO ( ID, First_Name, Last_Name ) select backupdata0_.ID as col_0_0_, backupdata0_.First_Name as col_1_0_, backupdata0_.Last_Name as col_2_0_ from Backup_Database backupdata0_ where backupdata0_.ID between 3 and 4
Hibernate: select userinfo0_.ID as ID1_1_, userinfo0_.First_Name as First_Name2_1_, userinfo0_.Last_Name as Last_Name3_1_ from USER_INFO userinfo0_
Retrieving and displaying the updated content User_Details
Id : 1
First Name : First
Last Name : User
Id : 2
First Name : Second
Last Name : User
Id : 3
First Name : Third
Last Name : User
Id : 4
First Name : Fourth
Last Name : User
select * from User_Info;
select * from Backup_Database;
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement