-->
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.  [ 5 posts ] 
Author Message
 Post subject: Please help with mapping.
PostPosted: Sat Aug 04, 2007 8:55 am 
Newbie

Joined: Mon Jul 23, 2007 10:17 am
Posts: 12
Location: www.ultrasharpware.com
I'm new to Hibernate and maybe my problem is quite simple, but steal I have problems with mapping.
What I want to do is to have two links for different objects from one table.
Lets say I have a Pair and a Person classes. Every Pair can have 1(until second person doesn't connect) or 2 Persons or even more(It would not be a pair then =)). Lets say it could have a set of objects. And I would like a Peson class to have a Pair-class link.

I've thought of having following tables:

person(person_id, pair_id, person_name);
pair(pair_id, ... another pair info);

So this two tables would be joined by pair_id.
I've tryied to map this:

Code:
        <class name="Pair" table="pair">

                <id name="id" type="long" unsaved-value="null">
                <column name="id" sql-type="int(11)" not-null="true"/>
                <generator class="native"/>
                </id>

                <set name="person" table="person">
                        <key column="pair_id" not-null="true"/>
                        <one-to-many class="Person"/>
                </set>

        </class>

        <class name="Person" table="person">

                <id name="id" type="long" unsaved-value="null">
                <column name="id" sql-type="int(11)" not-null="true"/>
                <generator class="native"/>
                </id>

                <property name="name">
                <column name="name" sql-type="char(255)" not-null="true"/>
                </property>

                <one-to-one name="pair" property-ref="person"/>
</class>


When using this mapping I can't save objects
(Person p= new Person();
Set s = new HashSet();
s.add(p);
Pair pair = new Pair();
pair.setPersons(s);
session.save(pair);
)
I receive:
Code:
splayer Error: 500 Location: /fightlistInternal Servlet Error:org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:61)
at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:46)
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:68)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:145)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
at $Proxy0.flush(Unknown Source)
at fightclub.FightListServlet.doGet(FightListServlet.java:72)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:405)
at org.apache.tomcat.core.Handler.service(Handler.java:287)
at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:812)
at org.apache.tomcat.core.ContextManager.service(ContextManager.java:758)
at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:213)
at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416)
at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:501)
at java.lang.Thread.run(Thread.java:534)



But when I've manually inserted data into tables, I could read them...
What am I doing wrong?
Thank's for the help!

_________________
kodlan
http://www.UltraSharpWare.com
http://www.UltraSharpWare.com/blog


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 05, 2007 1:08 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Please post the generated SQL.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 06, 2007 9:18 am 
Newbie

Joined: Mon Jul 23, 2007 10:17 am
Posts: 12
Location: www.ultrasharpware.com
thatmikewilliams wrote:
Please post the generated SQL.

You mean log4j output?
Code:
insert into pair (participants) values (?)
update person set pair_id=? where id=?

and than

16:11:26,476 DEBUG AbstractBatcher:484 - preparing statement
16:11:26,476 DEBUG AbstractBatcher:44 - Executing batch size: 1
16:11:26,526 ERROR AbstractBatcher:51 - Exception executing batch:
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
...
16:11:26,556 ERROR AbstractFlushingEventListener:301 - Could not synchronize database state with session
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1


PS: participants (int(11)) is the number of persons in pair.

_________________
kodlan
http://www.UltraSharpWare.com
http://www.UltraSharpWare.com/blog


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 06, 2007 9:27 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Yes, but set the hibernate.show_sql property to true first. That way we can see the SQL statements hibernate generates.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 06, 2007 11:17 am 
Newbie

Joined: Mon Jul 23, 2007 10:17 am
Posts: 12
Location: www.ultrasharpware.com
thatmikewilliams wrote:
Yes, but set the hibernate.show_sql property to true first. That way we can see the SQL statements hibernate generates.

It's working!!
My fault was (I think it was=) that I wasn't saving newly created persons. I was doing only this:

Code:
Person p = new Person();
pair.getPerson().add(p);

session.save(pair);


I think session.save(p); helped. I'm not so sure because I've made a lot of changes to make that work =).

2 thatmikewilliams: Thanks for trying to help!

_________________
kodlan
http://www.UltraSharpWare.com
http://www.UltraSharpWare.com/blog


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