Hibernate provides its own transaction management implementation. However, when using spring to integrate hibernate, we recommend that you hand over the transaction to spring for programmatic transaction management.
1. User. Java
Package cn.com. Boyang;
Public class user {
Private integer ID;
Private string name;
Private integer age;
Public integer GETID (){
Return ID;
}
Public void setid (integer ID ){
This. ID = ID;
}
Public String getname (){
Return name;
}
Public void setname (string name ){
This. Name = Name;
}
Public integer getage (){
Return age;
}
Public void setage (integer age ){
This. Age = age;
}
}
2. iuserdao. Java
Package cn.com. Boyang;
Public interface iuserdao {
Public void insert (User user );
Public user find (integer ID );
}
3. userdao. Java
Package cn.com. Boyang;
Import org. hibernate. sessionfactory;
Import org. springframework. Dao. dataaccessexception;
Import org. springframework. Orm. hibernate3.hibernatetemplate;
Import org. springframework. transaction. transactiondefinition;
Import org. springframework. transaction. transactionstatus;
Import org. springframework. transaction. Support. transactioncallbackwithoutresult;
Import org. springframework. transaction. Support. transactiontemplate;
Import org. springframework. Orm. hibernate3.hibernatetransactionmanager;
Public class userdao implements iuserdao {
Private transactiontemplate;
Private hibernatetemplate;
Public void setsessionfactory (
Sessionfactory ){
This. transactiontemplate =
New transactiontemplate (
New hibernatetransactionmanager (
Sessionfactory ));
This. hibernatetemplate = new hibernatetemplate (sessionfactory );
}
Public void insert (User user ){
Final user userdata = user;
Transactiontemplate. setpropagationbehavior (
Transactiondefinition. propagation_required );
Transactiontemplate.exe cute (
New transactioncallbackwithoutresult (){
Protected void dointransactionwithoutresult (
Transactionstatus status ){
Try {
Hibernatetemplate. Save (userdata );
}
Catch (dataaccessexception e ){
E. printstacktrace ();
Status. setrollbackonly ();
}
}
});
}
Public user find (integer ID ){
User user =
(User) hibernatetemplate. Get (user. Class, ID );
Return user;
}
}
4. springhibernatedemo. Java
Package cn.com. Boyang;
Import org. springframework. Context. applicationcontext;
Import org. springframework. Context. Support. filesystemxmlapplicationcontext;
Public class springhibernatedemo {
Public static void main (string [] ARGs ){
Applicationcontext context =
New filesystemxmlapplicationcontext (
"Src/beans-config.xml ");
// Create a DaO object
Iuserdao userdao = (iuserdao) Context. getbean ("userdao ");
User user = new user ();
User. setname ("caterpillar ");
User. setage (New INTEGER (30 ));
Userdao. insert (User );
User = userdao. Find (New INTEGER (1 ));
System. Out. println ("name:" + User. getname ());
}
}
5. User. 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 = "cn.com. Boyang. User"
Table = "user">
<ID name = "ID" column = "ID">
<Generator class = "native"/>
</ID>
<Property name = "name" column = "name"/>
<Property name = "Age" column = "Age"/>
</Class>
</Hibernate-mapping>
6. beans-config.xml
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype beans public "-// spring/DTD bean/EN"
Http://www.springframework.org/dtd/spring-beans.dtd>
<Beans>
<Bean id = "datasource"
Class = "org. springframework. JDBC. datasource. drivermanagerdatasource">
<Property name = "driverclassname">
<Value> com. MySQL. JDBC. Driver </value>
</Property>
<Property name = "url">
<Value> JDBC: mysql: // localhost: 3306/dbsourcedemo </value>
</Property>
<Property name = "username">
<Value> root </value>
</Property>
<Property name = "password">
<Value> 12345678 </value>
</Property>
</Bean>
<Bean id = "sessionfactory"
Class = "org. springframework. Orm. hibernate3.localsessionfactorybean"
Destroy-method = "close">
<Property name = "datasource">
<Ref bean = "datasource"/>
</Property>
<Property name = "mappingresources">
<List>
<Value> cN/COM/Boyang/user. HBM. xml </value>
</List>
</Property>
<Property name = "hibernateproperties">
<Props>
<Prop key = "hibernate. dialect">
Org. hibernate. dialect. mysqldialect
</Prop>
</Props>
</Property>
</Bean>
<Bean id = "userdao" class = "cn.com. Boyang. userdao">
<Property name = "sessionfactory">
<Ref bean = "sessionfactory"/>
</Property>
</Bean>
</Beans>
7. Load the package file.
Spring-beans.jar spring-context.jar spring-core.jar spring-dao.jar spring-jdbc.jar spring-hibernate3.jar hibernate 3.1 core libraries mysql-connector-Java- 5.0.8 -Bin. Jar
8. database table structure definition.
Create Database dbsourcedemo;
Use dbsourcedemo;
Create Table user (
Id int (11) not null auto_increment primary key,
Name varchar (100) not null default '',
Age int
);