Spring hibernate template save or update
We strongly recommend that new users go more
Http://www.java2s.com/turn.
//////////////////////////////////////// /////////////////////////////////
Import java. util .*;
ImportOrg. springframework. Context. Support. classpathxmlapplicationcontext;
ImportOrg. springframework. Orm. hibernate3.hibernatetemplate;
ImportOrg. springframework. Orm. hibernate3.hibernatecallback;
ImportOrg. hibernate .*;
ImportOrg. hibernate. criterion .*;
Public classMain {
Public staticVoidMain (string [] ARGs ){
Hibernateutil. Setup ("create table events (UID int, name varchar, start_date Date, duration INT );");
// Hibernate code start
Hibernatefactory. buildsessionfactory ();
Sessionfactory = hibernatefactory. getsessionfactory ();
Hibernatetemplate template =NewHibernatetemplate (sessionfactory );
Event event =NewEvent ();
Event. setname ("Spring hibernate template ");
Template. saveorupdate (event );
Event OBJ = (event) template. Load (event. Class, event. GETID ());
Hibernateutil. checkdata ("select uid, name from events ");
Hibernatefactory. closefactory ();
// Hibernate code end
}
}
//////////////////////////////////////// /////////////////////////////////
<? 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>
<ClassName = "Event" table = "events">
<ID name = "ID" column = "uid" type = "long">
<GeneratorClass= "Increment"/>
</ID>
<Property name = "name" type = "string"/>
<Property name = "startdate" column = "start_date" type = "date"/>
<Property name = "duration" type = "integer"/>
</Class>
</Hibernate-mapping>
//////////////////////////////////////// /////////////////////////////////
ImportJava. Io. serializable;
ImportJava. util. date;
ImportJava. util. Set;
ImportJava. util. javashashset;
Public classEventImplementsSerializable {
PrivateLong ID;
PrivateIntDuration;
PrivateString name;
PrivateDate startdate;
PublicEvent (){
}
PublicEvent (string name ){
This. Name = Name;
}
/**
* @ Hibernate. ID generator-class = "native" column = "uid"
* @ Return
*/
PublicLong GETID (){ReturnID ;}
PublicVoidSetid (long ID ){This. ID = ID ;}
/**
* @ Hibernate. Property column = "name"
* @ Return
*/
PublicString getname (){ReturnName ;}
PublicVoidSetname (string name ){This. Name = Name ;}
/**
* @ Hibernate. Property column = "start_date"
* @ Return
*/
PublicDate getstartdate (){ReturnStartdate ;}
PublicVoidSetstartdate (date startdate ){This. Startdate = startdate ;}
/**
* @ Hibernate. Property column = "duration"
* @ Return
*/
PublicIntGetduration (){ReturnDuration ;}
PublicVoidSetduration (IntDuration ){This. Duration = duration ;}
}
//////////////////////////////////////// /////////////////////////////////
<? 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>
<! -- Database connection settings -->
<Property name = "connection. driver_class"> org. HSQLDB. jdbcdriver </property>
<Property name = "connection. url"> JDBC: HSQLDB: Data/tutorial </property>
<Property name = "connection. username"> SA </property>
<Property name = "connection. Password"> </property>
<! -- JDBC connection pool (use the built-in) -->
<Property name = "connection. pool_size"> 1 </property>
<! -- SQL Dialect -->
<Property name = "dialect"> org. hibernate. dialect. hsqldialect </property>
<! -- Echo all executed SQL to stdout -->
<Property name = "show_ SQL">True</Property>
<Mapping Resource = "event. HBM. xml"/>
</Session-factory>
</Hibernate-configuration>
//////////////////////////////////////// /////////////////////////////////
/**
* Represents exceptions thrown by the data access layer.
*/
Public classDataaccesslayerexceptionExtendsRuntimeexception {
PublicDataaccesslayerexception (){
}
PublicDataaccesslayerexception (string message ){
Super(Message );
}
PublicDataaccesslayerexception (throwable cause ){
Super(Cause );
}
PublicDataaccesslayerexception (string message, throwable cause ){
Super(Message, cause );
}
}