reset password
Author Message
abhishek_sharma
Posts: 79
Posted 12:45 May 05, 2009 |

Hi all,

Please help me to figure it out, how we can use auto_increment in PostGreSql?

I need tht to maintain blogid.

 

 

yurimuradyan
Posts: 42
Posted 13:20 May 05, 2009 |
abhishek_sharma wrote:

Hi all,

Please help me to figure it out, how we can use auto_increment in PostGreSql?

I need tht to maintain blogid.

 

<id name="id" column="id">

<generator class="increment">

</id>

Hibernate will automatically insert it for you.

 

Last edited by yurimuradyan at 13:21 May 05, 2009.
cysun
Posts: 2935
Posted 15:25 May 05, 2009 |

What's wrong with <generator class="native" />?

yurimuradyan
Posts: 42
Posted 20:04 May 05, 2009 |
cysun wrote:

What's wrong with <generator class="native" />?

When I used the "native" method, it would create ids like 20060004 or some other very large numbers and I would get a ArrayOutOfBounds error. Changing it to "increment" helped me solve that issue. Was I doing something wrong in the first place?

abhishek_sharma
Posts: 79
Posted 20:19 May 05, 2009 |

Even when I am trying to insert an entry to create a New Blog without explicitly mentioning blogid field, database giving an error "blogid value cannot be null",

like

insert into blog (strblogtitle,strblogdescription,strimgbanner,intblogentries,lastblogentry,blogowner) values ('test','test','test',0,0,12345)

error:


ERROR:  null value in column "intblogid" violates not-null constraint

********** Error **********

ERROR: null value in column "intblogid" violates not-null constraint
SQL state: 23502

I guess since intblogid is autoincrement so I  should not get this error.

Please help if I am doing anythng wrong

Thanks

cysun
Posts: 2935
Posted 20:40 May 05, 2009 |
yurimuradyan wrote:
cysun wrote:

What's wrong with <generator class="native" />?

When I used the "native" method, it would create ids like 20060004 or some other very large numbers and I would get a ArrayOutOfBounds error. Changing it to "increment" helped me solve that issue. Was I doing something wrong in the first place?

Yes. Id's are identifiers, i.e. you shouldn't use them as array indexes.

cysun
Posts: 2935
Posted 20:47 May 05, 2009 |
abhishek_sharma wrote:

Even when I am trying to insert an entry to create a New Blog without explicitly mentioning blogid field, database giving an error "blogid value cannot be null",

like

insert into blog (strblogtitle,strblogdescription,strimgbanner,intblogentries,lastblogentry,blogowner) values ('test','test','test',0,0,12345)

error:


ERROR:  null value in column "intblogid" violates not-null constraint

********** Error **********

ERROR: null value in column "intblogid" violates not-null constraint
SQL state: 23502

I guess since intblogid is autoincrement so I  should not get this error.

Please help if I am doing anythng wrong

Thanks

You are doing something wrong if you try to execute an "insert" statement yourself. You should do something like "getHibernateTemplate().saveOrUpdate(blog)".

With saveOrUpdate(), a) if the object id field is null, Hibernate knows it's a new object, so it creates a new id (using the id generator), then perform an insert statement; b) if the object id is not null, Hibernate knows it's an existing object, so it performs an update statement. You should not run any SQL statement directly.

There are plenty of DaoImpl classes in CSNS. See how those are implemented.

Last edited by cysun at 20:48 May 05, 2009.