Spring hibernate template save or update

Source: Internet
Author: User

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 );
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.