Hibernate和Postgresql-休眠映射文件中的生成器类 [英] Hibernate and Postgresql - generator class in hibernate mapping file

查看:99
本文介绍了Hibernate和Postgresql-休眠映射文件中的生成器类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的postgresql数据库中的id是自动递增的(在数据库中定义了序列).创建休眠映射文件时,我将类生成器设置为递增:

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>

但是,我不断收到各种错误(空指针异常,org.hibernate.TransactionException:事务未成功启动),因此我首先想确保在调试并在其他地方查找错误之前,这是正确的生成器类.我尝试了序列(根本不起作用,在某些情况下,增量起作用).该应用程序是用JSF 2.0编写的.

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.

最好的问候, ass.

Best Regards, sass.

推荐答案

如果您想要使用序列,则如果您想打个招呼,则绝对应该使用sequenceseqhilo之一. /lo算法生成器.问题是根本不起作用" 根本无法帮助您理解遇到的问题.

If you want to use sequences, you should definitely use one of the sequence or seqhilo 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.

以防万一,下面是sequence生成器的代码段:

Just in case, here is a snippet for the sequence generator:

<id name="id" type="long" column="person_id">
        <generator class="sequence">
                <param name="sequence">person_id_sequence</param>
        </generator>
</id>

对于seqhilo生成器:

<id name="id" type="long" column="cat_id">
        <generator class="seqhilo">
                <param name="sequence">hi_value</param>
                <param name="max_lo">100</param>
        </generator>
</id>

如果您想调查为什么它根本不起作用",我建议启用

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.

还要注意,PostgreSQL确实支持identity生成器(请参见 HB- 875 HHH-1675 ),使用SERIAL或BIGSERIAL列.

Also note that PostgreSQL does support the identity generator (see HB-875 and HHH-1675) when using SERIAL or BIGSERIAL columns.

  • Hibernate Core参考指南
    • Hibernate Core Reference Guide
      • 5.1.4.1. Generator
      • 5.1.4.2. Hi/lo algorithm
      • 5.1.4.4. Identity columns and sequences

      这篇关于Hibernate和Postgresql-休眠映射文件中的生成器类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆