Hibernate 和 Postgresql - hibernate 映射文件中的生成器类
我的 postgresql 数据库中的 ids 是自动递增的(数据库中定义了序列)。创建休眠映射文件时,我将类生成器设置为递增:
<class name="model.Names" schema="public" table="names">
<id name="id" type="int">
<column name="id"/>
<generator class="increment"/>
</id>
但是,我不断收到各种错误(空指针异常,org.hibernate.TransactionException:事务未成功启动),所以我首先想确保这是在调试和在其他地方查找错误之前正确的生成器类。我尝试了序列(根本不起作用,增量在某些情况下起作用)。该应用程序是用 JSF 2.0 编写的。
预先感谢您的任何建议。
此致, 粗鲁。
The ids in my postgresql database are auto-incremental (there are sequences defined in a database). When creating a hibernate mapping files I set the class generator to increment:
<class name="model.Names" schema="public" table="names">
<id name="id" type="int">
<column name="id"/>
<generator class="increment"/>
</id>
However, I keep getting all kinds of errors (null pointer exceptions, org.hibernate.TransactionException: Transaction not successfully started) so I first wanted to make sure that this is the right generator class before debugging and looking for errors elsewhere. I tried sequence (does not work at all, the increment works in some cases). The application is written in JSF 2.0.
Thanks in advance for any suggestions.
Best Regards,
sass.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想要使用序列,并且需要 hi/lo 算法生成器,则绝对应该使用
sequence
或seqhilo
之一。问题是“根本不起作用”根本无助于理解您面临的问题。以防万一,这里是
sequence
生成器的片段:对于
seqhilo
生成器:如果您想调查为什么它“根本不起作用”,我建议启用记录生成的 SQL 以查看发生了什么。
另请注意,PostgreSQL 确实支持
identity
生成器(请参阅 HB-875 和 HHH-1675) 使用时SERIAL 或 BIGSERIAL 列。参考资料
If you want to use sequences, you should definitely use one of the
sequence
orseqhilo
if you want a hi/lo algorithm generators. The problem is that "does not work at all" does not help at all to understand what problem you faced.Just in case, here is a snippet for the
sequence
generator:And for the
seqhilo
generator:If you want to investigate why it "does not work at all", I suggest to enable logging of the generated SQL to see what is happening.
Also note that PostgreSQL does support the
identity
generator (see HB-875 and HHH-1675) when using SERIAL or BIGSERIAL columns.References