-->
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.  [ 7 posts ] 
Author Message
 Post subject: Parent Child Insertion problem Pls check my code
PostPosted: Fri Dec 23, 2005 1:36 am 
Newbie

Joined: Fri Dec 23, 2005 1:24 am
Posts: 3
Location: Delhi
Hi ,
Pls check my code and tell me Why Data is not inserted in Child table.Allthough It is insertion in Parent table sucessfully and no error is showing.


my Parent Class
::::::::::::;
package eg;

import java.util.Set;

public class Blog {
// private Long _id;
private String _id;
private String _name;
private Set _items;

public String getId() {
return _id;
}
public Set getItems() {
return _items;
}
public String getName() {
return _name;
}
public void setId(String long1) {
_id = long1;
}
public void setItems(Set list) {
_items = list;
}
public void setName(String string) {
_name = string;
}
}



::::::::::;
Child class

package eg;

import java.text.DateFormat;
import java.util.Calendar;

public class BlogItem {
// private Long _id;
private String _id;
// private Calendar _datetime;
private String _datetime;
private String _text;
private String _title;
private Blog _blog;

public Blog getBlog() {
return _blog;
}
public String getDatetime() {
return _datetime;
}
public String getId() {
return _id;
}
public String getText() {
return _text;
}
public String getTitle() {
return _title;
}
public void setBlog(Blog blog) {
_blog = blog;
}
public void setDatetime(String calendar) {
_datetime = calendar;
}
public void setId(String long1) {
_id = long1;
}
public void setText(String string) {
_text = string;
}
public void setTitle(String string) {
_title = string;
}
}


:::::::::::::::::;
parent Mapping
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping package="eg">

<class
name="Blog"
table="BLOGS"
lazy="true">
<id name="id" type="string" unsaved-value="null" >
<column name="BLOG_ID" sql-type="char(32)" not-null="true"/>
<generator class="assigned"/>
</id>

<property
name="name"
column="NAME"
not-null="true"
unique="true"/>

<set name="items" inverse="true" lazy="true" table="BLOG_ITEMS" >

<key column="BLOG_ID"/>
<one-to-many class="BlogItem"/>

</set>

</class>

</hibernate-mapping>


:::::::::::::::::::::::



child mapping

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping package="eg">



<class
name="BlogItem"
table="BLOG_ITEMS"
dynamic-update="true">
<!--
<class
name="BlogItem"
table="BLOG_ITEMS"
dynamic-update="true">

<id
name="id"
column="BLOG_ITEM_ID">

<generator class="assigned"/>

</id>
-->
<id name="id" type="string" unsaved-value="null" >
<column name="BLOG_ITEM_ID" sql-type="char(22)" not-null="true"/>
<generator class="native"/>
<!-- assigned-->
</id>

<property
name="title"
column="TITLE"
not-null="true"/>

<property
name="text"
column="TEXT"
not-null="true"/>

<property
name="datetime"
column="DATE_TIME"
not-null="true"/>

<many-to-one name="blog" class="Blog" column="BLOG_ID" not-null="true" cascade="save-update"/>

</class>

</hibernate-mapping>




Calling Code ::::::::::::::::::;



package eg;
import org.hibernate.*;
import org.hibernate.cfg.*;
import net.sf.hibernate.examples.quickstart.* ;
import java.util.*;
public class DMLBlogs
{
public DMLBlogs(){
System.out.println("Conatructor DMLCat Called ");
}

public void execute(){
Session session = HibernateUtil.currentSession();

Transaction tx= session.beginTransaction();

Blog b = new Blog();
b.setId("24");
System.out.println(" Now ID will set 24::::::::::::::;0");

b.setName("test blob:24");
Set s = new HashSet();

BlogItem i = new BlogItem();

System.out.println(" Now ID will set 24 ::::::::::::::;1");
i.setId("25");
i.setText("RD");
i.setTitle("Rad");
i.setDatetime("11");
i.setBlog(b);

s.add(i);

i = new BlogItem();
i.setBlog(b);
System.out.println(" Now ID will set 24:::::::::::::::;;2");
i.setId("26");
i.setTitle("TV01");
i.setText("Tele");
i.setDatetime("2");
s.add(i);


b.setItems(s);


session.save(b);
//session.save(i);

System.out.println(" After SAVE ::::::::::::Now ID will set 24:::::::::::::::;;2");
tx.commit();

HibernateUtil.closeSession();
}

}






With regards


Siddharth Singh




Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

_________________
Siddharth Singh
singhsiddh@yahoo.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 27, 2005 2:58 pm 
Regular
Regular

Joined: Fri Sep 09, 2005 11:35 am
Posts: 101
<set name="items" inverse="true" lazy="true" table="BLOG_ITEMS" >

<key column="BLOG_ID"/>
<one-to-many class="BlogItem"/>

</set>

remove inverse=true then it should work.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 28, 2005 12:21 am 
Newbie

Joined: Fri Dec 23, 2005 1:24 am
Posts: 3
Location: Delhi
Hi abg1979

After removing [ inverse="true" ] Now This Error is comming

Dec 28, 2005 9:46:27 AM org.hibernate.event.def.AbstractFlushingEventListener performExecutions
SEVERE: Could not synchronize database state with session
org.hibernate.HibernateException: Unexpected row count: 0 expected: 1
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:32)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:916)
at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:23)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:324)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at eg.DMLBlogs.execute(DMLBlogs.java:53)
at org.apache.jsp.dmlcat_jsp._jspService(dmlcat_jsp.java:53)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:92)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:809)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:162)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:240)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:187)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:809)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:200)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:146)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:209)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:144)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2358)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:133)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:118)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:594)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:116)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:594)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:127)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:152)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
at java.lang.Thread.run(Thread.java:534)
exp = org.hibernate.HibernateException: Unexpected row count: 0 expected: 1



pls explain why

_________________
Siddharth Singh
singhsiddh@yahoo.com


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 28, 2005 4:35 am 
Beginner
Beginner

Joined: Thu Sep 01, 2005 7:43 am
Posts: 31
Location: León (Spain)
The point here is the cascade attribute:

<many-to-one name="blog" class="Blog" column="BLOG_ID" not-null="true" cascade="save-update"/>

With this line you are telling hibernate that if you save a child object, save also the parent (and I supose you want the inverse operation).

Move "cascade=..." to the "<set ...>" mapping in the parent hbm.xml

Hope this helps.
Bye.

_________________
Please rate...

Expert on Hibernate errors... I've had them all... :P


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 28, 2005 7:29 am 
Newbie

Joined: Fri Dec 23, 2005 1:24 am
Posts: 3
Location: Delhi
Hi,

Can any one send me complete example of code for parent-child scenario if some one already attempted this. appreciate your help in advance.


Siddharth singh

_________________
Siddharth Singh
singhsiddh@yahoo.com


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 28, 2005 12:00 pm 
Regular
Regular

Joined: Fri Sep 09, 2005 11:35 am
Posts: 101
remove the cascade attribute also.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 28, 2005 11:52 pm 
Beginner
Beginner

Joined: Tue Dec 21, 2004 5:34 pm
Posts: 25
Siddharth wrote:
Hi,

Can any one send me complete example of code for parent-child scenario if some one already attempted this. appreciate your help in advance.


Siddharth singh


http://www.hibernate.org/hib_docs/v3/reference/en/html/example-parentchild.html


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