Tuesday, July 21, 2015

Hibernate 4 one to one mapping with MySql

This is a basic working example of OneToOne Mapping with Hibernate 4 and MySQL.First you have to create a database named test on MySQL and start the thing. Then give your MySQL root user name and password to ,

 <property name="hibernate.connection.username">root</property> //user Name
    <property name="hibernate.connection.password">root</property> //password
in  hibernate.cfg.xml file.

And then prepare following files.

hibernate.cfg.xml

<?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>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">root</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="show_sql">true</property>
    <property name="hbm2ddl.auto">create</property>
    <mapping class="com.dilan.model.User" />
    <mapping class="com.dilan.oneToOne.Customer" />
    <mapping class="com.dilan.oneToOne.CustomerDeatail" />
</session-factory>
</hibernate-configuration>

Customer.java

package com.dilan.oneToOne;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name="Customer")
public class Customer {
@Id
@GeneratedValue
@Column(name="CUSTOMER_ID")
private int customerId;
@Column(name="FIRST_NAME")
private String customerFisrName;
@Column(name="LAST_NAME")
private String customerLastName;
@OneToOne(mappedBy="customer", cascade=CascadeType.ALL)
private CustomerDeatail customerDeatail;
public Customer(){}
public Customer(String customerFisrName,
String customerLastName, CustomerDeatail customerDeatail) {
super();
this.customerFisrName = customerFisrName;
this.customerLastName = customerLastName;
this.customerDeatail = customerDeatail;
}
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public String getCustomerFisrName() {
return customerFisrName;
}
public void setCustomerFisrName(String customerFisrName) {
this.customerFisrName = customerFisrName;
}
public String getCustomerLastName() {
return customerLastName;
}
public void setCustomerLastName(String customerLastName) {
this.customerLastName = customerLastName;
}
public CustomerDeatail getCustomerDeatail() {
return customerDeatail;
}
public void setCustomerDeatail(CustomerDeatail customerDeatail) {
this.customerDeatail = customerDeatail;
}

}

CustomerDeatail.java

package com.dilan.oneToOne;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;
@Entity
@Table(name="CUSTOMER_DETAIL")
public class CustomerDeatail {
@GenericGenerator(name="gen", strategy = "foreign",
parameters = @Parameter(name = "property", value = "customer"))
@Id
@Column(name="CUSTOMER_ID")
@GeneratedValue(generator="gen")
private int customerId;
@Column(name="HEIGHT")
private int height;
@Column(name="WEIGHT")
private int weight;
@OneToOne
@PrimaryKeyJoinColumn
private Customer customer;
public CustomerDeatail(){}
public CustomerDeatail( int height, int weight,
Customer customer) {
super();
this.height = height;
this.weight = weight;
this.customer = customer;
}
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}

}

App.java

package com.dilan.app;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

import com.dilan.model.User;
import com.dilan.oneToOne.Customer;
import com.dilan.oneToOne.CustomerDeatail;

public class App {

public static void main(String[] args) {
     

        Configuration configuration = new Configuration().configure();

        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
        SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
       
        Session session = sessionFactory.openSession();
       
        session.beginTransaction();
           
     
        CustomerDeatail customerDeatail = new CustomerDeatail();
        customerDeatail.setHeight(163);
        customerDeatail.setWeight(75);
        Customer customer = new Customer("Dilan","priyankara",customerDeatail);
        customerDeatail.setCustomer(customer);
       
        session.save(customer);
        session.getTransaction().commit();
       
        System.out.println("Done");

}

}

Run the App.java file and it will create the tables and insert data as OneToOne mapping.




No comments:

Post a Comment