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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: