views:

518

answers:

1

I have a postgresql table in which I have declared a column 'inv_seq' which is declared as 'SERIAL'.

I have a Hibernate bean class to map the table. All the other columns are read properly except this column. here is the declaration in the Hibernate bean class:

....


  ....


        @GeneratedValue(strategy=javax.persistence.GenerationType.AUTO)
        @Column(name = "inv_seq")
        public Integer getInvoiceSeq() {
            return invoiceSeq;
        }

         public void setInvoiceSeq(Integer invoiceSeq) {
        this.invoiceSeq = invoiceSeq;
    }

    ....

    .....

Is the declaration correct? I am able to see the serialized numbers generated by the column in the database,but I am not able to access them in the java class.

Please Help.

Thanks, Aditya

A: 

Depending on your situation, this may not work. There is a bug opened against Hibernate that documents this behavior.

http://opensource.atlassian.com/projects/hibernate/browse/HHH-4159

If you are open to using a mapping file instead of annotations, I have been able to recreate the issue (NULL's in SERIAL columns that are not part of the primary key). Using the "generated" attribute of the property element causes Hibernate to re-read the row after an insert to pick up the generated column value:

<class name="Ticket" table="ticket_t">
    <id name="id" column="ticket_id">
        <generator class="identity"/>
    </id>
    <property name="customerName" column="customer_name"/>
    <property name="sequence" column="sequence" generated="always"/>
</class>

KeithL
But I already have the @Id tag for the primaryKey field in the bean class.
Aditya R
@Column(name = "oid" , nullable = false ) @Id @GeneratedValue(strategy=javax.persistence.GenerationType.AUTO) public Long getOid() { return oid; }
Aditya R
To clarify - there is more than one generated column in the table, and column inv_seq is NOT part of the primary key?
KeithL
Yes thats right.
Aditya R
Try using the GenerationType.IDENTITY strategy. Also, what value does the Java object return when you call getInvoiceSeq()?
KeithL
getInvoiceSeq() returns a null value. I will try the above method which you suggested and get back to you. Thanks.
Aditya R
Sorry it is returning null even when I use this:@Column(name = "inv_seq") @GeneratedValue(strategy=GenerationType.IDENTITY) public Integer getInvoiceSeq() { return invoiceSeq; }
Aditya R
Are you using the correct dialect in your Hibernate configuration file? Since SERIAL is specific to PostgreSQL it's possible that Hibernate is not recognizing that column type and ignoring it.Syntax is:<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
KeithL
I was able to recreate your issue, and have updated my answer.
KeithL
Thanks a lot Keith. I don't think I can use the mapping file instead of the annotation, since we are using annotation throughout the application. I will consult my manager and get back.
Aditya R