-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Problem generating automatic ID - hibernate to postgres db
PostPosted: Fri Feb 17, 2006 10:41 am 
Newbie

Joined: Wed Feb 01, 2006 10:33 am
Posts: 15
Hi,

Im trying to insert a new object (user) using hibernate and a postgresql server.

As seen on examples, I tried to make the id SERIAL in the postgres database, but was not allowed to do this operation. I changed it to integer with the following parameter: nextval('public."Users_userid_seq"'::text)

The hibernate mapping file did NOT include the <generator class> tag then. However, this did not work. It sent the new user to the database without generating a new ID, and the database tried to store with primary key = 0.

Now I tried to add the <generator class> tag to see if this could work. It didn't. All the documentation included is from this last attempt.

Does anyone know how to solve this or can link to any relevant documentation?

Thanks in advance, typofrank


Hibernate version: 3.0

Mapping documents:

<hibernate-mapping>
<class name="ValueObjects.Users" table="users">
<id name="userID" column="userid" type="java.lang.Integer">
<generator class="sequence">
<param name="sequence">users_userid_seq</param>
</generator>
</id>

<property name="firstName" column="firstname" type="string" />
<property name="lastName" column="lastname" type="string" />
<property name="groupID" column="groupid" type="integer" />
<property name="userName" column="username" type="string" />
<property name="userPass" column="userpass" type="string" />
<property name="address" column="address" type="string" />
<property name="homePhone" column="homephone" type="string" />
<property name="cellPhone" column="cellphone" type="string" />
<property name="email" column="email" type="string" />
<property name="lastLogin" column="lastlogin" type="string" />
<property name="lastIP" column="lastip" type="string" />
<property name="isActive" column="isactive" type="integer" />
<property name="position" column="position" type="string" />
<property name="language" column="language" type="string" />
<property name="isDeleted" column="isdeleted" type="integer" />
<property name="postNo" column="postno" type="integer" />
<property name="postRegion" column="postregion" type="string" />
<property name="workPhone" column="workphone" type="string" />

</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():

The createUser method:

public void createUser( Users user ){

try {
// Get session from session factory
Session session = HibernateSessionFactory.currentSession();

// Starting a new transaction before interacting with the database
Transaction transaction = session.beginTransaction();

// Tell hibernate to save to database
session.save( user );

// Commit to save changes
transaction.commit();

// Closing session
HibernateSessionFactory.closeSession();

} catch (Exception ex){
System.out.println("EXCEPTION! An Exception occured in class HandleUsersController method createUser()");
ex.printStackTrace();
}

Full stack trace of any exception that occurs:

5:31:57,060 ERROR [JDBCExceptionReporter] ERROR: relation "users_userid_seq" does not exist
15:31:57,061 INFO [STDOUT] EXCEPTION! An Exception occured in class HandleUsersController method createUser()
15:31:57,064 INFO [STDOUT] org.hibernate.exception.SQLGrammarException: could not get next sequence value
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:65)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.id.SequenceGenerator.generate(SequenceGenerator.java:96)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:91)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:524)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:514)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:510)
at HandleUsers.HandleUsersController.createUser(HandleUsersController.java:94)
at org.apache.jsp.handleuserscreate_jsp._jspService(org.apache.jsp.handleuserscreate_jsp:249)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:159)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:59)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
at java.lang.Thread.run(Thread.java:595)
Caused by: java.sql.SQLException: ERROR: relation "users_userid_seq" does not exist
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:1482)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1267)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:186)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:392)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:330)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:240)
at org.hibernate.id.SequenceGenerator.generate(SequenceGenerator.java:75)
... 36 more


Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 17, 2006 1:11 pm 
Regular
Regular

Joined: Tue Nov 29, 2005 12:31 pm
Posts: 75
Hi,

1.

nextval('public."Users_userid_seq"'::text)
<param name="sequence">users_userid_seq</param>

Case sensitive issue ?

2.

Try using:

<generator class="increment" />

If it's still not working, post more details.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 22, 2006 6:58 am 
Newbie

Joined: Wed Feb 01, 2006 10:33 am
Posts: 15
Thanks, helped us again :-)

We actually changed both. The first one alone was not enough.

Do you / any of you by any chance know how we can get the primary key in return when using session.save()

something like: (psudo code)

Integer userID = session.save().returnPrimaryKey();


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 23, 2006 9:53 am 
Regular
Regular

Joined: Tue Nov 29, 2005 12:31 pm
Posts: 75
Hi,

session.save() is returning a Serializable object reprezenting the primary key of the object you just saved.

In your case:

Integer userID = new Integer(hibernateSession.save(yourObject).toString());


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.