Hibernate Many to Many Annotation Mapping
Relationship
Diagram
Project
Structure
File Name
|
Use
|
Language.java
|
Pojo
class with mapping for Language.
|
Person.java
|
Pojo
class with mapping for Person.
|
hibernate.cfg.xml
|
Hibernate
Configuration File.
|
Main.java
|
It will
control the overall process.
|
Language.java
package manyToMany.annotation;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="LANGUAGE")
public class Language
{
@Id
@Column(name = "LANGUAGE_ID")
@GeneratedValue
private long langId;
@Column(name = "LANGUAGE_NAME")
private String langName;
public Language() { } //no argument constructor
public Language(String langName) {
this.langName = langName;
}
public long getLangId() {
return langId;
}
public void setLangId(long langId) {
this.langId = langId;
}
public String getLangName() {
return langName;
}
public void setLangName(String langName) {
this.langName = langName;
}
}
Person.java
package manyToMany.annotation;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
@Entity
@Table(name="PERSON")
public class Person
{
@Id
@Column(name = "PERSON_ID")
@GeneratedValue
private long personId;
@Column(name = "PERSON_NAME")
private String personName;
@ManyToMany(cascade = {CascadeType.ALL})
@JoinTable(name = "PERSON_LANGUAGE",
joinColumns = {@JoinColumn(name="PERSON_ID")},
inverseJoinColumns={@JoinColumn(name="LANGUAGE_ID")})
private Set<Language> languages = new HashSet<Language>(0);
public Person() {} //no argument constructor
public long getPersonId() {
return personId;
}
public void setPersonId(long personId) {
this.personId = personId;
}
public String getPersonName() {
return personName;
}
public void setPersonName(String personName) {
this.personName = personName;
}
public Set<Language> getLanguages() {
return languages;
}
public void setLanguages(Set<Language> languages) {
this.languages = languages;
}
}
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/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/mydb</property>
<property name="hibernate.connection.username">root</property>
<property name="connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="show_sql">true</property>
<mapping class="manyToMany.annotation.Person"/>
<mapping class="manyToMany.annotation.Language"/>
</session-factory>
</hibernate-configuration>
Main.java
package manyToMany.simple;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Main
{
public static void
main(String[] args)
{
Configuration config
= new
Configuration().configure("manyToMany\\simple\\hibernate.cfg.xml");
SessionFactory
myFactory = config.buildSessionFactory();
Session session =
myFactory.openSession();
Transaction
transaction = null;
try {
transaction =
session.beginTransaction();
Set<Language>
languages = new HashSet<Language>();
languages.add(new
Language("English"));
languages.add(new
Language("Hindi"));
Person person = new
Person();
person.setPersonName("Hemant
Kumar");
person.setLanguages(languages);
session.save(person);
transaction.commit();
}
catch(HibernateException
e) {
transaction.rollback();
e.printStackTrace();
}
finally {
session.close();
}
}
}


Comments
Post a Comment