Hibernate 的生成器类没有真正工作?
在对我和我的 Eclipse 感到沮丧几个小时后,在这里问这个问题。希望能在这里找到喘息的机会。
我正在尝试通过 Hibernate 3.0 将 pojo 对象保存到 MySQL 数据库中。基本上我的要求是:我需要在保存之前为对象分配 id,而不是让 Hibernate 为我做这件事。
为此,我查阅了文档,发现
完全符合我的要求。因此,我通过 .hbm.xml
文件更新了 id
的以下内容:
<id name="id" type="int">
<column name="ID" />
<generator class="assigned" />
</id>
我的 pojo 将 .hbm.xml
文件与 T 匹配
。我正在设置所有参数,包括我的 pojo 的 ID
并调用 Hibernate 的 saveOrUpdate(object)
方法。
如果有任何帮助,我的数据库表的 ID
列已禁用“auto-inc”。
令人难以置信的是,当我查看数据库表内容时,插入了一行,其中包含 Hibernate 自己的 ID,而不是我设置的 ID。
怎么可能?还有其他因素会影响 ID
吗?我错过了什么吗?有什么解决办法吗?
我的 hibernate.properties 如下所示(如果有任何帮助):
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
Asking this question here after hours of frustration with me and my Eclipse. Hoping to find some respite here.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,恶作剧警报!这实际上是
Eclipse
-Tomcat
集成的问题。在hbm
文件生效之前,我必须清理Tomcat
的目录并重新发布。提示:如果您在 Eclipse 中运行 ant,请务必时常刷新 Eclipse 工作区。学习一些东西是很困难的。
Well, hoax alarm!. It was really an issue with
Eclipse
-Tomcat
integration. I had to clean upTomcat
's directories and republish before thehbm
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.
您可以编写自定义序列生成器类来返回适合您要求的 id。有关更多详细信息,请参阅: http:// danu-javastuff.blogspot.com/2009/01/custom-id-generator-class-for-hibernate.html
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