=============
package mypack;
import java.io.Serializable;
public class Address implements Serializable{
private int addrId;
private String city;
private int foreignkey;
public Address(){
}
public int getForeignkey() {
return foreignkey;
}
public void setForeignkey(int foreignkey) {
this.foreignkey = foreignkey;
}
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;
}
public String toString(){
return addrId+" "+city;
}
}
Employee.java
==============
package mypack;
import java.io.Serializable;
import java.util.Set;
public class Employee implements Serializable{
private int empId;
private String name;
private Set set;
public Employee(){
}
public Set getSet() {
return set;
}
public void setSet(Set set) {
this.set = set;
}
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;
}
}
address.hbm.xml
===============
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="mypack.Address" table="ONETOMANYADDR">
<id name="addrId" ></id>
<property name="city" length="12" ></property>
<property name="foreignkey" insert="false" ></property>
</class>
</hibernate-mapping>
employee.hbm.xml
=================
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="mypack.Employee" table="ONETOMANYEMP" >
<id name="empId" ></id>
<property name="name" ></property>
<set name="set" cascade="all" >
<key column="foreignkey" ></key>
<one-to-many class="mypack.Address" ></one-to-many>
</set>
</class>
</hibernate-mapping>
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/ONETOMANY</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 resource="employee.hbm.xml"/>
<mapping resource="address.hbm.xml"/>
</session-factory>
</hibernate-configuration>
SaveData.java
=====================
package mypack;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class SaveData {
public static void main(String[] args) {
SessionFactory factory = new AnnotationConfiguration().configure().buildSessionFactory();
Session session = factory.openSession();
Transaction tx = session.beginTransaction();
Employee emp = new Employee();
Address addr1 = new Address();
Address addr2 = new Address();
Set set = new HashSet();
emp.setEmpId(11111);
emp.setName("Mohan");
addr1.setAddrId(22);
addr1.setCity("Bengaluru22");
addr2.setAddrId(33);
addr2.setCity("Bengaluru33");
set.add(addr1);
set.add(addr2);
emp.setSet(set);
session.persist(emp);
tx.commit();
session.close();
// factory.close();
System.out.println("Record saved successfully...");
}
}
UpdateOrDelete.java
====================
package mypack;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
public class DeleteOrUpdate {
public static void main(String[] args) {
SessionFactory factory = new AnnotationConfiguration().configure().buildSessionFactory();
Session session = factory.openSession();
Transaction tx = session.beginTransaction();
// Employee emp = (Employee)session.load(Employee.class, 1312);
Address addr = (Address)session.load(Address.class, 22222);
//addr.setCity("Delhi");
// session.delete(emp);
// session.persist(addr);
session.delete(addr);
tx.commit();
session.close();
System.err.println("Record deleted successfully...");
}
}
FetchData.java
===============
package mypack;
import java.util.*;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class FetchData {
public static void main(String[] args) {
SessionFactory factory = new Configuration().configure().buildSessionFactory();
Session session = factory.openSession();
Transaction tx = session.beginTransaction();
Query q = session.createQuery("From Employee");
List list = q.list();
Iterator itr = list.iterator();
while(itr.hasNext()){
Employee emp = (Employee) itr.next();
System.out.println(emp.getEmpId()+" "+emp.getName());
System.out.println("Address : "+emp.getSet());
}
// System.err.println("Records retrieval over...");
}
}
No comments:
Post a Comment