Example of @OneToOne in Hibernate

By Arvind Rai, May 20, 2013
@OneToOne in hibernate do one-to-one mapping. More than one entity can be associated by @OneToOne. One-to-one mapping can be achieved by three ways. 1- Associated entities can share the same primary key, 2- A foreign key, 3- association table. Here we have used shared primary key to achieve one-to-one mapping. For shared primary key we need to use @PrimaryKeyJoinColumn.
The meaning of one-to-one is that one table will be associated with another table by single value. In the example we suppose that a country can have one president and we did one-to-one mapping. Syntax will look like
 @OneToOne(cascade = CascadeType.ALL)
 @PrimaryKeyJoinColumn
 


Country.java
package com.concretepage.persistence;
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;

@Entity
public class Country implements Serializable {
	private static final long serialVersionUID = 1L;
	@Id
	@Column(name="id")
	private int id;
	
	@Column(name="name")
	private String name;
	
	@OneToOne(cascade = CascadeType.ALL)
        @PrimaryKeyJoinColumn
	private President president;
	
	public Country (int id, String name,President president){
		this.id=id;
		this.name=name;
		this.president=president;
	}
    public Country (){}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public President getPresident() {
		return president;
	}
	public void setPresident(President president) {
		this.president = president;
	}
}
 


President.java
package com.concretepage.persistence;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "president")
public class President {
	@Id
	@Column(name = "id")
	private int id;
	@Column(name = "name")
	private String name;
	public President(){	}
	public President(int id, String name){	
		this.id=id;
		this.name=name;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}
 


HibernateUtil.java
package com.concretepage.util;
import java.util.Properties;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import com.concretepage.persistence.Country;
import com.concretepage.persistence.President;
public class HibernateUtil {
	private static final SessionFactory concreteSessionFactory;
		static {
		 try {
				Properties prop= new Properties();
				prop.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/hibernate");
				prop.setProperty("hibernate.connection.username", "root");
				prop.setProperty("hibernate.connection.password", "");
				prop.setProperty("hibernate.hbm2ddl.auto", "update");
				prop.setProperty("show_sql", "true");
				prop.setProperty("dialect", "org.hibernate.dialect.MySQLDialect");
				
				concreteSessionFactory = new AnnotationConfiguration()
			   .addPackage("com.concretepage.persistence")
					   .addProperties(prop)
					   .addAnnotatedClass(Country.class)
					   .addAnnotatedClass(President.class)
					   .buildSessionFactory();
		  } catch (Throwable ex) {
			throw new ExceptionInInitializerError(ex);
		  }
		}
		public static Session getSession()
				throws HibernateException {
			return concreteSessionFactory.openSession();
		}
		
		public static void main(String... args){
			Session session=getSession();
			session.beginTransaction();
			President p= new President(1,"ABC");
			Country country= new Country(1,"India",p);
	    	        session.save(country);
			session.getTransaction().commit();
		}
	}
 
POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us