============
package mypack;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name="address")
public class Address
{
@Id
@Column(name="addrId")
private int addrId;
@Column(name="city",length=10)
private String city;
@ManyToMany(targetEntity=Employee.class,mappedBy="addresses")
private Set employees;
public Set getEmployees() {
return employees;
}
public void setEmployees(Set employees) {
this.employees = employees;
}
public int getAddrId() {
return addrId;
}
public void setAddrId(int addrId) {
this.addrId = addrId;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
Employee.java
=============
package mypack;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name="Employee")
public class Employee
{
@Id
@Column(name="empId")
private int empId;
@Column(name="empname",length=10)
private String name;
@ManyToMany(targetEntity=Address.class,cascade=CascadeType.ALL)
@JoinTable(name="emp_addr",joinColumns=@JoinColumn(name="empId_fk",referencedColumnName="empId"),inverseJoinColumns=@JoinColumn(name="addrId_fk",referencedColumnName="addrId"))
private Set addresses;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set getAddresses() {
return addresses;
}
public void setAddresses(Set addresses) {
this.addresses = addresses;
}
}
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.dialect">org.hibernate.dialect.DerbyDialect</property>
<property name="hibernate.connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
<property name="hibernate.connection.url">jdbc:derby://localhost:1527/mydata</property>
<property name="hibernate.connection.username">app</property>
<property name="hibernate.connection.password">app</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="mypack.Address"></mapping>
<mapping class="mypack.Employee"></mapping>
</session-factory>
</hibernate-configuration>
OurLogic.java
=============
package mypack;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
public class OurLogic {
public static void main(String args[])
{
//SessionFactory factory = new Configuration().configure().buildSessionFactory();
SessionFactory factory = new AnnotationConfiguration().configure().buildSessionFactory();
Session ses = factory.openSession();
Employee emp1 = new Employee();
emp1.setEmpId(1);
emp1.setName("Hari");
Employee emp2=new Employee();
emp2.setEmpId(2);
emp2.setName("Krishna");
Address addr1=new Address();
Address addr2 = new Address();
addr1.setAddrId(101);
addr1.setCity("Bangalore");
addr2.setAddrId(102);
addr2.setCity("Chennai");
Set s =new HashSet();
s.add(addr1);
s.add(addr2);
emp1.setAddresses(s);
emp2.setAddresses(s);
Transaction tx = ses.beginTransaction();
ses.save(emp1);
ses.save(emp2);
tx.commit();
System.out.println("Man to Many using annotations been done...!!!!!");
ses.close();
}
}
No comments:
Post a Comment