hibernate onetoone annotation example:
---------------------------------------
Employee .java
----------------
package madhav;
import javax.persistence.Entity;
import javax.persistence.*;
@Entity
@Table(name="employee1")
public class Employee {
@Id
@GeneratedValue
private int empno;
private String ename;
@OneToOne(mappedBy="employee", cascade=CascadeType.ALL)
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;
import javax.persistence.*;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;
@Entity
@Table(name="personaldetails")
public class PersonalDetails{
@Id
@GeneratedValue(generator="ram")
@GenericGenerator(name="ram", strategy="foreign", parameters=@Parameter(name="property", value="employee"))
private int empno;
private String firstname;
private String lastname;
@OneToOne
@PrimaryKeyJoinColumn
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;
}
}
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">root</property>
<property name="connection.password">mysql</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="madhav.Employee" />
<mapping class="madhav.PersonalDetails"/>
</session-factory>
</hibernate-configuration>
TestCase.java
---------------
package madhav;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.classic.Session;
public class TestCase {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
// Create the SessionFactory from hibernate.cfg.xml
SessionFactory sf= new
AnnotationConfiguration().configure().buildSessionFactory();
Session s=sf.openSession();
Transaction tx=s.beginTransaction();
PersonalDetails pd=new PersonalDetails();
pd.setFirstname("abc");
pd.setLastname("rao");
// pd.setEmpno(101);
Employee e=new Employee();
e.setEname("madhav");
// e.setEmpno(101);
e.setPersonalDetails(pd);
pd.setEmployee(e);
System.out.println("before save");
s.save(e);
System.out.println("after save");
tx.commit();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
}
}
}
output from 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