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>
When you specify a
<generator>, you’re telling Hibernate how to create the ID.assignedmeans 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.identitymeans the database does it for you, which is what you want. In some databases you can usesequence(which will query the sequence generator to get the ID, then write the record), and Hibernate allows you to usenativeto specify the most applicable for your DB.apparently works in this case.