One to One Table Mapping

HibernateRepository.java
public class HibernateUtil {
private static SessionFactory sessionFactory;
private HibernateUtil(){}
static {
StandardServiceRegistry standardRegistry =
       new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
Metadata metaData =
        new MetadataSources(standardRegistry).getMetadataBuilder().build();
sessionFactory = metaData.getSessionFactoryBuilder().build();
}
public static Session getSession() {
return sessionFactory.openSession();
}
}

hibernate.cfg.xml DB setting
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle8iDialect</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.username">user</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
         </session-factory>
</hibernate-configuration>

Test Class
public class Client1 {
public static void main(String[] args) {
Session session = HibernateUtil.getSession();

Address address = new Address();
//address.setAddressId(1001);
address.setHouseNo("404/16/04");
address.setStreetName("CHM Road");

Student student = new Student();
//student.setId(1001);
student.setFirstName("Ramu");
student.setLastName("Singh");
student.setAddress(address);

session.beginTransaction();
//session.save(address);
session.save(student);
session.getTransaction().commit();

System.out.println("One to one uni directional is successful");
}
}

Uni-Directional One To One Mapping

Examples Classes
public class Student {
private int id;
private String firstName;
private String lastName;
private Address address;
        // setters......getters......
}

public class Address {
private int addressId;
private String houseNo;
private String streetName;
        //setters....getters
}

1. XML Based Mapping

student.hbm.xml
<hibernate-mapping
package="com.practice.one2one.mapping.unidirectional.xml.approach"
auto-import="false">

<class name="Student" table="STUDENT1">
<id name="id" column="STUDENT_ID">
<generator class="foreign">
<param name="property">address</param>
</generator>
</id>
<property name="firstName" />
<property name="lastName" />
<one-to-one name="address" class="Address"
constrained="true" cascade="all" />
</class>
</hibernate-mapping>

address.hbm.xml
<hibernate-mapping
package="com.practice.one2one.mapping.unidirectional.xml.approach"
auto-import="false">

<class name="Address" table="ADDRESS1">
<id name="addressId" column="ADDRESS_ID" />
<property name="houseNo" />
<property name="streetName" />
</class>
</hibernate-mapping>

hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<mapping resource="com/practice/one2one/mapping/unidirectional/xml/approach/student.hbm.xml" />
<mapping resource="com/practice/one2one/mapping/unidirectional/xml/approach/address.hbm.xml" />
</session-factory>
</hibernate-configuration>

Test Class
public class Client1 {
public static void main(String[] args) {
Session session = HibernateUtil.getSession();

Address address = new Address();
//address.setAddressId(1001);
address.setHouseNo("404/16/04");
address.setStreetName("CHM Road");

Student student = new Student();
//student.setId(1001);
student.setFirstName("Ramu");
student.setLastName("Singh");
student.setAddress(address);

session.beginTransaction();
//session.save(address);
session.save(student);
session.getTransaction().commit();

System.out.println("One to one uni directional is successful");
}
}

2. Annotation Based Mapping
@Entity(name = "com.practice.one2one.mapping.unidirectional.annotation.approach.Student")
@Table(name = "STUDENT2")
public class Student {
@Id
@Column(name = "STUDENT_ID")
private int id;
private String firstName;
private String lastName;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "HOME_ADDRESS_ID")
private Address address;
        //setters....getters
}

@Entity(name="com.practice.one2one.mapping.unidirectional.annotation.approach.Address")
@Table(name = "ADDRESS2")
public class Address {
@Id
@Column(name = "ADDRESS_ID")
private int addressId;
private String houseNo;
private String streetName;
//setters....getters
}

Bi-Directional One To One Mapping

public class Student {
private int id;
private String firstName;
private String lastName;
private Address address;
        //setters...getters
}

public class Address {
private int addressId;
private String houseNo;
private String streetName;
private Student student;
        //setters...getters
}

1. XML Based Mapping

student.hbm.xml
<hibernate-mapping
package="com.practice.one2one.mapping.bidirectional.xml.approach"
auto-import="false">
<class name="Student" table="STUDENT3">
<id name="id" column="STUDENT_ID">
<generator class="native"></generator><!-- it takes the sequence in Oracle,
if no sequence name is provided then -->
</id>
<property name="firstName" />
<property name="lastName" />
<one-to-one name="address" class="Address" cascade="all" />
<!-- Cascade having the values……. none (default) save update save-update
delete all all-delete-orphan -->
</class>
</hibernate-mapping>

address.hbm.xml
<hibernate-mapping
package="com.practice.one2one.mapping.bidirectional.xml.approach"
auto-import="false">
<class name="Address" table="ADDRESS3">
<id name="addressId" column="ADDRESS_ID">
<generator class="foreign">
<param name="property">student</param><!-- we have selected the generator
as foreign, so that it uses the PRIMARY KEY of the EMPLOYEE table. -->
</generator>
</id>
<property name="houseNo" />
<property name="streetName" />
<one-to-one name="student" class="Student"
constrained="true" />
</class>
</hibernate-mapping>

2. Annotation Based Mapping

@Entity(name = "com.practice.one2one.mapping.bidirectional.annotation.approach.Student")
@Table(name = "STUDENT4")
public class Student {
@Id
@GeneratedValue // Here SEQUENCE Strategy will be used as this the default strategy for Oracle
@Column(name = "STUDENT_ID")
private int id;
private String firstName;
private String lastName;

@OneToOne(mappedBy = "student")
// mappedBy = "student", this indicates that this side is not the owner of the
// relationship.
private Address address;
        //setters...getters
}

@Entity(name = "com.practice.one2one.mapping.bidirectional.annotation.approach.Address")
@Table(name = "ADDRESS4")
public class Address {
@Id
@GeneratedValue // Here SEQUENCE Strategy will be used as this the default strategy for Oracle
@Column(name = "ADDRESS_ID")
private int addressId;
private String houseNo;
private String streetName;

@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "S_ID") // @JoinColumn annotation indicates that this entity will act as the owner of the relationship
private Student student;
}

Comments

Popular posts from this blog

SQL basic interview question

gsutil Vs Storage Transfer Service Vs Transfer Appliance