Ex6:
//Hibernate
one-to-one xml mapping
Employee.java
package madhav;
public class Employee {
private int empno;
private String ename;
private PersonalDetails
personalDetails;
public PersonalDetails
getPersonalDetails() {
return personalDetails;
}
public void setPersonalDetails(PersonalDetails
personalDetails) {
this.personalDetails =
personalDetails;
}
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String
getEname() {
return ename;
}
public void setEname(String
ename) {
this.ename = ename;
}
}
PersonalDetails.java
package madhav;
public class
PersonalDetails{
private int empno;
private String firstname;
private String lastname;
private Employee employee;
public Employee getEmployee()
{
return employee;
}
public void
setEmployee(Employee employee) {
this.employee = employee;
}
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
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;
}
}
employee.bhm.xml
<?xml version="1.0"
encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC
"-//Hibernate/Hibernate
Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="madhav">
<class name="Employee"
table="employee">
<id name="empno"
type="int">
<column name="empno"></column>
<generator class="increment"/>
</id>
<property name="ename">
<column name="ename"
/>
</property>
<one-to-one name="personalDetails"
class="PersonalDetails">
</one-to-one>
</class>
</hibernate-mapping>
personaldetails.hbm.xml
<?xml version="1.0"
encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC
"-//Hibernate/Hibernate
Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="madhav">
<class name="PersonalDetails"
table="personaldetails">
<id name="empno"
type="int" column="empno"
>
<generator class="increment"/>
</id>
<property name="firstname">
<column name="firstname"
/>
</property>
<property name="lastname">
<column name="lastname"/>
</property>
<one-to-one name="employee"
class="Employee">
</one-to-one>
</class>
</hibernate-mapping>
hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration
PUBLIC
"-//Hibernate/Hibernate
Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!--
Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/mysql</property>
<property name="connection.username">madhav</property>
<property name="connection.password">madhav</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="show_sql">true</property>
<mapping resource="employee.hbm.xml"/>
<mapping resource="personaldetails.hbm.xml"/>
</session-factory>
</hibernate-configuration>
TestCase.java
package madhav;
import
org.hibernate.SessionFactory;
import
org.hibernate.classic.Session;
import
org.hibernate.cfg.*;
import org.hibernate.*;
import org.apache.log4j.*;
public class TestCase {
SessionFactory factory;
public void createEmp()
{
Session session=null;
Transaction tx=null;
try
{
session
=factory.openSession();
tx=session.beginTransaction();
PersonalDetails pd=new
PersonalDetails();
Employee e=new Employee();
pd.setFirstname("abc");
pd.setLastname("rao");
pd.setEmployee(e);
e.setEname("madhav");
e.setPersonalDetails(pd);
session.save(e);
session.save(pd);
tx.commit();
System.out.println("record inserted");
}
catch(HibernateException e)
{
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method
stub
System.out.println("first");
TestCase t=new TestCase();
Configuration cfg=new
Configuration();
cfg.configure();
System.out.println("break
2");
t.factory=cfg.buildSessionFactory();
t.createEmp();
System.out.println("execution
over");
}
}
Output-console:
Related Topics:
- hibernate-basic-example(xml-based)
- hibernate-inheritance-tableforclass-hierarchy(xml-based)
- hibernate-inheritance-joinsubclass-(xml-based)
- hibernate-inheritance-tableforconcreteclass(xml-based)
- hibernate-onetoone-relation(xml-based)
- hibernate-onetomany-relation(xml-based)
- hibernate-manytomany-relation(xml-based)
- hibernate-basic-example(Annotation-based)
- hibernate-inheritance-tableforclass-hierarchy(Annotation-based)
- hibernate-inberitance-joinsubclass-(Annotation-based)
- hibernate-inheritance-tableforconcreteclass(Annotation-based)
- hibernate-onetoone-relation(Annotation-based)
- hibernate-onetomany-relation(Annotation-based)
- hibernate-manytomany-relation(Annotation-based)
No comments:
Post a Comment