-->
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.  [ 2 posts ] 
Author Message
 Post subject: problem with one-to-one and keys
PostPosted: Tue Sep 05, 2006 10:46 am 
Newbie

Joined: Tue Sep 05, 2006 9:40 am
Posts: 1
Hi,
I have a simple example but I can't get through it.

Hibernate version:
3.1.3

The tables:
Code:
USERS
-------------
User_ID [PK]
login
password

and
Code:
CLIENTS
-------------
Client_ID [PK]
User_ID [FK]
name


Mapping documents:
Main fragments of .hbm.xml files are
Code:
<class name="Users" table='"USERS"'>
   <id name="userid" column='"User_ID"' type="java.lang.Integer">
      <generator class="sequence">
         <param name="sequence">"USERS_User_ID_seq"</param>
      </generator>
   </id>
   <property name="login" column="login" not-null="true" type="java.lang.String" />
   <property name="password" column="password" not-null="true" type="java.lang.String" />
   <one-to-one name="client" class="Clients" constrained="false"/>
</class>

and
Code:
<class name="Clients" table='"CLIENTS"'>
   <id name="clientid" column='"Client_ID"' type="java.lang.Integer">
      <generator class="foreign">
         <param name="property">users</param>
      </generator>
   </id>
   <one-to-one name="users" class="Users" constrained="true"/>
   <property name="name" column='"name"' type="java.lang.String"  not-null="true" />
</class>


Full stack trace of any exception that occurs:
Quote:
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:63)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:181)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:226)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:136)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:324)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at Clients.saveOrUpdate(Clients.java:53)
at CreatingAction.execute(CreatingAction.java:26)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
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.java: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:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Unknown Source)
Caused by: java.sql.BatchUpdateException: Zadanie wsadowe 0 insert into "CLIENTS" ("name", "Client_ID") values (asdf, 23) zostało przerwane. Wywołaj getNextException by poznać przyczynę.
at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2512)
at org.postgresql.core.v3.QueryExecutorImpl$1.handleError(QueryExecutorImpl.java:399)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1310)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:347)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2574)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:174)
... 29 more


Name and version of the database you are using:
PostgreSQL 8.1.4-1

Debug level Hibernate log excerpt:
I've received following:
Quote:
Hibernate: select nextval ('"USERS_User_ID_seq"')
Hibernate: insert into "USERS" (login, password, "User_ID") values (?, ?, ?)
Hibernate: insert into "CLIENTS" ("name", "Client_ID") values (?, ?)
111130 [http-8080-Processor25] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 0, SQLState: null
111130 [http-8080-Processor25] ERROR org.hibernate.util.JDBCExceptionReporter - Zadanie wsadowe 0 insert into "CLIENTS" ("name", "Client_ID") values (asdf, 23) zostało przerwane. Wywołaj getNextException by poznać przyczynę.
111130 [http-8080-Processor25] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 0, SQLState: 23502
111130 [http-8080-Processor25] ERROR org.hibernate.util.JDBCExceptionReporter - ERROR: null value in column "User_ID" violates not-null constraint
111140 [http-8080-Processor25] ERROR org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session

After creating a new user I have a new one in the table USERS but CLIENTS isn't updated. User_ID is "23" like Client_ID in logs mentioned above. I wonder if I made some mistakes in my .hbm.xml files. Can somebody tell me are they properly or do I misunderstand the idea of one-to-one association? Or maybe I should look for mistakes in .java files when I update the db?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 06, 2006 1:02 pm 
Regular
Regular

Joined: Mon Jun 13, 2005 12:38 pm
Posts: 56
Location: Austin, TX
The first thing I see is the id's are mapped differently for both cases. I would suggest changing the client to use a sequence.

You might want to try something like:

Code:
<class name="Users" table='"USERS"'>
...
<one-to-one name="client" class=clients" column="User_ID" constrained="false" property-ref="users"/>
...
</class>


Hope this helps,

_________________
Michael Masters


Don't forget to rate.


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