-->
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.  [ 11 posts ] 
Author Message
 Post subject: Problem with find method
PostPosted: Fri Dec 05, 2003 12:18 pm 
Beginner
Beginner

Joined: Thu Oct 02, 2003 8:02 am
Posts: 45
I have 2 classes Internaute and Login bind by a one-to-one bidirectionnal association.
When a user authenticates, i retrieve informations in order to create a Login object. Then i execute the following method :
Code:
public Internaute rechercherUtilisateur(Login l)
   {
      try
            {
               Session session = HibernateDAOFactory.creerConnection();
               String identifiant = l.getIdentifiant();
               Transaction tx = session.beginTransaction();
               List users = session.find("from Internaute as internaute where internaute.login =?",l,Hibernate.entity(Login.class));
               Iterator it = users.iterator();
               Internaute user=null;
               while ( it.hasNext())
               {
                  user = (Internaute)it.next();      
               }
               tx.commit();
               HibernateSession.closeSession();
                              return user;
                     
            }
            catch(Exception e)
            {
               e.printStackTrace();
               // return null;
            }
         }


I have the error message :
net.sf.hibernate.TransientObjectException: object references an unsaved transien
t instance - save the transient instance before flushing: com.cfort.utilisateur.
Login
at net.sf.hibernate.impl.SessionImpl.throwTransientObjectException(Sessi
onImpl.java:2385)
at net.sf.hibernate.impl.SessionImpl.getEntityIdentifierIfNotUnsaved(Ses
sionImpl.java:2377)
at net.sf.hibernate.type.EntityType.getIdentifier(EntityType.java:56)
at net.sf.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:33
)
at net.sf.hibernate.loader.Loader.prepareQueryStatement(Loader.java:494)

at net.sf.hibernate.loader.Loader.doFind(Loader.java:136)
at net.sf.hibernate.loader.Loader.find(Loader.java:620)
at net.sf.hibernate.hql.QueryTranslator.find(QueryTranslator.java:928)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1343)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1322)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1318)
at com.cfort.dao.HibernateUtilisateurDAO.rechercherUtilisateur(Hibernate
UtilisateurDAO.java:109)
at com.cfort.service.UtilisateurService.rechercherUtilisateur(Utilisateu
rService.java:91)
at com.cfort.struts.action.LoginAction.execute(LoginAction.java:56)
at org.apache.struts.action.RequestProcessor.processActionPerform(Reques
tProcessor.java:484)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.ja
va:274)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:148
2)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:763)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:284)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:204)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:256)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:563)
at org.apache.catalina.core.StandardContextValve.invokeInternal(Standard
ContextValve.java:245)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:199)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:563)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:195)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:151)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:164)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:149)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:563)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:156)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:563)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:972)

at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:20
9)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java
:670)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.proce
ssConnection(Http11Protocol.java:517)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java
:575)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadP
ool.java:666)
at java.lang.Thread.run(Thread.java:534)

I read the documentation and found an example with the same piece of code
Code:
List mates = sess.find(
    "from Cat as cat where cat.mate = ?",
    izi,
    Hibernate.entity(Cat.class)
);


Does it mean that the search criteria ( here Login object) must be persistent ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 05, 2003 1:05 pm 
Beginner
Beginner

Joined: Tue Oct 07, 2003 11:36 am
Posts: 46
Location: Rennes, France
Salut,

U may forget saving a newly created user by doing a session.flush().

a+
Yann


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 05, 2003 1:08 pm 
Beginner
Beginner

Joined: Thu Oct 02, 2003 8:02 am
Posts: 45
Sorry, but i don't understand what you exactly mean !


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 05, 2003 1:12 pm 
Beginner
Beginner

Joined: Tue Oct 07, 2003 11:36 am
Posts: 46
Location: Rennes, France
Have u performed any operations on a Internaute object before calling this search method of Internaute ?

For example,
Internaute i = new Internaute();
i.setNom("toto");
i.save();
<--- do a flush here !
rechercherUtilisateur(new Login("blabla")); --> Exception !


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 05, 2003 1:32 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
Er...
Why do you pass parameter of type String to find() and tell Hibernate its type is Login ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 05, 2003 9:54 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Look at the exception message:

Quote:
net.sf.hibernate.TransientObjectException: object references an unsaved transient instance -
save the transient instance before flushing: com.cfort.utilisateur.
Login



That is really quite helpful, I think.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 05, 2003 9:55 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
ie. The Login l is transient.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 08, 2003 10:35 am 
Beginner
Beginner

Joined: Thu Oct 02, 2003 8:02 am
Posts: 45
OK. The Login l is transient.
As i see, the criteria must be persistent !
So how can i do to retrieve a login by using a parameter of type login (enter by a user) in find method, knowing that the Login parameter is trancient (which seems to be logical for me) ?
THANKS


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 08, 2003 10:45 am 
Beginner
Beginner

Joined: Tue Oct 07, 2003 11:36 am
Posts: 46
Location: Rennes, France
What happens when login is loaded from session.load() instead of new Login() ?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 08, 2003 11:20 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Example query might help
(section 14.6 of the reference guide)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 08, 2003 12:01 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
Does your Login has ID property initialized?


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