-->
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.  [ 10 posts ] 
Author Message
 Post subject: using sequence
PostPosted: Thu Apr 26, 2007 2:00 am 
Newbie

Joined: Tue Apr 24, 2007 5:57 am
Posts: 9
I have created a small program in hibernate.I am actually using p[ostgres database.

While running the program i get the following exceptiom :
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not get next sequence value
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
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:99)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
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:535)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
at com.hibernate.util.ApplicationManager.WriteData(ApplicationManager.java:41)
at com.hibernate.util.ApplicationManager.main(ApplicationManager.java:21)
Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near "value"
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:1548)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1316)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:191)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:452)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:351)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:255)
at org.hibernate.id.SequenceGenerator.generate(SequenceGenerator.java:75)
... 11 more


I have created a sequence and also included it in the hibernate configuration file.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 26, 2007 2:26 am 
Newbie

Joined: Thu Apr 19, 2007 1:46 am
Posts: 10
Hi ,
I too use the sequence for id generation but in oracle.
Can you post your mapping file so that it is more clear

This is the way i use it in my mapping file.
I hope this helps:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="domainobject">

<class name="RecommendedAction" table="RECOMMENDED_ACTIONS">
<id name="id" column="id" type="integer">
<generator class="native">
<param name="sequence">recommended_actions_seq</param>
</generator>
</id>
<property name="name" type="string" column="name"/>
<property name="description" type="string" column="description"/>
<many-to-one name="automatedScript" column="script_id" outer-join = "true" class="AutomatedScript" />
</class>

<query name="RecommendedAction.getAllRecommendedActions">
from RecommendedAction
</query>
</hibernate-mapping>


Last edited by gurpreet on Thu Apr 26, 2007 4:52 am, edited 2 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 26, 2007 2:29 am 
Newbie

Joined: Tue Apr 24, 2007 5:57 am
Posts: 9
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.hibernate.beans.Event" table="Event">
<id name="id" column="event_id">
<generator class="sequence">
<param name="sequence">event_seq</param>
</generator>
</id>
<property name="date" type="timestamp" column="date_val"/>
<property name="title"/>

</class>
</hibernate-mapping>


This is my mapping file.But if i use "native" instead of sequence then i get the following exception :

Exception in thread "main" org.hibernate.exception.ConstraintViolationException: could not insert: [com.hibernate.beans.Event]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.id.insert.AbstractSelectingDelegate.performInsert(AbstractSelectingDelegate.java:40)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2158)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2638)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:48)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:298)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
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:535)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
at com.hibernate.util.ApplicationManager.WriteData(ApplicationManager.java:41)
at com.hibernate.util.ApplicationManager.main(ApplicationManager.java:21)
Caused by: org.postgresql.util.PSQLException: ERROR: null value in column "event_id" violates not-null constraint
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:1548)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1316)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:191)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:452)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:351)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:305)
at org.hibernate.id.insert.AbstractSelectingDelegate.performInsert(AbstractSelectingDelegate.java:33)
... 17 more


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 26, 2007 3:11 am 
Newbie

Joined: Tue Apr 24, 2007 5:57 am
Posts: 9
Hi actually in my hibernate.cfg.xml i changed the dialect property from org.hibernate.dialect.HSQLDialect to org.hibernate.dialect.PostgreSQLDialec

When i run the program now, i get the following error


Exception in thread "main" org.hibernate.HibernateException: identifier of an instance of com.hibernate.beans.Event was altered from 1 to 0
at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:58)
at org.hibernate.event.def.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:157)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:113)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:196)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.hibernate.util.ApplicationManager.WriteData(ApplicationManager.java:46)
at com.hibernate.util.ApplicationManager.main(ApplicationManager.java:21)


Can anyone put comments on this.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 26, 2007 4:39 am 
Regular
Regular

Joined: Mon Jun 12, 2006 5:33 am
Posts: 63
Hi ksriram,

what is the code that caused the exception? And if you want more feedback, use the code tag when sending code or config stuff. Or it's hard to read. e.g:
Code:
   public static void main(String[] args) {
      System.out.println("Hello World");
      

   }


chucky

_________________
chucky

Don't forget to rate if this helps
-----------------------------------


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 26, 2007 4:49 am 
Newbie

Joined: Thu Apr 19, 2007 1:46 am
Posts: 10
gurpreet wrote:
Hi ,
I too use the sequence for id generation but in oracle.
Can you post your mapping file so that it is more clear

This is the way i use it in my mapping file.
I hope this helps:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="domainobject">

<class name="domainobject.RecommendedAction" table="RECOMMENDED_ACTIONS">
<id name="id" column="id" type="integer">
<generator class="native">
<param name="sequence">recommended_actions_seq</param>
</generator>
</id>
<property name="name" type="string" column="name"/>
<property name="description" type="string" column="description"/>
<many-to-one name="automatedScript" column="script_id" outer-join = "true" class="AutomatedScript" />
</class>

<query name="RecommendedAction.getAllRecommendedActions">
from RecommendedAction
</query>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 27, 2007 12:54 am 
Newbie

Joined: Tue Apr 24, 2007 5:57 am
Posts: 9
I actually resolved the problem.

1) I changed my the dialect from org.hibernate.dialect.HSQLDialect to org.hibernate.dialect.PostgreSQLDialect.

This was one of the mistakes.I learn t that we should always use the underlying database's dialect.

2) The problem was in my POJO class.I had declared a files as int rather than Integer.

In Hibernate, the POJO classes filed/variables should have only wrapper class.
But in a normal bean file we can have primitive data type.

I want to know the reason why a Wrapper class is used instead of primitive data type in the POJO classes.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 27, 2007 5:20 am 
Regular
Regular

Joined: Mon Jun 12, 2006 5:33 am
Posts: 63
Hi ksriram,

wrapper classes are preferred because fields can be nullable. You achieve that with primitives.

_________________
chucky

Don't forget to rate if this helps
-----------------------------------


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 27, 2007 5:21 am 
Regular
Regular

Joined: Mon Jun 12, 2006 5:33 am
Posts: 63
Sorry,
you CAN'T achieve that with primitives.

_________________
chucky

Don't forget to rate if this helps
-----------------------------------


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 27, 2007 6:20 am 
Senior
Senior

Joined: Sat Apr 21, 2007 11:01 pm
Posts: 144
Hi, you might find this thread usefull: http://forum.hibernate.org/viewtopic.php?t=973823
The issues Monkey and I were having was caused by a problem with sequence/native.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 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.