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 ?