Serial Postgresql Hibernate

'The type names serial and serial4 are equivalent: both create integer columns.' Right now we are getting several instances of Hibernate not picking the right primary key during insertion using the AUTO strategy for tables (which. Torrent Backtrack 3 Final. I'm switching from MySQL to PostgreSQL and was wondering how I can do autoincrement values. I saw in the PostgreSQL docs a datatype 'serial', but I.
I am using Hibernate 3.3 and PostgreSQL 8.x and would like to use Hibernate annotations to map an auto-incremented column which is NOT a primary key. It doesn't matter if the column is mapped using SERIAL type or sequences in Postgres as long as it gets auto-incremented by the database and not by Hibernate. I tried the following mappings, but they always generated null orderId. @Column(name = 'orderId', insertable = false) @Generated(GenerationTime.INSERT) //@GeneratedValue(strategy = javax.persistence.GenerationType.AUTO) private Integer orderId; I will appreciate any help with this. The following mapping should work fine: @Column(name = 'orderId') @Generated(GenerationTime.INSERT) private Integer orderId; Note, however, that generated value for freshly saved objects is not available until session is flushed. EDIT: Note that this mapping doesn't affect doesn't make Hibernate to create a column of type serial during schema generation, since Hibernate doesn't know anything about the nature of value generation at the database side. Therefore, if you want Hibernate to create a column with a proper type, you need to specifiy it explicitly: @Column(name = 'orderId', columnDefinition = 'serial') @Generated(GenerationTime.INSERT) private Integer orderId; And on a recent Hibernate version (4.3), you can use this: @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Long orderId.
Hibernate definitely supports this. From the docs: 'Generated properties are properties which have their values generated by the database. Typically, Hibernate applications needed to refresh objects which contain any properties for which the database was generating values. Marking properties as generated, however, lets the application delegate this responsibility to Hibernate. Aplikasi Menggunakan Bahasa Assembly on this page. Essentially, whenever Hibernate issues an SQL INSERT or UPDATE for an entity which has defined generated properties, it immediately issues a select afterwards to retrieve the generated values.'
For properties generated on insert only, your property mapping (.hbm.xml) would look like: For properties generated on insert and update your property mapping (.hbm.xml) would look like: Unfortunately, I don't know JPA, so I don't know if this feature is exposed via JPA (I suspect possibly not) Alternatively, you should be able to exclude the property from inserts and updates, and then 'manually' call session.refresh( obj ); after you have inserted/updated it to load the generated value from the database. This is how you would exclude the property from being used in insert and update statements: Again, I don't know if JPA exposes these Hibernate features, but Hibernate does support them.
