-->
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.  [ 15 posts ] 
Author Message
 Post subject: Simple mapping
PostPosted: Wed Jan 28, 2004 2:30 am 
Beginner
Beginner

Joined: Wed Jan 28, 2004 2:06 am
Posts: 33
I have two tables. Table class and table instance.

class:

templateRefId;



Instance:

instId;
templateRefId;




There is a one to many between table class and table Instance. One row in table a will have many entries in the table b.


for table Class the mapping is defined as follows :
<class name="Class" table="Class">
<id name="tempRefId" column="templateRefId" type="int" unsaved-value="0">
<generator class="increment"/>
</id>
<set name="instances" inverse="true">
<key column="templateRefId"/>
<one-to-many class="Instance"/>
</set>
</class>





class InstanceData that desribes the table b has the following mapping.
<class name="Instance" table="Instance">
<id name="instId" column="instanceId" type="int" unsaved-value="0">
<generator class="increment"/>
</id>
<property name="tempRefId" column="templateRefId" type="int" />
<!-- <many-to-one name="tempRefId" class="Class" column="templateRefId"/> -->
</class>




I want this relation to be bi-directional. Please let me know what should be changed for this to work. When i try to execute the code with the following mapping i get a SQL error saying that the FORIEGN KEY constraint has been violated. The insert into class goes fine. I am kind off stuck and documentation is not taking me anywhere.


Top
 Profile  
 
 Post subject: Re
PostPosted: Wed Jan 28, 2004 3:46 am 
Regular
Regular

Joined: Tue Jan 27, 2004 12:22 pm
Posts: 103
class InstanceData that desribes the table b has the following mapping.
<class name="Instance" table="Instance">
<id name="instId" column="instanceId" type="int" unsaved-value="0">
<generator class="increment"/>
</id>
<property name="tempRefId" column="templateRefId" type="int" />
<!-- <many-to-one name="tempRefId" class="Class" column="templateRefId"/> -->
</class>

Remove the bold line and remove the comment of the underlined line, it should work. The tempRefId can't be used twice in the class, i.e. for a reference and a instance value.

_________________
Dencel
- The sun has never seen a shadow -


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 28, 2004 3:48 am 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
This is a FAQ. See the manual - specifically Chapter 9, section 9.2: Bidirectional one to many. The problem and solution is detailed there.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 28, 2004 4:17 am 
Beginner
Beginner

Joined: Wed Jan 28, 2004 2:06 am
Posts: 33
Hello. Thank you very much. I did make that correction. I am a bit confused with whats happening now. The error is




net.sf.hibernate.HibernateException: SQL update or deletion failed (row not found)
net.sf.hibernate.HibernateException: SQL update or deletion failed (row not found)
at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:672)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:625)
at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2308)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2262)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2187)
at com.trulogica.truaccess.wfengpersist.dao.WfTemplateDAOTest.testSaveTemplate(Unknow
at com.trulogica.truaccess.wfengpersist.ejb.WfTemplateEJB.addTemplate(Unknown Source)
at com.trulogica.truaccess.wfengpersist.ejb.WfTemplateEJB_5hsagc_EOImpl.addTemplate(W
at com.trulogica.truaccess.wfengpersist.ejb.WfTemplateEJB_5hsagc_EOImpl_WLSkel.invoke
at weblogic.rmi.internal.BasicServerRef.invoke(BasicServerRef.java:360)
at weblogic.rmi.cluster.ReplicaAwareServerRef.invoke(ReplicaAwareServerRef.java:93)
at weblogic.rmi.internal.BasicServerRef.handleRequest(BasicServerRef.java:329)
at weblogic.rmi.internal.BasicExecuteRequest.execute(BasicExecuteRequest.java:22)
at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:140)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:121)

I am not doing a update or delete. I am just trying to ininsert into the class table and then into the instance table. Why is that a update being issued. The code is given below.


Class c = new Class();
Set instances = new HashSet();
Instance instance0 = new Instance();
instances.add(instance0);
template.settempRefId(instances);
Session session = PersistenceManager.getSession();
session.saveOrUpdate(template);
session.flush();
System.out.println("call flush done");
session.connection().commit();


I read through the documentation. Somehow i am not really getting a grip on that. Please do help out.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 28, 2004 4:27 am 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
I see that you have set your unsaved value to 0. What this this field inited to when you create the object?


Top
 Profile  
 
 Post subject: Re
PostPosted: Wed Jan 28, 2004 5:35 am 
Regular
Regular

Joined: Tue Jan 27, 2004 12:22 pm
Posts: 103
Remove the unsave-value=0 value. You use a hibernate generated key. Setting the unsave-value to 0 will tell Hibernate only to save new instances of this object with key value is 0 and to update instances with another key value. Because Hibernate sets the key value to another value then 0 it will try to update the object and it will fail, giving a Row not found exception.

This also applies to unsaved-value is 0 for the Instance key in your mapping file.

_________________
Dencel
- The sun has never seen a shadow -


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 28, 2004 11:23 am 
Beginner
Beginner

Joined: Wed Jan 28, 2004 2:06 am
Posts: 33
Hello:

Thanks for the help. But i am still getting the same error. Its trying to update when i have to insert into the database.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 28, 2004 11:23 am 
Beginner
Beginner

Joined: Wed Jan 28, 2004 2:06 am
Posts: 33
Hello:

Thanks for the help. But i am still getting the same error. Its trying to update when i have to insert into the database.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 28, 2004 11:28 am 
Beginner
Beginner

Joined: Wed Jan 28, 2004 2:06 am
Posts: 33
Hello:

Thanks for the help. But i am still getting the same error. Its trying to update when i have to insert into the database.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 29, 2004 6:56 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
This is for sure related to usaved-value, reread the doc and FAQ on that subject, it will help you a lot: this is a key feature of Hibernate

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 30, 2004 2:37 am 
Beginner
Beginner

Joined: Wed Jan 28, 2004 2:06 am
Posts: 33
Exactly. I solved the issue by changing the unsaved-value param. But now i am getting a different error.


Caused by: java.sql.SQLException: Syntax error converting the nvarchar value 'TemplateId1' to a column of data type int.

I am trying to find a row with the following query.

String queryStr = " FROM templateId in class WfTemplateData where
templateId=? "
results = session.find(queryStr, tempId, Hibernate.STRING);



My class is defined as (i have trimmed the unwanted fields)



public class WfTemplateData {

/**
* @return
* @hibernate.property column="templateId" type="string" length="50" not-null="true"
*/

// THIS FIELD IS VARCHAR (50) IN THE MS SQL DATABASE
public String getTemplateId() {
return templateId;
}



/**
* @param string
*/
public void setTemplateId(String string) {
templateId = string;
}

}[/b]


Top
 Profile  
 
 Post subject: Re
PostPosted: Fri Jan 30, 2004 5:20 am 
Regular
Regular

Joined: Tue Jan 27, 2004 12:22 pm
Posts: 103
When I look at you mapping file I see:
<id name="tempRefId" column="templateRefId" type="int" unsaved-value="0">
<generator class="increment"/>
</id>

You problably want this to be type="string".

Reading the documentation is a good starter. It's explained there. You problably also want to set the unsaved-value to null (the default) because you use an Object as indentification.

_________________
Dencel
- The sun has never seen a shadow -


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 30, 2004 11:12 am 
Beginner
Beginner

Joined: Wed Jan 28, 2004 2:06 am
Posts: 33
Hi.

The problem is really in the templateId field. Its not in remplateRefId field. Just prior to reply i gave you the .java files with the mappings. Please let me know.


Top
 Profile  
 
 Post subject: re
PostPosted: Fri Jan 30, 2004 11:24 am 
Regular
Regular

Joined: Tue Jan 27, 2004 12:22 pm
Posts: 103
You should check your posts. You haven't supplied the mapping of Template, only the sourcecode. We can't assume you've done the mapping correctly.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 30, 2004 11:27 am 
Beginner
Beginner

Joined: Wed Jan 28, 2004 2:06 am
Posts: 33
<property
name="templateId"
type="string"
update="true"
insert="true"
column="templateId"
length="50"
not-null="true"
/>


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