-->
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: many-to-one
PostPosted: Wed Jul 07, 2004 4:27 am 
Newbie

Joined: Mon Jul 05, 2004 5:49 am
Posts: 6
We have Object(1) Linked to another Object (2). both of them are not in DB. they are only in operation memory.

I want save 1 to DB , together with 2.

but when i save 1-st Object to DB, 2nd Object doesnt saved.

please tell me how to do this

Source Cat:

public class Cat {

int cat_id;
int age;
String nick;
}

<hibernate-mapping>
<class name="test.hibernate.Cat" table="cat">
<id name="cat_id" type="integer" unsaved-value="null" >
<column name="cat_id" sql-type="integer"
not-null="true"/>
<generator class="vm"/>
</id>
<property name="age">
<column name="age" sql-type="integer" not-null="true"/>
</property>

<property name="nick">
<column name="nick" sql-type="varchar" not-null="true"/>
</property>

</class>

</hibernate-mapping>

Source Dog:


public class Dog {

int dog_id;
int age;
String nick;
Cat cat;
}

<hibernate-mapping>
<class name="test.hibernate.Dog" table="dog">
<id name="dog_id" type="integer" unsaved-value="null" >
<column name="dog_id" sql-type="integer"
not-null="true"/>
<generator class="vm"/>
</id>
<property name="age">
<column name="age" sql-type="integer" not-null="true"/>
</property>

<property name="nick">
<column name="nick" sql-type="varchar" not-null="true"/>
</property>

<many-to-one name="cat" class="test.hibernate.Cat"
cascade="all"column="cat_id" />

</class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 07, 2004 5:21 am 
Newbie

Joined: Mon Jul 05, 2004 5:49 am
Posts: 6
Cat must saves, because of cascade="all", but the program throws exception


C:\j2sdk1.4.2_04\bin\javaw.exe -classpath C:\j2sdk1.4.2_04\jre\lib\charsets.jar;C:\j2sdk1.4.2_04\jre\lib\jce.jar;C:\j2sdk1.4.2_04\jre\lib\jsse.jar;C:\j2sdk1.4.2_04\jre\lib\plugin.jar;C:\j2sdk1.4.2_04\jre\lib\rt.jar;C:\j2sdk1.4.2_04\jre\lib\sunrsasign.jar;C:\j2sdk1.4.2_04\jre\lib\ext\dnsns.jar;C:\j2sdk1.4.2_04\jre\lib\ext\ldapsec.jar;C:\j2sdk1.4.2_04\jre\lib\ext\localedata.jar;C:\j2sdk1.4.2_04\jre\lib\ext\sunjce_provider.jar;D:\progs\hello_hibernate\classes;D:\progs\hello_hibernate\lib;D:\progs\hello_hibernate\lib\hibernate2.jar;D:\progs\hello_hibernate\lib\oracle.jar;D:\progs\hello_hibernate\lib\dom4j-1.4.jar;D:\progs\hello_hibernate\lib\ehcache-0.7.jar;D:\progs\hello_hibernate\lib\cglib-full-2.0.1.jar;D:\progs\hello_hibernate\lib\commons-logging-1.0.3.jar;D:\progs\hello_hibernate\lib\commons-collections-2.1.jar;D:\progs\hello_hibernate\lib\jta.jar;D:\progs\hello_hibernate\lib\odmg-3.0.jar;D:\progs\hello_hibernate\lib\log4j.jar test.hibernate.Dog
16:01:53,668 INFO Environment:462 - Hibernate 2.1.4
16:01:53,748 INFO Environment:496 - loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver, hibernate.cglib.use_reflection_optimizer=true, hibernate.cache.provider_class=net.sf.ehcache.hibernate.Provider, hibernate.cache.use_query_cache=true, hibernate.max_fetch_depth=1, hibernate.dialect=net.sf.hibernate.dialect.Oracle9Dialect, hibernate.jdbc.use_streams_for_binary=true, hibernate.jdbc.batch_size=0, hibernate.query.substitutions=true 1, false 0, yes 'Y', no 'N', hibernate.proxool.pool_alias=pool1, hibernate.connection.username=HLYSTOV_HIBERNATE, hibernate.cache.region_prefix=hibernate.test, hibernate.connection.url=jdbc:oracle:thin:@cmt:1521:esmt, hibernate.connection.password=HLYSTOV_HIBERNATE, hibernate.connection.pool_size=1}
16:01:53,778 INFO Environment:521 - using java.io streams to persist binary types
16:01:53,818 INFO Environment:522 - using CGLIB reflection optimizer
16:01:54,038 INFO Configuration:347 - Mapping resource: test/hibernate/Dog.hbm.xml
16:01:56,502 INFO Binder:229 - Mapping class: test.hibernate.Dog -> dog
16:01:57,493 INFO Configuration:347 - Mapping resource: test/hibernate/Cat.hbm.xml
16:01:57,784 INFO Binder:229 - Mapping class: test.hibernate.Cat -> cat
16:01:57,794 INFO Configuration:613 - processing one-to-many association mappings
16:01:57,794 INFO Configuration:622 - processing one-to-one association property references
16:01:57,794 INFO Configuration:647 - processing foreign key constraints
16:01:58,234 INFO Dialect:82 - Using dialect: net.sf.hibernate.dialect.Oracle9Dialect
16:01:58,254 INFO SettingsFactory:58 - Maximim outer join fetch depth: 1
16:01:58,254 INFO SettingsFactory:62 - Use outer join fetching: true
16:01:58,294 INFO DriverManagerConnectionProvider:42 - Using Hibernate built-in connection pool (not for production use!)
16:01:58,304 INFO DriverManagerConnectionProvider:43 - Hibernate connection pool size: 1
16:01:58,755 INFO DriverManagerConnectionProvider:77 - using driver: oracle.jdbc.driver.OracleDriver at URL: jdbc:oracle:thin:@cmt:1521:esmt
16:01:58,765 INFO DriverManagerConnectionProvider:78 - connection properties: {user=HLYSTOV_HIBERNATE, password=HLYSTOV_HIBERNATE}
16:01:58,795 INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended)
16:02:00,958 INFO SettingsFactory:102 - Use scrollable result sets: true
16:02:00,958 INFO SettingsFactory:105 - Use JDBC3 getGeneratedKeys(): false
16:02:00,968 INFO SettingsFactory:108 - Optimize cache for minimal puts: false
16:02:00,968 INFO SettingsFactory:117 - Query language substitutions: {no='N', true=1, yes='Y', false=0}
16:02:00,968 INFO SettingsFactory:128 - cache provider: net.sf.ehcache.hibernate.Provider
16:02:00,998 INFO Configuration:1093 - instantiating and configuring caches
16:02:01,609 INFO SessionFactoryImpl:119 - building session factory
16:02:03,101 INFO SessionFactoryObjectFactory:82 - no JNDI name configured
16:02:03,111 INFO UpdateTimestampsCache:35 - starting update timestamps cache at region: net.sf.hibernate.cache.UpdateTimestampsCache
16:02:03,191 WARN Configurator:123 - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/D:/progs/hello_hibernate/lib/ehcache-0.7.jar!/ehcache-failsafe.xml
16:02:03,272 WARN Plugin:95 - Could not find configuration for net.sf.hibernate.cache.UpdateTimestampsCache. Configuring using the defaultCache settings.
16:02:03,352 INFO QueryCache:39 - starting query cache at region: net.sf.hibernate.cache.QueryCache
16:02:03,362 WARN Plugin:95 - Could not find configuration for net.sf.hibernate.cache.QueryCache. Configuring using the defaultCache settings.
[-500] Bunny, 4 years. Cat - [50] Mistery, 1 years


16:02:04,063 ERROR SessionImpl:2375 - Could not synchronize database state with session
net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)
at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:689)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:642)
at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2414)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2368)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2236)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at test.hibernate.Dog.saveDog(Dog.java:72)
at test.hibernate.Dog.main(Dog.java:114)
16:02:04,113 WARN SessionImpl:3386 - afterTransactionCompletion() was never called
16:02:04,113 WARN SessionImpl:3396 - unclosed connection
net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)
at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:689)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:642)
at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2414)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2368)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2236)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at test.hibernate.Dog.saveDog(Dog.java:72)
at test.hibernate.Dog.main(Dog.java:114)
Process terminated with exit code 0


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 07, 2004 3:22 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
Your IDs are set up with an unsaved value as NULL. However your id datatypes are primitives (int) which will never in a million years be null :)

They will default to 0, so in essence you are telling hibernate to update your Cat and Dog since you're telling them by your mapping that they're already saved.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 07, 2004 9:01 pm 
Newbie

Joined: Mon Jul 05, 2004 5:49 am
Posts: 6
thank a lot!!!!!!!!!!!!! i've been beeting this problem for two days you save me!


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.