-->
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.  [ 11 posts ] 
Author Message
 Post subject: begginer problem. Dont understand why i cannot add a row...
PostPosted: Fri Aug 26, 2005 6:47 am 
Newbie

Joined: Mon Aug 22, 2005 3:15 am
Posts: 9
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.0

Mapping documents:
<?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>
<!--
Auto-generated mapping file from
the hibernate.org cfg2hbm engine
-->
<class name="common.db.Sport" table="TB_SPORT" schema="dbo" catalog="Common">
<id name="sportId" type="integer">
<column name="sport_id" />
<generator class="assigned" />
</id>
<property name="eventTypeId" type="integer">
<column name="event_type_id" not-null="true" />
</property>
<property name="sportName" type="string">
<column name="sport_name" length="50" not-null="true" />
</property>
<property name="shortName" type="string">
<column name="short_name" length="50" />
</property>
<property name="isActive" type="boolean">
<column name="is_active" not-null="true" />
</property>
</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Session session = common.util.HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
Sport s = new Sport(Integer.getInteger("8"));
s.setSportId(Integer.getInteger("8"));
s.setEventTypeId(Integer.getInteger("2"));
s.setIsActive(Boolean.FALSE);
s.setShortName("TST");
s.setSportName("TEST");
session.save(s);
tx.commit();
common.util.HibernateUtil.closeSession();


Full stack trace of any exception that occurs:
rg.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before
alling save(): common.db.Sport
at org.hibernate.id.Assigned.generate(Assigned.java:32)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventLi
tener.java:85)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(D
faultSaveOrUpdateEventListener.java:184)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSa
eEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrU
dateEventListener.java:173)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventList
ner.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpda
eEventListener.java:69)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:481)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:476)
at common.xml.XMLGenerator.addObject2Database(XMLGenerator.java:221)
at common.xml.XMLGenerator.run(XMLGenerator.java:188)
at ExporterServlet.doGet(ExporterServlet.java:74)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.j
va:252)
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.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(Http11P
otocol.java:744)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.ja
a:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)


Name and version of the database you are using:
Microsoft SQL SERVER 2000


The generated SQL (show_sql=true):
I DO NOT GET ANY IN THE ADD I GET ALOT OF SELECTS BEFORE... BUT NO ADD...


Debug level Hibernate log excerpt:
How do you mean?



can anyone please try to explain to me what is going on... I am setting all the fields that the table has....

kind regards
Jonny


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 26, 2005 9:04 am 
Beginner
Beginner

Joined: Wed Nov 24, 2004 10:54 am
Posts: 48
Your "generator class" in your mapping document is set to "assigned". This is telling Hibernate that you are going to set the ID field in the program before you try and save it. If this is a true identity field in MSSQL, then you should set this to "native" not "assigned".


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 26, 2005 9:07 am 
Beginner
Beginner

Joined: Wed Nov 24, 2004 10:54 am
Posts: 48
Also saw something else. You are using a getInteger("8") method. I think it should be valueOf("8") if you truely trying to set that Interger value to 8.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 29, 2005 2:32 am 
Newbie

Joined: Mon Aug 22, 2005 3:15 am
Posts: 9
thanks for the info, i tried this and it kind of worked... the strange thing is that it is not a identity field, i will assign it myself, i was planning to do it as identity later but i am just testing now.
Though it still says that i am not setting all the fields....


Exception
Hibernate: insert into Common.dbo.TB_SPORT (event_type_id, sport_name, short_name, is_active
) values (?, ?, ?, ?)
org.hibernate.exception.ConstraintViolationException: could not insert: [common.db.TbSport]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:63)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1777
)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:2178
)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:3
4)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEven
tListener.java:240)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.j
ava:160)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventLi
stener.java:95)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(D
efaultSaveOrUpdateEventListener.java:184)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSa
veEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrU
pdateEventListener.java:173)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventList
ener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpda
teEventListener.java:69)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:481)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:476)
at common.xml.XMLGenerator.addObject2Database(XMLGenerator.java:221)
at common.xml.XMLGenerator.run(XMLGenerator.java:188)
at ExporterServlet.doGet(ExporterServlet.java:74)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.j
ava:252)
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.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(Http11P
rotocol.java:744)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.ja
va:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
Caused by: java.sql.SQLException: Cannot insert the value NULL into column 'sport_id', table 'expres
soCommon.dbo.TB_SPORT'; column does not allow nulls. INSERT fails.
at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:364)
at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2754)
at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2195)
at net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:620)
at net.sourceforge.jtds.jdbc.JtdsStatement.processResults(JtdsStatement.java:483)
at net.sourceforge.jtds.jdbc.JtdsStatement.executeSQL(JtdsStatement.java:445)
at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.executeUpdate(JtdsPreparedStatement.java:
402)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1759
)
... 32 more


and the source

Session session = common.util.HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();

Sport s = new Sport();
s.setSportId(Integer.valueOf("8"));
s.setEventTypeId(Integer.valueOf("2"));
s.setSportName("TEST");
s.setShortName("TST");
s.setIsActive(Boolean.FALSE);
session.save(s);
tx.commit();
common.util.HibernateUtil.closeSession();

and mapping
<?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>
<class name="common.db.Status" table="TB_STATUS" schema="dbo" catalog="Common">
<id name="id" type="integer">
<column name="id" />
<generator class="assigned" />
</id>
<property name="sportId" type="integer">
<column name="sport_id" not-null="true" />
</property>
<property name="sportCode" type="character">
<column name="sport_code" length="3" />
</property>
<property name="status" type="string">
<column name="status" length="50" not-null="true" />
</property>
<property name="statustype" type="string">
<column name="statustype" length="50" not-null="true" />
</property>
<property name="sortOrder" type="integer">
<column name="sort_order" not-null="true" />
</property>
<property name="hasResults" type="boolean">
<column name="has_results" not-null="true" />
</property>
</class>
</hibernate-mapping>



it looks pretty much as the same problem but with a different output. THe values never get set...


any help...
kind regards
Jonny


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 29, 2005 2:35 am 
Newbie

Joined: Mon Aug 22, 2005 3:15 am
Posts: 9
oops cpoied the wrong mapping

<hibernate-mapping>
<class name="common.db.Sport" table="TB_SPORT" schema="dbo" catalog="Common">
<id name="sportId" type="integer">
<column name="sport_id" />
<generator class="native" />
</id>
<property name="eventTypeId" type="integer">
<column name="event_type_id" not-null="true" />
</property>
<property name="sportName" type="string">
<column name="sport_name" length="50" not-null="true" />
</property>
<property name="shortName" type="string">
<column name="short_name" length="50" />
</property>
<property name="isActive" type="boolean">
<column name="is_active" not-null="true" />
</property>
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 29, 2005 12:46 pm 
Beginner
Beginner

Joined: Wed Nov 24, 2004 10:54 am
Posts: 48
You have them as not null = true. If you have that, you must set those fields. If those fields can truely be null, then just leave off that setting.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 29, 2005 12:49 pm 
Beginner
Beginner

Joined: Wed Nov 24, 2004 10:54 am
Posts: 48
Also, I have seen this "Convert" error before. In my case the table had not been built yet. Make sure the table is there and with the same name.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 29, 2005 12:53 pm 
Beginner
Beginner

Joined: Wed Nov 24, 2004 10:54 am
Posts: 48
I guess I should have read your post more closely. I have been using Hibernate for about 1.5 years now. I have not seen a mapping file done in this way before. It may in fact be OK, but, here is how mine is done.

Code:
<property name="lastName" column="@CLNAM" type="java.lang.String" />


Try and changing yours to match mine. I do not have a <column> tag at all. Let me know how it works.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 30, 2005 2:43 am 
Newbie

Joined: Mon Aug 22, 2005 3:15 am
Posts: 9
jiseller wrote:
You have them as not null = true. If you have that, you must set those fields. If those fields can truely be null, then just leave off that setting.


This is the create table of the TB_SPORT
CREATE TABLE [dbo].[TB_SPORT] (
[sport_id] [int] NOT NULL ,
[event_type_id] [int] NOT NULL ,
[sport_name] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[short_name] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[is_active] [bit] NOT NULL
) ON [PRIMARY]

which says that all columns except short_name must not be null...

/Jonny


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 30, 2005 2:45 am 
Newbie

Joined: Mon Aug 22, 2005 3:15 am
Posts: 9
jiseller wrote:
Also, I have seen this "Convert" error before. In my case the table had not been built yet. Make sure the table is there and with the same name.


If i understand you right you mean the table in the database or the class file...

Anyway table is there
Class file is there too..

So is the problem ;)

/Jonny


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 30, 2005 2:46 am 
Newbie

Joined: Mon Aug 22, 2005 3:15 am
Posts: 9
jiseller wrote:
I guess I should have read your post more closely. I have been using Hibernate for about 1.5 years now. I have not seen a mapping file done in this way before. It may in fact be OK, but, here is how mine is done.

Code:
<property name="lastName" column="@CLNAM" type="java.lang.String" />


Try and changing yours to match mine. I do not have a <column> tag at all. Let me know how it works.


Well the mapping was done by eclipse so it should be correct...
Tried your way too, still get the same error...

/Jonny


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