The Following Example is shows how to save data into database for that you required the follwoing components
1.hibernate mapping file(hbm.xml)
2. simple PlainOldJavaObject (POJO) class with setters and Getters
3.required jar files for hibernate nad database driver
4.Ant script to run the program
5. simple Class with main() method
1.First i have Person table in database with the following fields
sql-query for creating table: create table person (personid int primary key,name varchar2(30),age int, weight int);
2. creating person.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 package="com.siva">
<class name="Person" table="person">
<id name="personId" type="java.lang.Long">
<generator class="increment"/>
<id/>
<property name="name" type="string"/>
<property name="age" type="java.lang.Long"/>
<property name="weight" type="java.lang.Long"/>
<class/>
<hibernate-mapping/>
In the above hbm file you have id generator class="increment" for id you no need to insert explicit value hibernate it self give id to you .
3. simple POJO class of Person
package com.siva;
public class Person
{
private Long personId;
private String name;
private Long age;
private Long weight;
public Long getPersonId() {
return personId;
}
public void setPersonId(Long personId) {
this.personId = personId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getAge() {
return age;
}
public void setAge(Long age) {
this.age = age;
}
public Long getWeight() {
return weight;
}
public void setWeight(Long weight) {
this.weight = weight;
}
}
Now for hibernate we have all are in place except configuration which database we need to point and what is driver for that so writing another xml file for this we mention all this in The following class itself
4. SimpleHibernateTest class as following
package com.siva;
import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.hibernate.cfg.Environment;
public class SimpleHibernateTest
{
public static void main(String[] args)
{
Transaction tx = null;
try
{
Configuration cfg = new Configuration();
cfg.setProperty(Environment.AUTOCOMMIT ,"true" );
cfg.setProperty (Environment.DRIVER, "oracle.jdbc.driver.OracleDriver");
cfg.setProperty(Environment.DIALECT, "org.hibernate.dialect.Oracle9Dialect");
cfg.setProperty(Environment.USER, "database username");
cfg.setProperty(Environment.PASS,"database password");
cfg.setProperty(Environment.URL, "jdbc:oracle:thin:@localhost:1521:SID");
//here SID is database name it is xe for oracle 10G
System.out.println("connected to databse " + cfg.getProperty(Environment.URL));
cfg.addFile("person.hbm.xml");
Person p = new Person();
p.setName("siva");
p.setAge(26L); p.setWeight(63L);
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.openSession();
tx = session.beginTransaction();
session.save(p);
System.out.println("successfully save the record in database");
tx.commit();
session.close();
} //try end
catch(Exception ex)
{
ex.printStackTrace();
tx.rollback();
}//catch end
}//main method end
}//class end
Now we run this program in two ways
1. we run this through command prompt like noramal program
javac com.siva.SimpleHibernateTest.java
but we need required jar files so all jar files we need to keep in classpath
or if we using eclipse no need to bother about class path you can all jar files to build path of
project properties.
2. writing ANT file through that we can run the program
if we want to run the program through ANT we need ANT bin directory to path.
the follwoing ANT XML file
<project name="test" default="compile">
<property name="sourcedir" value="${basedir}"/>
<property name="targetdir" value="${basedir}/bin"/>
<property file="build.properties"/>
<path id="libraries">
<fileset dir="${librarydir}">
<include name="*.jar">
<fileset>
<fileset dir="${OraLib}">
<include name="*.jar">
</fileset>
</path>
<target name="clean">
<delete dir="${targetdir}"/>
<mkdir dir="${targetdir}"/>
</target>
<target name="compile" depends="clean, copy-resources">
<javac srcdir="${sourcedir}"
destdir="${targetdir}"
classpathref="libraries"/>
</target>
<target name="copy-resources">
<copy todir="${targetdir}">
< fileset dir="${sourcedir}">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target name="run" depends="compile">
<java fork="true" classname="com.siva.SimpleHibernateTest" classpathref="libraries">
<classpath path="${targetdir}"/>
</java>
</target>
</project>
In the above build.xml file there is one propety file=build.properties
this is for to give the location of jar files
where your hibernate jar files and database jar fiels
like the following
librarydir = C:/Hibernate Training/hibernate-3.1.2/hibernate-3.1/lib
OraLib = C:/oracle/ora90/jdbc/lib
After completing the above steps
just locate where is your build.xml file
for eg: c:\test\build.xml
come to command prompt and type like the following
c:\test> ANT
automatically it will compile because in your nat file you written Default as compile
once successful compilation you need to rubn for that
in the command prompt type the following
c:\test>ANT run
it will executs and insert the data into database.
This is the simple Hibernate example to start to learn hibernate.
Wednesday, October 8, 2008
Wednesday, August 20, 2008
Java Access specifiers
Hi,
in java there is one more interesting concept about access specifiers
the following four access speciers are there in java
private
default (no access specifier keep blank)
protected
public
Now the explanation as below with example
Private
the private access specifier for only with in the class accessible not other than class
example
public class Person
{
private String name;
}
class Employee extends Person
{
public String helloString()
{
return name; // here it will show compilation error Person.name is not visible because
private access specifier used in Person class for name
}
}
public
we used this specifier variables, methods any where in any class
in other packages also we can use this variables and methods
example
package com.test;
public class Person
{
public String name = "siva";
}
package com.test1;
import com.test.Person;
public class Person extends Person
{
public String helloString()
{
return name;
}
}
if we take this scenario it will work fine here one more concept you need to know
Person class is extending Person class only why it is not showing exception
Because one Person class in one package
another Person class in another package
so public access specifier you can use any package any class you need to extend that classes
accordingly.
default(if you are not specifing any access specier it is default)
this access is package level access
example
package com.test
public Class Person
{
String name = "siva";
public static void main(String args[])
{
Employee e = new Employee()
System.out.println("Name is "+e.getPersonDetails());
}
}
class Employee extends Person
{
public String getPersonDetails()
{
return name;
}
}
in this Example you need not identify one more concept.
in one package two classes are there but one class having public one is having nothing
in any package accepts only one public class
protected
one of most important concept for interviews
suppose we have two packages
1 com.test;
2 com.test1;
package com.test; having one class with protected method
package com.test1;
two class one class not overriding protected method directly using that method.
now the second class in com.test1 package is extending the same package class
the protected method is available only for first class of in second package i.e com.test1
not for second class
if you are try to access that method it will through exception.
the good example is Object class Clone method it is protected
in java there is one more interesting concept about access specifiers
the following four access speciers are there in java
private
default (no access specifier keep blank)
protected
public
Now the explanation as below with example
Private
the private access specifier for only with in the class accessible not other than class
example
public class Person
{
private String name;
}
class Employee extends Person
{
public String helloString()
{
return name; // here it will show compilation error Person.name is not visible because
private access specifier used in Person class for name
}
}
public
we used this specifier variables, methods any where in any class
in other packages also we can use this variables and methods
example
package com.test;
public class Person
{
public String name = "siva";
}
package com.test1;
import com.test.Person;
public class Person extends Person
{
public String helloString()
{
return name;
}
}
if we take this scenario it will work fine here one more concept you need to know
Person class is extending Person class only why it is not showing exception
Because one Person class in one package
another Person class in another package
so public access specifier you can use any package any class you need to extend that classes
accordingly.
default(if you are not specifing any access specier it is default)
this access is package level access
example
package com.test
public Class Person
{
String name = "siva";
public static void main(String args[])
{
Employee e = new Employee()
System.out.println("Name is "+e.getPersonDetails());
}
}
class Employee extends Person
{
public String getPersonDetails()
{
return name;
}
}
in this Example you need not identify one more concept.
in one package two classes are there but one class having public one is having nothing
in any package accepts only one public class
protected
one of most important concept for interviews
suppose we have two packages
1 com.test;
2 com.test1;
package com.test; having one class with protected method
package com.test1;
two class one class not overriding protected method directly using that method.
now the second class in com.test1 package is extending the same package class
the protected method is available only for first class of in second package i.e com.test1
not for second class
if you are try to access that method it will through exception.
the good example is Object class Clone method it is protected
Wednesday, July 2, 2008
Java Interview Concepts
Hi,
i am writing the java interview concepts i am not giving complete details i am giving the overall structure for glance when ever interview is there it is for quick reference i hope this might help
Object class Details
Object Class is Super class of all java classes
Object class having the following methods
public final Class getClass()
public int hashCode()
public boolean equals(Object obj)
protected Object clone() throws CloneNotSupportedException
public String toString()
public final void notify()
public final void notifyAll()
public final void wait(long timeout) throws InterruptedException
protected void finalize() throws Throwable
--------etc
you will get all the implementation details and some where most of the people
ask about these details some questions are
Can we override finalize() method
Ans: yes we can override this method when ever we know object is going out of memory
that type object we call in finalize method. this work automatically done by java only
it has good alogitihm to remove the object from memory.
notify(), wait(),notifyAll() are related to threads but why there are overrided in
Object Class
Ans: this methods not only for Threads it is external resources also that's why they are
overrided inside Object class
Difference b/n equal (==) operator and equals() method why should we override
Ans: in equals() method also they are using == operator only but when ever we want to
compare content of the two objects and both objects content has same but when
we compare it it return false so we should have to override equals() in our class.
Now i am going to explain methods in Objects class with examples
1. toString()
Syntax: public String toString()
{
return " ";
}
> in String class toString() method is overrided. when ever you are working with String class you no need to override toString() method.
example without overriding toString()
public class Person
{
private String mname;
public Person(String aname)
{
mname= aname;
}
public static void main(String args[])
{
Person p = new Person("siva");
System.out.println("Name is " + p);
or //toString() method is Object class method so it will come for
any class
System.out.println("Name is " + p.toString())
}
}
ANS: Name is ClassName i.e Person@some hexadecimal value
here hexa decimal value is where the object stored in memory area
so result is not correct we need correct result for that we have to override
toString() method
attach this code for Person class
public String toString()
{
return mname;
}
Now run the program you will get result as ----- Name is siva
i am writing the java interview concepts i am not giving complete details i am giving the overall structure for glance when ever interview is there it is for quick reference i hope this might help
Object class Details
Object Class is Super class of all java classes
Object class having the following methods
public final Class getClass()
public int hashCode()
public boolean equals(Object obj)
protected Object clone() throws CloneNotSupportedException
public String toString()
public final void notify()
public final void notifyAll()
public final void wait(long timeout) throws InterruptedException
protected void finalize() throws Throwable
--------etc
you will get all the implementation details and some where most of the people
ask about these details some questions are
Can we override finalize() method
Ans: yes we can override this method when ever we know object is going out of memory
that type object we call in finalize method. this work automatically done by java only
it has good alogitihm to remove the object from memory.
notify(), wait(),notifyAll() are related to threads but why there are overrided in
Object Class
Ans: this methods not only for Threads it is external resources also that's why they are
overrided inside Object class
Difference b/n equal (==) operator and equals() method why should we override
Ans: in equals() method also they are using == operator only but when ever we want to
compare content of the two objects and both objects content has same but when
we compare it it return false so we should have to override equals() in our class.
Now i am going to explain methods in Objects class with examples
1. toString()
Syntax: public String toString()
{
return " ";
}
> in String class toString() method is overrided. when ever you are working with String class you no need to override toString() method.
example without overriding toString()
public class Person
{
private String mname;
public Person(String aname)
{
mname= aname;
}
public static void main(String args[])
{
Person p = new Person("siva");
System.out.println("Name is " + p);
or //toString() method is Object class method so it will come for
any class
System.out.println("Name is " + p.toString())
}
}
ANS: Name is ClassName i.e Person@some hexadecimal value
here hexa decimal value is where the object stored in memory area
so result is not correct we need correct result for that we have to override
toString() method
attach this code for Person class
public String toString()
{
return mname;
}
Now run the program you will get result as ----- Name is siva
Tuesday, June 24, 2008
Wednesday, June 11, 2008
Simple Hello world in core with java installation
Learn java in easy way
The following steps are how to install java , how to set path and "Hello world"
1. First download JDK latest version from http://java.sun.com/javase/downloads/index.jsp
2. save your hard disk and double click on that
3.while instalation it will ask location to install the JRE and JDK
otherwise by default it will install inside Programfiles of your system.
for JDK choose another location and for JRE keep by default location.
4. you completed instalation successfully.
5. Now you need to tell your operating System where you installed java as well as
to run java program also for this you have to set path in your system.
setting path in different ways i am giving simple way
right click on My Computer -properties-Advanced-Environment variables
if you click on Environment Variables one box will open and in that all the paths will be
located .
in that we find two windows
UserVaribles
SystemVaribles
-- in System variable you find variable name as path edit this and come to end of that
line give semicolon(;)
After that copy your java (JDK) installation location eg:d:\java\jdk1.5 you need to copy upto bin(eg:d:\java\jdk1.5\bin) copy location and paste after semicolon ( ; ) and give semicolon after java path also.
now your set the path successfully.
if you want to see path is set fine or not just open command prompt and type java
if it gives some help text then your path is ok otherwise again set the path by follwing above steps carefully.
once set the path successfully. Then cretae one folder for your all java programs in your system
eg:d:\javaexamples
How To Write a Hello world Program
1. open notepad or edit plus
type the following code
class Hello
{
public static void main(String args[])
{
System.out.println("Hello world");
}
}
2.save this program in d:\javaexamples folder.
3.open the command prompt and open d:\javaexamples
4. now type like this javac Hello.java
5. if you have no errors in your program and while setting the path agin it will show
d:\javaexamples
6. after that you type java Hello
7. after run the program you will see "Hello world" in commond prompt
The following steps are how to install java , how to set path and "Hello world"
1. First download JDK latest version from http://java.sun.com/javase/downloads/index.jsp
2. save your hard disk and double click on that
3.while instalation it will ask location to install the JRE and JDK
otherwise by default it will install inside Programfiles of your system.
for JDK choose another location and for JRE keep by default location.
4. you completed instalation successfully.
5. Now you need to tell your operating System where you installed java as well as
to run java program also for this you have to set path in your system.
setting path in different ways i am giving simple way
right click on My Computer -properties-Advanced-Environment variables
if you click on Environment Variables one box will open and in that all the paths will be
located .
in that we find two windows
UserVaribles
SystemVaribles
-- in System variable you find variable name as path edit this and come to end of that
line give semicolon(;)
After that copy your java (JDK) installation location eg:d:\java\jdk1.5 you need to copy upto bin(eg:d:\java\jdk1.5\bin) copy location and paste after semicolon ( ; ) and give semicolon after java path also.
now your set the path successfully.
if you want to see path is set fine or not just open command prompt and type java
if it gives some help text then your path is ok otherwise again set the path by follwing above steps carefully.
once set the path successfully. Then cretae one folder for your all java programs in your system
eg:d:\javaexamples
How To Write a Hello world Program
1. open notepad or edit plus
type the following code
class Hello
{
public static void main(String args[])
{
System.out.println("Hello world");
}
}
2.save this program in d:\javaexamples folder.
3.open the command prompt and open d:\javaexamples
4. now type like this javac Hello.java
5. if you have no errors in your program and while setting the path agin it will show
d:\javaexamples
6. after that you type java Hello
7. after run the program you will see "Hello world" in commond prompt
Tuesday, May 20, 2008
AJAX simple example
Hi,
This is simple ajax example with jsp when key up in one textbox time will be displayed on another textbox
1.we need html page look follwing
<form name="myform">
Name:<input type="text" name="username" onkeyup="ajaxFunction()"/>
Time:<input type= "text" name="time"/> </form>
<script type="text/javascript">
function ajaxFunction()
{
var xmlHttp;
try
{
xmlHttp = new XMLHttpRequest();//This is for other than IE browser creating
object.
}
catch(e)
{
try
{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
alert("Your browser does not support ajax");
return false;
}
}
}
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4)
{
document.myform.time.value=xmlHttp.responseText;
}
}
xmlHttp.open("GET","time.jsp",true);
xmlHttp.send(null);
}
</script>
2. time.jsp in same location
<%@page import="java.text.SimpleDateFormat%>
< %@page import="java.util.Calendar"%>
<%
Calendar cal = Calendar.getInstance();
SimpleDateFormat sd = new SimpleDateFormat("HH:mm:ss a");
String time = sd.format(cal.getTime());
out.println(time);
%>
if we run this application thru server we will get the result
This is simple ajax example with jsp when key up in one textbox time will be displayed on another textbox
1.we need html page look follwing
<form name="myform">
Name:<input type="text" name="username" onkeyup="ajaxFunction()"/>
Time:<input type= "text" name="time"/> </form>
<script type="text/javascript">
function ajaxFunction()
{
var xmlHttp;
try
{
xmlHttp = new XMLHttpRequest();//This is for other than IE browser creating
object.
}
catch(e)
{
try
{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
alert("Your browser does not support ajax");
return false;
}
}
}
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4)
{
document.myform.time.value=xmlHttp.responseText;
}
}
xmlHttp.open("GET","time.jsp",true);
xmlHttp.send(null);
}
</script>
2. time.jsp in same location
<%@page import="java.text.SimpleDateFormat%>
< %@page import="java.util.Calendar"%>
<%
Calendar cal = Calendar.getInstance();
SimpleDateFormat sd = new SimpleDateFormat("HH:mm:ss a");
String time = sd.format(cal.getTime());
out.println(time);
%>
if we run this application thru server we will get the result
spring hibernate standalone example
Hi, This is developed in eclipse.I given the structure wt eclipse follows
1.First open eclipse File - new - javaproject After that it opens one new window give name for that project after that click next and
finish
2.structure is like this
siva
src
bin
.classpath
3.create package. right click on src new package
com.siva
com.siva.domain
com.siva.service
com.siva.dao
com.siva.mappings
config-it is new folder to keep jdbc.properties file it is not package but it is inside src folder only
4.create new class on first package the class like this
/**
*
*/
package com.siva;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.siva.service.SimpleServiceImpl;
/**
* @author sivakumar
*
*/
public class SimpleSpringHibernate
{
public static void main(String[] args) {
System.out.println("WELCOME TO SIMPLE SPRING HIBERNATE EXAMPLE");
ApplicationContext context= new ClassPathXmlApplicationContext ("applicationContext.xml");
System.out.println("application context loaded successfully");
SimpleServiceImpl simple =(SimpleServiceImpl)context.getBean("simpleService");
System.out.println("simple Service called successfully");
simple.getDetails();
}
}
5.Now we need one service class
it look like
package com.siva.service;
import com.siva.dao.SimpleDaoHibernate;
public class SimpleServiceImpl
{
private SimpleDaoHibernate aSimpleDao;
public SimpleServiceImpl(SimpleDaoHibernate simpleDao)
{
aSimpleDao = simpleDao;
}
public void getDetails()
{ System.out.println("inside SimpleServiceImpl method");
aSimpleDao.getDetails();
}
}
6.DAo class look the following
package com.siva.dao;
import java.util.ArrayList;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.siva.domain.Emp;
public class SimpleDaoHibernate extends HibernateDaoSupport
{
public void getDetails()
{
System.out.println("insdie DAO Hibernate class");
String sql= "from Emp";
List details = (ArrayList)getHibernateTemplate().find(sql);
System.out.println("details is"+details.size());
System.out.println("Query executed successfully");
}
}
7.domain class like the following it is for OR Mapping
/**
*
*/
package com.siva.domain;
/**
* @author sivakumar
*
*/
public class Emp {
private Long id;
private String name;
private String salary;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSalary() {
return salary;
}
public void setSalary(String salary) {
this.salary = salary;
}
}
8.The mapping hbm file look the following
<hibernate-mapping package="com.siva.domain"
<class name="Emp" table="emp"
name="id" column="id" type="java.lang.Long"
name="name" column="name" type="string"
name="salary" column="salary" type="string" />
9.appicationContext.xml it is like the following this file inside src folder.
beans
bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
property name="locations"
list
value
classpath:config/jdbc.properties
value
list
property
bean
bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> property name="url" value="${hibernate.sem.connection.url}" />
property name="driverClassName"
value="${hibernate.sem.connection.driver_class}" />
property name="username"
value="${hibernate.sem.connection.username}" />
property name="password"
value="${hibernate.sem.connection.password}" /> bean
bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
property name="hibernateProperties">
props
prop key="hibernate.dialect"> ${hibernate.sem.dialect}
prop
prop key="hibernate.show_sql"> ${hibernate.sem.show_sql}
prop
props
property
property name="dataSource"
ref bean="dataSource" /> property
property name="mappingDirectoryLocations">
list
value
classpath:/com/siva/mappings/
value
list
property
bean
bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
property name="sessionFactory"> ref bean="sessionFactory" />
property
bean
bean id="simpleDao" class="com.siva.dao.SimpleDaoHibernate">
property name="hibernateTemplate"> ref bean="hibernateTemplate" /> property
bean
bean id="simpleService"
class="com.siva.service.SimpleServiceImpl">
constructor-arg ref="simpleDao">constructor-arg
bean
beans
10.jdbc.properties file
#hibernate.sem.dialect=org.hibernate.dialect.Oracle9Dialect
#hibernate.sem.show_sql=true
#hibernate.sem.connection.driver_class=oracle.jdbc.driver.OracleDriver
#hibernate.sem.connection.url=jdbc:oracle:thin:@localhost:1521:XE
#hibernate.sem.connection.username=user
#hibernate.sem.connection.password=password
hibernate.sem.dialect=org.hibernate.dialect.MySQLDialect
hibernate.sem.show_sql=true
hibernate.sem.connection.driver_class=com.mysql.jdbc.Driver
hibernate.sem.connection.url=jdbc:mysql://localhost:3306/test
hibernate.sem.connection.username=root
hibernate.sem.connection.password=
1.First open eclipse File - new - javaproject After that it opens one new window give name for that project after that click next and
finish
2.structure is like this
siva
src
bin
.classpath
3.create package. right click on src new package
com.siva
com.siva.domain
com.siva.service
com.siva.dao
com.siva.mappings
config-it is new folder to keep jdbc.properties file it is not package but it is inside src folder only
4.create new class on first package the class like this
/**
*
*/
package com.siva;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.siva.service.SimpleServiceImpl;
/**
* @author sivakumar
*
*/
public class SimpleSpringHibernate
{
public static void main(String[] args) {
System.out.println("WELCOME TO SIMPLE SPRING HIBERNATE EXAMPLE");
ApplicationContext context= new ClassPathXmlApplicationContext ("applicationContext.xml");
System.out.println("application context loaded successfully");
SimpleServiceImpl simple =(SimpleServiceImpl)context.getBean("simpleService");
System.out.println("simple Service called successfully");
simple.getDetails();
}
}
5.Now we need one service class
it look like
package com.siva.service;
import com.siva.dao.SimpleDaoHibernate;
public class SimpleServiceImpl
{
private SimpleDaoHibernate aSimpleDao;
public SimpleServiceImpl(SimpleDaoHibernate simpleDao)
{
aSimpleDao = simpleDao;
}
public void getDetails()
{ System.out.println("inside SimpleServiceImpl method");
aSimpleDao.getDetails();
}
}
6.DAo class look the following
package com.siva.dao;
import java.util.ArrayList;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.siva.domain.Emp;
public class SimpleDaoHibernate extends HibernateDaoSupport
{
public void getDetails()
{
System.out.println("insdie DAO Hibernate class");
String sql= "from Emp";
List
System.out.println("details is"+details.size());
System.out.println("Query executed successfully");
}
}
7.domain class like the following it is for OR Mapping
/**
*
*/
package com.siva.domain;
/**
* @author sivakumar
*
*/
public class Emp {
private Long id;
private String name;
private String salary;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSalary() {
return salary;
}
public void setSalary(String salary) {
this.salary = salary;
}
}
8.The mapping hbm file look the following
<hibernate-mapping package="com.siva.domain"
<class name="Emp" table="emp"
9.appicationContext.xml it is like the following this file inside src folder.
beans
bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
property name="locations"
list
value
classpath:config/jdbc.properties
value
list
property
bean
bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> property name="url" value="${hibernate.sem.connection.url}" />
property name="driverClassName"
value="${hibernate.sem.connection.driver_class}" />
property name="username"
value="${hibernate.sem.connection.username}" />
property name="password"
value="${hibernate.sem.connection.password}" /> bean
bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
property name="hibernateProperties">
props
prop key="hibernate.dialect"> ${hibernate.sem.dialect}
prop
prop key="hibernate.show_sql"> ${hibernate.sem.show_sql}
prop
props
property
property name="dataSource"
ref bean="dataSource" /> property
property name="mappingDirectoryLocations">
list
value
classpath:/com/siva/mappings/
value
list
property
bean
bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
property name="sessionFactory"> ref bean="sessionFactory" />
property
bean
bean id="simpleDao" class="com.siva.dao.SimpleDaoHibernate">
property name="hibernateTemplate"> ref bean="hibernateTemplate" /> property
bean
bean id="simpleService"
class="com.siva.service.SimpleServiceImpl">
constructor-arg ref="simpleDao">constructor-arg
bean
beans
10.jdbc.properties file
#hibernate.sem.dialect=org.hibernate.dialect.Oracle9Dialect
#hibernate.sem.show_sql=true
#hibernate.sem.connection.driver_class=oracle.jdbc.driver.OracleDriver
#hibernate.sem.connection.url=jdbc:oracle:thin:@localhost:1521:XE
#hibernate.sem.connection.username=user
#hibernate.sem.connection.password=password
hibernate.sem.dialect=org.hibernate.dialect.MySQLDialect
hibernate.sem.show_sql=true
hibernate.sem.connection.driver_class=com.mysql.jdbc.Driver
hibernate.sem.connection.url=jdbc:mysql://localhost:3306/test
hibernate.sem.connection.username=root
hibernate.sem.connection.password=
Subscribe to:
Posts (Atom)