2

I have designed a table (in postgres) where 'id' column is autoincremented via its SEQUENCE entity. But when I began use hibernate I met the problem .... due creating the insert statement, it use the follow statement

INSERT INTO mytable (id, name) VALUE (0, 'blablabla')

...

but I want it make somethink like that:

INSERT INTO mytable (name) VALUE ('blablabla')

... Postgres have to generate id automatically (at least when I ran such scripts withing sql editor, it worked)

I belive it can be configured, but I don't know how...

Here is my part of my .hbm.xml:

<hibernate-mapping>
    <class name="com.cse.database.bean.Category" table="category">
        <id name="id" type="int">
            <column name="id" />
            <generator class="assigned" />
        </id>
        <property name="name" type="string">
            <column name="name" length="100" not-null="true" unique="true" />
        </property>
    </class>
</hibernate-mapping>
3
  • 1
    It matters that it's PostgreSQL. When you specify a <generator>, you're telling Hibernate how to create the ID. assigned means your application is doing it (which you don't want). identity means the database does it for you, which is what you want, but docs.jboss.org/hibernate/core/3.3/reference/en/html/… doesn't list PostgreSQL as supporting that. Maybe sequence works with PostgreSQL? You may just be able to use native as well, and it'll automagically work... Commented Jan 31, 2012 at 23:08
  • Seems setting generator to 'idenitity' solved my problem :) Can you post your message as answer and I'll accept it Commented Jan 31, 2012 at 23:34
  • Done. Wasn't sure if identity would work for PostgreSQL, since the Hibernate docs didn't list that DB. Commented Jan 31, 2012 at 23:38

2 Answers 2

2

When you specify a <generator>, you're telling Hibernate how to create the ID. assigned means your application is doing it (which you don't want). You can specify a Hibernate class or even an application class to do it, which you don't want either. identity means the database does it for you, which is what you want. In some databases you can use sequence (which will query the sequence generator to get the ID, then write the record), and Hibernate allows you to use native to specify the most applicable for your DB.

    <id name="id" type="int">
        <column name="id" />
        <generator class="identity" />
    </id>

apparently works in this case.

Sign up to request clarification or add additional context in comments.

Comments

1

assigned means that you explicitely assign the ID of the entity, which is not the case. The problem with these auto-generated IDs in postgreSQL, AFAIK, is that it's not possible to get the last generated ID, and that Hibernate has no way to get the ID assigned to the entity after it has inserted it. You should not use an autogenerated ID in PostgreSQL.

Instead, let Hibernate use a sequence generator and assign the ID to the entity before inserting it:

@SequenceGenerator(name = "FOO_SEQ", sequenceName = "FOO_SEQ")
public class Foo {
    @Id
    @GeneratedValue(generator = "FOO_SEQ")
    @Column(name = "FOO_ID")
    private Long id;

3 Comments

>> with these auto-generated IDs in postgreSQL, AFAIK ..... I don't want to get last generated id .. O just want Hibernate won't try to fill 'id' column due inserting the data
Hibernate NEEDS to know the generated ID. Without an ID, Hibernate can't maintain entities in the session. Hibernate won't be usable if it doesn't know the generated ID of the entities.
What if you don't need hibernate to maintain the entity in the session?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.