Confusion caused by hibernate UUID. HEX primary key generation + spring

Source: Internet
Author: User
When the second insert operation is performed, an error is returned?

Table Structure

Create Table 'cloumn '(
'C _ id' varchar (16) not null,
'U _ id' varchar (16) default null,
'C _ name' varchar (100) default null,
'C _ sort 'int (11) default null,
'C _ beizhu' varchar (100 ),
Primary Key ('C _ id ')
 
)

2. configuration file <? 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 = "hibernate3test. Dal. cloumn"
Table = "cloumn"
>
<ID name = "CID" type = "Java. Lang. String" column = "c_id">
// UUID. HEX Generation Method
<Generator class = "UUID. Hex"/>
</ID>
<Property name = "u_id" type = "Java. Lang. String" column = "u_id"/>
<Property name = "cname" type = "Java. Lang. String" column = "c_name"/>
<Property name = "csort" type = "Java. Lang. Integer" column = "c_sort"/>
<Property name = "cbeizhu" type = "Java. Lang. String" column = "c_beizhu"/>

</Class>
</Hibernate-mapping>

Spring configuration file <bean id = "test" class = "hibernate3test. Dal. Dal">

<Property name = "sessionfactory"> <ref local = "sessionfactory"/> </property>
</Bean>

VO public class cloumn implements serializable {

Private string CID;
Private string u_id;
Private string cname;
Private integer csort;
Private string cbeizhu;
Public cloumn (){
}
// Geter setter

Dao code package hibernate3test. Dal;
Import org. springframework. Orm. hibernate3.support. hibernatedaosupport;

Public class Dal extends hibernatedaosupport {
Public Dal (){
}
Public void addcloumn ()
{
Cloumn cc = new cloumn ();
Cc. setu_id ("297ebd930b46197a ");
Cc. setcname ("test adding Block 1 ");
Cc. setcbeizhu ("Remarks ");
Cc. setcsort (1 );
This. gethibernatetemplate (). Save (CC );
}

Test public static void main (string ARGs [])
{
// Load the spring configuration file
Resource rs = new classpathresource ("appcontext. xml ");
Beanfactory factory = new xmlbeanfactory (RS );
// Obtain the dal
Dal Dao = (DAL) Factory. getbean ("test ");
// Add
Dao. addcloumn ();
// An error is returned when you add the object again the second time ..??
// Dao. addcloumn ();
}

How can this happen? The first time you can add data to the database, and the second time you call Dao. addcloumn (), an error is returned?

There should be no problem with this ?? Note: I have no problem using hibernate alone for testing, and the spring problem will arise ..

The following is the hibernate test code (passed)
// Obtain the session
Public session getsession ()
{
Try
{
Configuration CFG = new configuration (). Configure ();
Sessionfactory Sf = cfg. buildsessionfactory ();
Return SF. opensession ();
}
Catch (exception ee)
{
Log. Error ("error:" + ee. getmessage ());
}
Return NULL;
}

// Add Method
Public void addcloumn ()
{
Cloumn cc = new cloumn ();
Cc. setu_id ("297ebd930b46197a ");
Cc. setcname (" 1 ");
Cc. setcbeizhu ("Remarks ");
Cc. setcsort (1 );
Session Ss = This. getsession ();
SS. Save (CC );
SS. Flush ();
SS. Close ();
}

Public static void main (string [] ARGs ){
Dao test = New Dao ();
// Add
Test. addcloumn ();
Add again
Test. addcloumn ();

}

So there is no problem ..

When spring is added, the problem occurs.

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.