-->
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: Cascade Saving Issues
PostPosted: Thu Apr 01, 2004 6:44 am 
Newbie

Joined: Thu Apr 01, 2004 6:16 am
Posts: 2
I am having problems with cascading of save to array types, using the attacked code(see below): I am using Hypersonic, Hibernate is creating the following error:

Caused by: 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.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:538)
at net.sf.hibernate.impl.ScheduledCollectionRecreate.execute(ScheduledCollectionRecreate.java:23)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2382)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2339)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2204)


Can somebody point me in the right direction?

Thanks


[code]package hibtest;

public class TestTwo {

private Long id;

public TestTwo(Long id)
{
this.id = id;
}

/**
*
*/
public TestTwo() {
super();
}

/**
* @return
*/
public Long getId() {
return id;
}

/**
* @param long1
*/
public void setId(Long long1) {
id = long1;
}


/**
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object obj) {
if(obj == this)
{
return true;
}
else
{
if(obj instanceof TestTwo)
{
return this.id.equals(((TestTwo)obj).id);
}
}
return false;
}

/**
* @see java.lang.Object#hashCode()
*/
public int hashCode() {
return this.id.hashCode();
}


}
[/code]
[code]
package hibtest;


public class TestRel {

private Long id;
private TestTwo[] related;

public TestRel(Long id,TestTwo[] related)
{
this.id = id;
this.related = related;
}

/**
*
*/
public TestRel() {
super();
}


/**
* @return
*/
public Long getId() {
return id;
}

/**
* @return
*/
public TestTwo[] getRelated() {
return related;
}

/**
* @param long1
*/
public void setId(Long long1) {
id = long1;
}

/**
* @param twos
*/
public void setRelated(TestTwo[] twos) {
related = twos;
}




/**
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object obj) {
if(obj == this)
{
return true;
}
else
{
if(obj instanceof TestRel)
{
return this.id.equals(((TestRel)obj).id);
}
}
return false;
}

/**
* @see java.lang.Object#hashCode()
*/
public int hashCode() {
return this.id.hashCode();
}

}
[/code][code]
public void testSimple() throws Exception
{
TestRel tr = new TestRel(new Long(-1), new TestTwo[]{new TestTwo(new Long(-1))});
Session s = factory.openSession();
Transaction tx = null;
ClassMetadata cme = factory.getClassMetadata(o.getClass());
Serializable ser = cme.getIdentifier(o);
try {
tx = s.beginTransaction();
Object obj = s.get(o.getClass(), ser);
if (obj == null) {
if (log.isDebugEnabled()) {

log.debug("Adding object to persistent cache " + o);
}
s.save(o);
}
s.flush();
tx.commit();
} catch (Exception e) {
if (tx != null)
tx.rollback();
throw e;
} finally {
s.close();
}
}
[/code]
[code]<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping default-cascade="save-update" auto-import="false">

<class name="hibtest.TestRel" mutable="true" polymorphism="implicit" dynamic-update="false" dynamic-insert="false" batch-size="1" select-before-update="false" >
<id name="id">
<generator class="assigned"/>
</id>

<array name="related" lazy="false" inverse="false" cascade="all" outer-join="true">
<key column="related_id"/>
<one-to-many class="hibtest.TestTwo"/>
<index column="index" type="java.lang.String"/>
</array>

</class>

<class name="hibtest.TestTwo" mutable="true" polymorphism="implicit" dynamic-update="false" dynamic-insert="false" batch-size="1" select-before-update="false" >
<id name="id">
<generator class="assigned"/>
</id>
</class>

</hibernate-mapping>[/code]


Top
 Profile  
 
 Post subject: Further issue
PostPosted: Thu Apr 01, 2004 9:34 am 
Newbie

Joined: Thu Apr 01, 2004 6:16 am
Posts: 2
As suspected, it was user error, as the application has assigned id's

The Parent child chapter has some useful information around how to fix this.

However making the adjustments to the code and Hibernate mappings the code then produces the following SQL

INSERT INTO TESTREL VALUES(-1,'2004-04-01 14:32:48.966')
INSERT INTO TESTTWO VALUES(-1,'2004-04-01 14:32:48.966',-1,NULL)

Which hibernate does not like as the index colum is null.


[code]<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping default-cascade="save-update" auto-import="false">

<class name="hibtest.TestRel" mutable="true" polymorphism="implicit" dynamic-update="false" dynamic-insert="false" batch-size="1" select-before-update="false" >
<id name="id">
<generator class="assigned"/>
</id>

<array name="related" lazy="false" inverse="true" cascade="all" outer-join="true">
<key column="related_id"/>
<one-to-many class="hibtest.TestTwo"/>
<index column="in"/>
</array>
<timestamp column="timestamp" name="ts" access="field" unsaved-value="null" />

</class>

<class name="hibtest.TestTwo" cascade="all" mutable="true" polymorphism="implicit" dynamic-update="false" dynamic-insert="false" batch-size="1" select-before-update="false" >
<id name="id">
<generator class="assigned"/>
</id>
<many-to-one name="owner" column="related_id" not-null="true"/>
<timestamp column="timestamp" name="ts" access="field" unsaved-value="null" />
</class>

</hibernate-mapping>[/code]


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.