-->
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.  [ 22 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: one-to-many problem
PostPosted: Tue Nov 11, 2003 5:37 pm 
Beginner
Beginner

Joined: Tue Nov 11, 2003 5:15 pm
Posts: 23
I am trying to map parent-child object in the mapping file, when I save the Parent (Terms) object the child (Schedule) objects doesn't get saved, what am I doing wrong here? I defined the parent and child in differnt mapping file.

I created two objects, Terms, Schedule

Terms {
private String termsId;
private String ownerId;
private Set schduleSet;

..... (assume get/set available)

}
Schedule{
private String scheduleId;
private String termsId;
private Timestamp schduleDate;

..... (assume get/set available)
}

Mapping files:

Terms.hbm.xml

<class name="Terms"
table="terms"
dynamic-insert="true"
dynamic-update="true" >

<property name="name" type="string" column="owner_id" not-null="true" length="24"/>
<property name="ownerId" type="string" column="owner_id" not-null="true" length="24"/>
<id name="id" type="string" column="terms_id" length="14" >
<generator class="assigned"/>
</id>

<set name="scheduleSet" table="terms_schedule" inverse="true" lazy="true">
<key column="terms_id"/>
<one-to-many class="Schedule"/>
</set>
</class>


Schedule.hbm.xml

<class name="Schedule"
dynamic-insert="true"
dynamic-update="true"
table="terms_schedule">

<id name="id" type="string" column="schedule_id" length="24">
<generator class="assigned"/>
</id>
<property name="scheduleDate" type="timestamp" column="schedule_date" length="16"/>
<many-to-one name="termsId" column="terms_id" class="Terms"/>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 11, 2003 6:01 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
You need to add "cascade" attribute to the mapping


Top
 Profile  
 
 Post subject: gettting this exception after setting cascade=true
PostPosted: Tue Nov 11, 2003 6:38 pm 
Beginner
Beginner

Joined: Tue Nov 11, 2003 5:15 pm
Posts: 23
Caused by: net.sf.hibernate.MappingException: No persister for: java.lang.String
at net.sf.hibernate.impl.SessionFactoryImpl.getPersister(SessionFactoryImpl.java:441)
at net.sf.hibernate.impl.SessionImpl.getPersister(SessionImpl.java:2286)
at net.sf.hibernate.impl.SessionImpl.getPersister(SessionImpl.java:2293)
at net.sf.hibernate.impl.SessionImpl.getEntityIdentifierIfNotUnsaved(SessionImpl.java:2344)
at net.sf.hibernate.type.EntityType.getIdentifier(EntityType.java:55)
at net.sf.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:33)
at net.sf.hibernate.persister.EntityPersister.dehydrate(EntityPersister.java:367)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:626)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:602)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 11, 2003 6:45 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Your ammping must look like

Code:
Schedule{
private String scheduleId;
private Terms terms;
private Timestamp schduleDate;

..... (assume get/set available)
}
<class name="Schedule"
dynamic-insert="true"
dynamic-update="true"
table="terms_schedule">

<id name="id" type="string" column="schedule_id" length="24">
<generator class="assigned"/>
</id>
<property name="scheduleDate" type="timestamp" column="schedule_date" length="16"/>
<many-to-one name="terms" column="terms_id" class="Terms"/>
</class>


Do you have
Code:
Sechdule {
private String scheduleId;
public String getId() {
  return scheduleId;
}

or
Code:
Sechdule {
private String scheduleId;
public String getScheduleId() {
  return scheduleId;
}

In the second case, <id name="scheduleId"> is appropriate

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 11, 2003 6:59 pm 
Beginner
Beginner

Joined: Tue Nov 11, 2003 5:15 pm
Posts: 23
sorry to confuse you, actually in my code I have,

<id name="schduleId" type="string" column="schedule_id" length="24">
<generator class="assigned"/>
</id>

one more question,

<many-to-one name="terms" column="terms_id" class="Terms"/> is this correct? what does terms refer
to? should it be a property of Schedule with type as Terms


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 11, 2003 7:02 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
yes, your Schedule class must have a property named "terms" which is of class Terms


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 11, 2003 7:10 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I rename your termid property into term property since Schedule object must have a Term type property and not the term id (the many-to-one)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 11, 2003 7:18 pm 
Beginner
Beginner

Joined: Tue Nov 11, 2003 5:15 pm
Posts: 23
I created a terms property in Schedule Object and changed the mapping

Caused by: 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:633)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:602)
at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:27)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2101)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2074)

when I see the sql in profiler, hibernate send update sql, instead it should have sent insert sql,

update pay_terms_schedule set ........


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 11, 2003 7:25 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Can't see that in your previous mapping files.

Read the doco about saveOrUpdate() and unsaved-value.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 11, 2003 7:29 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
Look, it is hard to guess what is going on because:
1. you do not show example of the code
2. you are not showing the real mapping but looks like you type snippets from scratch and there are many inconsistencies there.

I suppose you need either
1. read section 8 of the manual ( http://www.hibernate.org/hib_docs/refer ... rent-child )
2. paste here _real_ examples


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 12, 2003 7:05 pm 
Beginner
Beginner

Joined: Tue Nov 11, 2003 5:15 pm
Posts: 23
I have the exact same problem as in one of the topic in this forum

http://forum.hibernate.org/viewtopic.php?t=925450

and seems to me that this is a bug in hibernate, because I don't see an answer/an example anywhere in the forum/documents........ so is this really a bug?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 12, 2003 7:25 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
stack trace?
final look of mapping files?
code which causes problem?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 12, 2003 8:20 pm 
Beginner
Beginner

Joined: Tue Nov 11, 2003 5:15 pm
Posts: 23
Here is the actually mapping file, java file and db structure
Although this is difft from what I posted in the begining, but I am working through this right now, if I can fix this, the same I need to apply to first one.

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

<hibernate-mapping>

<!--
Nov 3, 2003 - Created
Suresh R
-->

<class name="insite.business.domain.services.PayTermsRequest"
table="pay_terms_request"
dynamic-insert="true"
dynamic-update="true" >

<id name="id" type="string" column="pay_terms_request_id" length="14" >
<generator class="assigned"/>
</id>
<property name="name" type="string" column="name" not-null="true" length="100"/>
<property name="description" type="string" column="description" length="2000"/>
<property name="ownerId" type="string" column="owner_id" not-null="true" length="24"/>
<property name="buyerCode" type="string" column="buyer_code" length="4"/>


<set name="commentSet" lazy="false" cascade="all">
<key column="object_ref"/>
<one-to-many class="insite.business.domain.services.Comments"/>
</set>

</class>
</hibernate-mapping>


Comments.hbm.xml

<hibernate-mapping>


<class name="insite.business.domain.services.Comments" table="comments">
<id name="commentId" type="string" column="comment_id">
<generator class="assigned"/>
</id>
<property name="createTime" type="timestamp" column="create_time" length="16"/>
<property name="creatorId" type="string" column="creator_id" not-null="true" length="24"/>
<property name="comments" type="string" column="comments" length="1000"/>
<property name="rejectedReasonId" type="string" column="rejected_reason_id" length="24"/>
<property name="internalComments" type="string" column="internal_comments" length="1000"/>

<many-to-one name="request" column="object_ref"
class="insite.business.domain.services.PayTermsRequest" />

</class>
</hibernate-mapping>


Java File

Comments.java


import java.sql.Timestamp;
import java.io.Serializable;

import net.sf.hibernate.Lifecycle;
import net.sf.hibernate.Session;
import net.sf.hibernate.CallbackException;
import fieldglass.util.IdUtil;

public class Comments implements Lifecycle {

private String objectRef;
private String creatorId;
private Timestamp createTime;
private String comments;
private String rejectedReasonId;
private String internalComments;
private String commentId;

private PayTermsRequest request;

public Comments() {
commentId = IdUtil.generateId();
}

public String getCommentId() {
return commentId;
}

public void setCommentId(String commentId) {
this.commentId = commentId;
}

public String getObjectRef() {
return objectRef;
}

public void setObjectRef(String objectRef) {
this.objectRef = objectRef;
}

public String getCreatorId() {
return creatorId;
}

public void setCreatorId(String creatorId) {
this.creatorId = creatorId;
}

public Timestamp getCreateTime() {
return createTime;
}

public void setCreateTime(Timestamp createTime) {
this.createTime = createTime;
}

public String getComments() {
return comments;
}

public void setComments(String comments) {
this.comments = comments;
}

public String getRejectedReasonId() {
return rejectedReasonId;
}

public void setRejectedReasonId(String rejectedReasonId) {
this.rejectedReasonId = rejectedReasonId;
}

public String getInternalComments() {
return internalComments;
}

public void setInternalComments(String internalComments) {
this.internalComments = internalComments;
}

public PayTermsRequest getRequest() {
return request;
}

public void setRequest(PayTermsRequest request) {
this.request = request;
}

public boolean onSave(Session s) throws CallbackException {
return false;
}

public boolean onUpdate(Session s) throws CallbackException {
return false;
}

public boolean onDelete(Session s) throws CallbackException {
return false;
}

public void onLoad(Session s, Serializable id) {
}
}


PayTermsRequest.java




import java.util.HashSet;
import java.util.Set;

public class PayTermsRequest {

private String id;
private String name;
private String description;
private String buyerCode;
private String ownerId;

private Set commentSet;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getBuyerCode() {
return buyerCode;
}

public void setBuyerCode(String buyerCode) {
this.buyerCode = buyerCode;
}

public String getOwnerId() {
return ownerId;
}

public void setOwnerId(String ownerId) {
this.ownerId = ownerId;
}


public Set getCommentSet() {
return commentSet;
}

public void setCommentSet(Set commentSet) {
this.commentSet = commentSet;
}

}

Tables
=====

CREATE TABLE [dbo].[comments] (

[comment_id] [varchar] (24) not null,

[object_ref] [varchar] (24) NOT NULL ,

[creator_id] [varchar] (24) NOT NULL ,

[create_time] [datetime] NOT NULL ,

[comments] [varchar] (2000) NULL ,

[rejected_reason_id] [varchar] (24) NULL ,

[internal_comments] [varchar] (2000) NULL,

)
go

alter table dbo.comments
add primary key (object_ref, comment_id)
go


drop table pay_terms_request
CREATE TABLE [dbo].[pay_terms_request] (
[pay_terms_request_id] [varchar] (14) NOT NULL ,
[name] [varchar] (100) NOT NULL ,
[description] [varchar] (2000) NULL ,
[buyer_code] [varchar] (4) NOT NULL ,
[owner_id] [varchar] (24) NOT NULL
)
go
alter table dbo.pay_terms_request
add primary key (pay_terms_request_id)
go



package fieldglass.persist;

import fieldglass.db.DBException;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;

import java.sql.Connection;

public class PersistSession {
private Connection connection = null;
private SessionFactory sessionFactory;

protected PersistSession(Connection connection){
this.connection = connection;
try {
this.sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Exception e) {
throw new DBException(e);
}
}

public void commit(Object o) {
Transaction transaction = null;
try {
Session session = sessionFactory.openSession(this.connection);
transaction = session.beginTransaction();
session.save(o);
transaction.commit();
} catch (HibernateException e) {
try {
transaction.rollback();
} catch (HibernateException e1) {
throw new DBException(e);
}
throw new DBException(e);
}
}
}

StackTrace........

Caused by: 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:633)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:602)
at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:27)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2101)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2074)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2017)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:57)
at fieldglass.persist.PersistSession.commit(PersistSession.java:25)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 12, 2003 8:21 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
As I already asked on another thread:
shouldn't you constructor for Terms assign ID ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 12, 2003 8:43 pm 
Beginner
Beginner

Joined: Tue Nov 11, 2003 5:15 pm
Posts: 23
I am setting this id from outside,

let me also tell you how I populate the object to be saved

PayTermsRequest request = new PayTermsRequest();
request.setId("id-1")
request.setName("name");
request.setDescription("descrip");
request.setBuyerCode("COMP");
request.setOwnerId("ownerId");

HashSet set = new HashSet();
Comments comments = new Comments();
comments.setComments("my comments");
comments.setCreateTime(time);
comments.setCreatorId("cre");
comments.setInternalComments("internatl");
comments.setRejectedReasonId("reje");
comments.setRequest(request); //many-to-one
set.add(comments);

request.setCommentSet(set);


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 22 posts ]  Go to page 1, 2  Next

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.