Hibernate's generator class not really working?
Asking this question here after hours of frustration with me and my Eclipse. Hoping to find some respite her开发者_JAVA百科e.
I'm trying to save a pojo object into MySQL database via Hibernate 3.0. Basically my requirement is: I need to assign the id for the object before save and not let Hibernate do it for me.
For this I looked up in the documentation and saw that <generator class="assigned"/>
perfectly fits my bill. Consequently I updated by .hbm.xml
file with the following for the id
:
<id name="id" type="int">
<column name="ID" />
<generator class="assigned" />
</id>
My pojo matches .hbm.xml
file to the T.
I'm setting all the parameters including the ID
of my pojo and calling Hibernate's saveOrUpdate(object)
method.
If it's of any help, the ID
column of my database table has "auto-inc" disabled.
Unbelievably, when I look at the database table contents, a row has been inserted with Hibernate's own ID and not what I had set.
How's it possible? Is there anything else affecting the ID
? Am I missing on something? What's the work around?
My hibernate.properties
looks like below(if it's of any help):
hibernate.connection.driver_class =com.mysql.jdbc.Driver
hibernate.dialect =org.hibernate.dialect.MySQLDialect
hibernate.connection.url =jdbc:mysql://localhost/dbdbdbdbdb
hibernate.connection.username=root
hibernate.connection.password=password
hibernate.connection.pool_size=10
jdbc.batch_size=30
hibernate.show_sql=true
hibernate.current_session_context_class=true
hibernate.hbm2ddl.auto=validate
hibernate.cglib.use_reflection_optimizer=false
hibernate.generate_statistics=true
hibernate.cache.use_query_cache=true
hibernate.cache.region.factory_class=net.sf.ehcache.hibernate.EhCacheRegionFactory
Well, hoax alarm!. It was really an issue with Eclipse
-Tomcat
integration. I had to clean up Tomcat
's directories and republish before the hbm
files could take effect.
Tip: If you run ant within Eclipse, be sure to keep refreshing Eclipse workspace every now and then. Learning some things the hard way.
You can write your custom sequence generator class to return ids suitable to your requirement. For more details follow: http://danu-javastuff.blogspot.com/2009/01/custom-id-generator-class-for-hibernate.html
精彩评论