TUTIORALS FROM MADHAV:
SPRING-AOP-ANNOTATIONS SPRING -DAO SPRIN-MVC SPRING-SECUTITY
Simple hibernate example program:
department.java
package softech;
public class department {
private int deptno;
private String dname;
private String loc;
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
}
department.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="softech.department" table="dept">
<id name="deptno" type="int" column="deptno" >
<generator class="increment"/>
</id>
<property name="dname">
<column name="dname" />
</property>
<property name="loc">
<column name="loc"/>
</property>
</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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="myeclipse.connection.profile"></property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
<property name="connection.username">madhavrao</property>
<property name="connection.password">chiruta</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<mapping resource="department.hbm.xml"/>
</session-factory>
</hibernate-configuration>
TestCase.java
package softech;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.hibernate.cfg.*;
import org.hibernate.*;
import org.apache.log4j.*;
public class TestCase {
/**
* @param args
*/
SessionFactory factory;
public void createDept(department d)
{
Session session=null;
Transaction tx=null;
try
{
session =factory.openSession();
tx=session.beginTransaction();
System.out.println("break1 in cd");
session.save(d);
tx.commit();
System.out.println("record inserted");
}
catch(HibernateException e)
{
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("first");
TestCase t=new TestCase();
Configuration cfg=new Configuration();
cfg.configure();
//cfg.addResource("employee.hbm.xml");
//cfg.addResource("personaldetails.hbm.xml");
System.out.println("break 2");
t.factory=cfg.buildSessionFactory();
department d=new department();
d.setDname("one");
d.setLoc("hyd");
t.createDept(d);
System.out.println("execution over");
}
}
Problems of inheritance:
1) table for class hierarchy
Employee.java
package softech;
public class Employee {
private int empno;
private String ename;
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
}
SalariedEmployee.java
package softech;
public class SalariedEmployee extends Employee{
public double annualSal;
public double getAnnualSal() {
return annualSal;
}
public void setAnnualSal(double annualSal) {
this.annualSal = annualSal;
}
}
employee.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="softech.Employee" table="employeedetails">
<id name="empno" type="int" column="empno" >
<generator class="increment"/>
</id>
<discriminator column="type"></discriminator>
<property name="ename">
<column name="ename" />
</property>
<subclass name="softech.SalariedEmployee" discriminator-value="salaried">
<property name="annualSal"></property>
</subclass>
</class>
</hibernate-mapping>
department.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="softech.department" table="dept">
<id name="deptno" type="int" column="deptno" >
<generator class="increment"/>
</id>
<property name="dname">
<column name="dname" />
</property>
<property name="loc">
<column name="loc"/>
</property>
</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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">madhavrao</property>
<property name="connection.url">jdbc:odbc:goddati</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="connection.password">chiruta</property>
<property name="connection.driver_class">sun.jdbc.odbc.JdbcOdbcDriver</property>
<mapping resource="employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
TestCase.java
package softech;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.hibernate.cfg.*;
import org.hibernate.*;
import org.apache.log4j.*;
public class TestCase {
/**
* @param args
*/
SessionFactory factory;
public void createEmp(SalariedEmployee sm)
{
Session session=null;
Transaction tx=null;
try
{
session =factory.openSession();
tx=session.beginTransaction();
System.out.println("break1 in cd");
session.save(sm);
tx.commit();
System.out.println("record inserted");
}
catch(HibernateException e)
{
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("first");
TestCase t=new TestCase();
Configuration cfg=new Configuration();
cfg.configure();
//cfg.addResource("employee.hbm.xml");
//cfg.addResource("personaldetails.hbm.xml");
System.out.println("break 2");
t.factory=cfg.buildSessionFactory();
SalariedEmployee se=new SalariedEmployee();
se.setEname("madhav");
se.setAnnualSal(9600);
t.createEmp(se);
System.out.println("execution over");
}
}
2)table for subclass hierarchy
Employee.java
package softech;
public class Employee {
private int empno;
private String ename;
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
}
SalariedEmployee.java
package softech;
public class SalariedEmployee extends Employee{
public double annualSal;
public int empno;
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public double getAnnualSal() {
return annualSal;
}
public void setAnnualSal(double annualSal) {
this.annualSal = annualSal;
}
}
Employee.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="softech.Employee" table="employeedetails">
<id name="empno" type="int" column="empno" >
<generator class="increment"/>
</id>
<discriminator column="type"></discriminator>
<property name="ename">
<column name="ename" />
</property>
<joined-subclass name="softech.SalariedEmployee" table="salariedemployee">
<key column="empno"></key>
<property name="annualSal"></property>
</joined-subclass>
</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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">madhavrao</property>
<property name="connection.url">jdbc:odbc:goddati</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="connection.password">chiruta</property>
<property name="connection.driver_class">sun.jdbc.odbc.JdbcOdbcDriver</property>
<mapping resource="employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
TestCase.java
package softech;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.hibernate.cfg.*;
import org.hibernate.*;
import org.apache.log4j.*;
public class TestCase {
/**
* @param args
*/
SessionFactory factory;
public void createEmp(SalariedEmployee sm)
{
Session session=null;
Transaction tx=null;
try
{
session =factory.openSession();
tx=session.beginTransaction();
System.out.println("break1 in cd");
session.save(sm);
tx.commit();
System.out.println("record inserted");
}
catch(HibernateException e)
{
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("first");
TestCase t=new TestCase();
Configuration cfg=new Configuration();
cfg.configure();
//cfg.addResource("employee.hbm.xml");
//cfg.addResource("personaldetails.hbm.xml");
System.out.println("break 2");
t.factory=cfg.buildSessionFactory();
SalariedEmployee se=new SalariedEmployee();
se.setEname("madhav");
se.setAnnualSal(9600);
t.createEmp(se);
System.out.println("execution over");
}
}
Problems of relationships:
1)one to one
Employee.java
package softech;
public class Employee {
private int empno;
private String ename;
private PersonalDetails personalDetails;
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public PersonalDetails getPersonalDetails() {
return personalDetails;
}
public void setPersonalDetails(PersonalDetails personalDetails) {
this.personalDetails = personalDetails;
}
}
PersonalDetails.java
package softech;
public class PersonalDetails {
private int empno;
private String firstname;
private String lastname;
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
}
employee.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="softech">
<class name="Employee" table="employee">
<id name="empno" type="int">
<column name="empno"></column>
<generator class="increment"/>
</id>
<property name="ename">
<column name="ename" />
</property>
<one-to-one name="personalDetails" class="PersonalDetails">
</one-to-one>
</class>
<class name="PersonalDetails" table="personaldetails">
<id name="empno" type="int">
<column name="empno"></column>
<generator class="increment"/>
</id>
<property name="firstname">
<column name="firstname" />
</property>
<property name="lastname">
<column name="lastname"/>
</property>
</class>
</hibernate-mapping>
Personaldetails.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="softech">
<class name="PersonalDetails" table="personaldetails">
<id name="empno" type="int" column="empno" >
<generator class="increment"/>
</id>
<property name="firstname">
<column name="firstname" />
</property>
<property name="lastname">
<column name="lastname"/>
</property>
</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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">madhavrao</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
<!-- <property name="dialect">org.hibernate.dialect.OracleDialect</property>-->
<property name="connection.password">chiruta</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<mapping resource="employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
TestCase2.java
package softech;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
public class TestCase2 {
public static void main(String[] args)
{
// TODO Auto-generated method stub
Logger.getRootLogger().setLevel(Level.OFF);
System.out.println("first");
TestCase t=new TestCase();
Configuration cfg=new Configuration();
cfg.configure();
System.out.println("break 2");
try{
SessionFactory factory=cfg.buildSessionFactory();
Session session=factory.openSession();
//PersonalDetails ps=new PersonalDetails();
//ps.setFirstname("gopi");
//ps.setLastname("rao");
Employee e=(Employee)session.load(Employee.class, 9);
System.out.println("ename="+e.getEname());
System.out.println("empno="+e.getEmpno());
PersonalDetails ps=e.getPersonalDetails();
System.out.println("firstname="+ ps.getFirstname());
System.out.println("lastname="+ps.getLastname());
System.out.println("empno="+ps.getEmpno());
//Transaction tx=session.beginTransaction();
System.out.println("before create");
System.out.println("break1 in cd");
//session.save(e);
//tx.commit();
//System.out.println("record inserted");
session.close();
}
catch(HibernateException ee)
{
ee.printStackTrace();
}
}
}
No comments:
Post a Comment