-->
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: how i save the object with reference to other object ?
PostPosted: Mon Jan 21, 2008 11:11 pm 
Newbie

Joined: Fri Dec 07, 2007 1:17 am
Posts: 6
for example, the user object with two attribute id, name. when we want to save this object to database, we simply create a session and call .save
(object_name), but if the object obtain a reference to other object, for example, id, name, address_ref(this is reference to Address object with class type Address), how to persist this object to database !!??


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 22, 2008 2:27 am 
Newbie

Joined: Sun Mar 25, 2007 7:28 pm
Posts: 2
Cascading is off by default. If you turn on an appropriate cascade for that relation, hibernate will save the associated object automatically. If not, you must explicity perform a save on both objects.

You can find information on cascade in the hibernate doc Chp. 5.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 22, 2008 5:23 am 
Newbie

Joined: Fri Dec 07, 2007 1:17 am
Posts: 6
the object i want to save refer to other object that has been persisted, i want to persist the current object into the database. so, i can't save the both objects.


Top
 Profile  
 
 Post subject: can u help me..
PostPosted: Tue Jan 29, 2008 3:43 am 
Newbie

Joined: Fri Jan 25, 2008 8:32 am
Posts: 15
i am trying to insert the value in databases but it is fired by hibernate...

Hibernate: select unwanted0_.ID as ID0_0_, unwanted0_.name as name0_0_ from UNWANTED unwanted0_ where unwanted0_.ID=?
org.hibernate.MappingException: Unknown entity: java.lang.Long
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:548)
at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1338)
at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:180)
at org.hibernate.event.def.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:487)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:70)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:38)
at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:618)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:592)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:596)
at org.apache.jsp.unwanted_jsp._jspService(unwanted_jsp.java:131)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:874)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Unknown Source)

org.apache.jasper.JasperException: Exception in JSP: /unwanted.jsp:4

1: <%@ include file="protected.jsp" %>
2: <%@ include file="header.jsp" %>
3: <%
4: Long id=Long.valueOf(request.getParameter("Id"));
5: Session sess = HibernateUtil.currentSession();
6: Unwanted u= new Unwanted();
7:


Stacktrace:
my mapping looks like it..
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="com.ats.custFlight.pojo.Unwanted" table="unwanted" lazy="false">
<id column="ID" name="id" type="java.lang.Long"/>
<property column="name" name="name" type="java.lang.String"/>
</class>
</hibernate-mapping>



my code to upload data is--

Long id=Long.valueOf(request.getParameter("Id"));
Session sess = HibernateUtil.currentSession();
Unwanted u= new Unwanted();

Unwanted u2 = (Unwanted) sess.get (Unwanted.class,(long)77);
u.setId(id);
u.setName("RAHUL");
try{
sess.save(u);
}catch( Exception e )
{
e.printStackTrace();
}
System.out.println(" size is " + u.getId());

my hibernate.util code is--
package com.ats.custFlight.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.hibernate.*;
import org.hibernate.cfg.*;

public class HibernateUtil {

private static Log log = LogFactory.getLog(HibernateUtil.class);

private static final SessionFactory sessionFactory;

static {
try {
// Create the SessionFactory
sessionFactory = new Configuration().configure().buildSessionFactory();

} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
log.error("Initial SessionFactory creation failed.", ex);
ex.printStackTrace();
throw new ExceptionInInitializerError(ex);
}
}

public static final ThreadLocal < Session > session = new ThreadLocal < Session > ();

public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}

public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s != null)
s.close();
}
}
and JAVA CLASS IS..

package com.ats.custFlight.pojo;

public class Unwanted {

Long id;
String name;

public String getName() {
return name;
}

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

public Long getId() {
return id;
}

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


wenger_sieung wrote:
for example, the user object with two attribute id, name. when we want to save this object to database, we simply create a session and call .save
(object_name), but if the object obtain a reference to other object, for example, id, name, address_ref(this is reference to Address object with class type Address), how to persist this object to database !!??


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.