Hibernate One-to-Many and Many-to-One Mapping using Annotation

person
id
name
age
sex
location

client
id
name

person_client
id
person_id
client_id

Person.java

package com.dyutiman.beans;

@Entity
@Table(name = "person")
public class Person {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private long id;

	@OneToMany(mappedBy = "person")
	private List<PersonClient> personClients;

	@Column
	private String name;

	@Column
	private int age;

	@Column
	private String sex;

	@Column
	private String location;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public List<PersonClient> getPersonClients() {
		return personClients;
	}

	public void setPersonClients(List<PersonClient> personClients) {
		this.personClients = personClients;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}
}

Client.java

package com.dyutiman.beans;

@Entity
@Table(name = "client")
public class Client {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private long id;

	@OneToMany(mappedBy = "client")
	private List<PersonClient> pc;

	@Column
	private String name;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public List<PersonClient> getPc() {
		return pc;
	}

	public void setPc(List<PersonClient> pc) {
		this.pc = pc;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

PersonClient.java

package com.dyutiman.beans;

@Entity
@Table(name = "person_client")
public class PersonClient {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;

	@ManyToOne
	@JoinColumn(name = "person_id")
	private Person person;

	@ManyToOne
	@JoinColumn(name = "client_id")
	private Client client;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public Client getClient() {
		return client;
	}

	public void setClient(Client client) {
		this.client = client;
	}
}

Test

public void testLoad() {
	long id = 1;
	Client c = cd.load(id);

	List<PersonClient> pcList = c.getPc();
	for (PersonClient pc : pcList) {
		System.out.println(pc.getPerson().getName());
	}
}

public void testLoad() {
	long id = 1;
	Person p = pd.load(id);

	List<PersonClient> pcList = p.getPersonClients();
	for (PersonClient pc : pcList) {
		System.out.println(pc.getClient().getName());
	}
}
Advertisements

Hibernate Session Generator – HibernateUtil

Class :

public class HibernateUtil {

	private static final SessionFactory sessionFactory;
	static {
		try {
			sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
		}catch(Throwable e){
			throw new ExceptionInInitializerError(e);
		}
	}
	
	public static Session getSession(){
		return sessionFactory.openSession();
	}
}

and the implementation

Session session = HibernateUtil.getSession();

Simple Joining in Hibernate using Annotation

Using Hibernate with Annotation make life simpler.

Lets have 2 tables.
Country:
id bigint AUTO INCREAMENT
name varchar

and
State:
id bigint AUTO INCREAMENT
country_id bigint
name varchar

The relation is so obvious here. Our query will fetch a state with its country name.
The country class will look like

@Entity
@Table(name="country")
pulic class Country {
    
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private Long id;
	    
	@Column
	private String name;
}

and the state class

@Entity
@Table(name="state")
pulic class State {
    
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private Long id;

	@OneToOne
	@JoinColumn(name="country_id")
	private Country country;
	
	@Column
	private String name;
}

Thats it and thats all to define the relation between two tables.

Now when we load any state data, its also includes the country information

State s = (State) session.load(State.class, stateId);
System.out.println("State : " + s.getName());
System.out.println("Country : " + s.getCountry().getName());

and when we use HQL the scenario r3emains the same

State s = (State) session.createQuery("from " + State.class.getName() + " where id = " + stateId);
System.out.println("State : " + s.getName());
System.out.println("Country : " + s.getCountry().getName());