Search code examples
hibernatepostgresqlormsequences

Hibernate and Postgresql - generator class in hibernate mapping file


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.


Solution

  • 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.

    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>
    

    And for the seqhilo generator:

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

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

    References