Advertisement
package decodejava;
import java.util.Map;
public class Companies
{
private int id;
private String companyName;
private Map<String,String> employeeNames = new HashMap<String,String>();
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getCompanyName()
{
return companyName;
}
public void setCompanyName(String companyName)
{
this.companyName = companyName;
}
public Map<String, String> getEmployeeNames()
{
return employeeNames;
}
public void setEmployeeNames(Map<String, String> employeeNames)
{
this.employeeNames = employeeNames;
}
}
package decodejava;
import java.util.HashMap;
import java.util.Set;
import java.util.Map;
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 company
Companies company1 = new Companies();
company1.setCompanyName("Company1");
//Creating the second company
Companies company2 = new Companies();
company2.setCompanyName("Company2");
Map<String,String> collection1 = new HashMap<String,String>();
collection1.put("Hugh","Grant");
collection1.put("Anuj","Sobti");
collection1.put("James", "Stewart");
//Adding a duplicate value to the Set, but this won't be added
//collection.add(mob2);
//Setting the collection of employee names to the first company
company1.setEmployeeNames(collection1);
Map<String,String> collection2 = new HashMap<String,String>();
collection2.put("Rakesh","Khanna");
collection2.put("Sean","Webber");
collection2.put("Adam", "Shot");
//Setting the collection of employee names to the second company
company2.setEmployeeNames(collection2);
session.beginTransaction();
session.save(company1); //Saving the object of first Company
session.save(company2); //Saving the object of second Company
session.getTransaction().commit();
//Creating a new Session to retrieve the stored objects.
session= sf.openSession();
session.beginTransaction();
//Retrieve and displaying the employee details
company1 = (Companies)session.get(Companies.class,1);
company2 = (Companies)session.get(Companies.class,2);
collection1 = company1.getEmployeeNames();
Set<Map.Entry<String,String>> set1 = collection1.entrySet();
for(Map.Entry<String,String> mapE : set1)
{
System.out.print("First Name : " + mapE.getKey());
System.out.println(", Second Name : "+ mapE.getValue());
}
collection2 = company2.getEmployeeNames();
Set<Map.Entry<String,String>> set2 = collection2.entrySet();
for(Map.Entry<String,String> mapE : set2)
{
System.out.print("First Name : " + mapE.getKey());
System.out.println(", Second Name : "+ mapE.getValue());
}
session.getTransaction().commit();
session.close();
}
}
Advertisement
<?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.Companies" table = "COMPANIES">
<id name = "id" column = "Company_ID" type = "int">
<property name = "companyName" column = "Company_Name" type = "string"/>
<set name ="employeeNames" table="COMPANY_EMPLOYEES" cascade="all">
<key column="Company_ID"/>
<index column="First_Name" type="string"/>
<element column="Second_Name" type="string"/>
</set>
</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">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>
<!--Naming the mapping resource file -->
<mapping resource ="companies.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Hibernate: create table COMPANIES (COMPANY_ID number(10,0) not null, Company_Name varchar2(255 char), primary key (COMPANY_ID))
Hibernate: create table COMPANY_EMPLOYEES (COMPANY_ID number(10,0) not null, Employee_Names varchar2(255 char))
Hibernate: alter table COMPANY_EMPLOYEES add constraint FKf3ir1cup274vb9nd2jrwj3hea foreign key (COMPANY_ID) references COMPANIES
Hibernate: select max(COMPANY_ID) from COMPANIES
Hibernate: insert into COMPANIES (Company_Name, COMPANY_ID) values (?, ?)
Hibernate: insert into COMPANIES (Company_Name, COMPANY_ID) values (?, ?)
Hibernate: insert into COMPANY_EMPLOYEES (COMPANY_ID, Employee_Names) values (?, ?)
Hibernate: insert into COMPANY_EMPLOYEES (COMPANY_ID, Employee_Names) values (?, ?)
Hibernate: insert into COMPANY_EMPLOYEES (COMPANY_ID, Employee_Names) values (?, ?)
Hibernate: insert into COMPANY_EMPLOYEES (COMPANY_ID, Employee_Names) values (?, ?)
Hibernate: insert into COMPANY_EMPLOYEES (COMPANY_ID, Employee_Names) values (?, ?)
Hibernate: insert into COMPANY_EMPLOYEES (COMPANY_ID, Employee_Names) values (?, ?)
Hibernate: select companies0_.COMPANY_ID as COMPANY_ID1_0_0_, companies0_.Company_Name as Company_Name2_0_0_ from COMPANIES companies0_ where companies0_.COMPANY_ID=?
Hibernate: select companies0_.COMPANY_ID as COMPANY_ID1_0_0_, companies0_.Company_Name as Company_Name2_0_0_ from COMPANIES companies0_ where companies0_.COMPANY_ID=?
Retrieving the saved objects
Name of the first company : Company1
Hibernate: select employeena0_.COMPANY_ID as COMPANY_ID1_1_0_, employeena0_.Employee_Names as Employee_Names2_1_0_ from COMPANY_EMPLOYEES employeena0_ where employeena0_.COMPANY_ID=? order by employeena0_.Employee_Names
[Amit Sobti, Hugh Grant, James Stewart]
Retrieving the saved objects
Name of the second company : Company2
Hibernate: select employeena0_.COMPANY_ID as COMPANY_ID1_1_0_, employeena0_.Employee_Names as Employee_Names2_1_0_ from COMPANY_EMPLOYEES employeena0_ where employeena0_.COMPANY_ID=? order by employeena0_.Employee_Names
[Adam Shot, Rakesh Khanna, Sean Webber]
select * from COMPANIES;
select * from COMPANY_EMPLOYEES;
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement