-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: The generators does not work?
PostPosted: Tue Feb 14, 2006 10:43 am 
Newbie

Joined: Wed Feb 08, 2006 1:11 pm
Posts: 9
I am trying to run the example from hibernate reference. I am using Hibernate 3.1 and MySql server 5.0. The class to be stored is the following:

public class Event {
private Long id;
private String title;
private Date date;
...setters and getters
}


the mapping file is the following:

<?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>

<class name="events.Event" table="events">
<id name="id" column="EVENT_ID">
<generator class="native"/>
</id>
<property name="date" type="timestamp" column="EVENT_DATE"/>
<property name="title"/>

</class>


</hibernate-mapping>


In Msql:
EVENT_ID is a BIGINT
DATE is a DATE
TITLe a varchar;

When I am trying to insert a new Event in the table, The following error occurs:
5:16:11,381 DEBUG SQL:346 - insert into events (EVENT_DATE, title) values (?, ?)
15:16:11,461 WARN JDBCExceptionReporter:71 - SQL Error: 1364, SQLState: HY000
15:16:11,461 ERROR JDBCExceptionReporter:72 - Field 'EVENT_ID' doesn't have a default value



However if I change the type of the EVENT_ID column to varchar and the java type of id to string and i use the generator class "uuid.hex" all insertion is done smoothly.
Does anyone know what is the problem


Thanks!
Eduard


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 14, 2006 11:54 am 
Newbie

Joined: Fri Apr 01, 2005 10:38 am
Posts: 3
Location: Chile
Eduard,
Try the folowing

public class Event {
private int id;
private String title;
private Date date;
...setters and getters
}

the mapping file is the following:

<?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>

<class name="events.Event" table="events">
<id name="id" column="EVENT_ID" type="int">
<generator class="native"/>
</id>
<property name="date" type="timestamp" column="EVENT_DATE"/>
<property name="title"/>

</class>


</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 14, 2006 12:02 pm 
Newbie

Joined: Wed Feb 08, 2006 1:11 pm
Posts: 9
Well, meanwhile I solved the problem by setting the generator class to increment. The native generator does not work!

Regards!
Eduard


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 14, 2006 12:21 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
Eduard Barbu wrote:
Well, meanwhile I solved the problem by setting the generator class to increment. The native generator does not work!

Regards!
Eduard


I think you need to read the docs about exactly what specifying "native" does. It's not type of generator in itself, it just defaults to one of several depending on the DB being used. You still need to configure those properly.


native - picks identity, sequence or hilo depending on the underlying capabilities of the database.


MySQL supports identity columns but you have to have your table defined properly.

I don't think it supports sequences and the hilo generator requires additional configuration in your mapping.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 14, 2006 12:37 pm 
Newbie

Joined: Wed Feb 08, 2006 1:11 pm
Posts: 9
Well, you are right. I also notice this but afterwards. However the documentation is a little bit misleading! When the example is posted it is written:

"The nested generator element specifies the
identifier generation strategy, in this case we used native, which picks the best strategy depending on the configured database (dialect)."

Which made me think that this generator abstsracts away the user from how the identifier is generated (but it guaranties in the same time the uniqness of the identifier).
For my application however the "increment " is ok!

Thanks!
Eduard


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.