1

I'm trying to get a lot of data in one query with Hibernate.

I have this named query :

SELECT r FROM Reservationlight_SS r LEFT JOIN r.personne p WHERE 
  r.iDSport =:IDSport AND r.iDSaison =:IDSaison AND 
  r.dateReservation =:date GROUP BY r.numeroReservation, r.heureDebutReservation, r.iDTerrain

When I use "JOIN" insted of "LEFT JOIN" I have only row that match r.personne. But sometimes, r.personne is null. So I decide to use the "LEFT JOIN" and Hibernate give me this error :

org.hibernate.HibernateException: collection is not associated with any session

I try to search if it's possible to have the @OneToMany as optional but this is not possible. But I'm pretty sure it could be a way to do that no ?

This is my relation in my code now :

@OneToMany(fetch = FetchType.EAGER)
@OrderBy(value = "NumReservation")
@JoinColumns({
    @JoinColumn(name="NumReservation", referencedColumnName="NumeroReservation")
})
private List<Personnereservationlight> personne;

I success to have all Reservationlight_SS without the personne by deleting the "JOIN" and also success to have all Reservationlight_SS that have a r.personne in the other table with the "JOIN". But I can't have the both in the same request when I put the "LEFT JOIN".

Someone has an idea ?

Thank you for your help

Stack trace :

Infos:   org.hibernate.HibernateException: collection is not associated with any session
Avertissement:   A system exception occurred during an invocation on EJB ReservationFacade, method: public java.util.List ch.plugin.plugin.dao.ReservationFacade.getAllReservationByDate(java.lang.Integer,java.lang.Integer,java.util.Date)
Avertissement:   javax.ejb.TransactionRolledbackLocalException: Client's transaction aborted
    at com.sun.ejb.containers.EJBContainerTransactionManager.useClientTx(EJBContainerTransactionManager.java:357)
    at com.sun.ejb.containers.EJBContainerTransactionManager.preInvokeTx(EJBContainerTransactionManager.java:251)
    at com.sun.ejb.containers.BaseContainer.preInvokeTx(BaseContainer.java:4524)
    at com.sun.ejb.containers.BaseContainer.preInvoke(BaseContainer.java:1986)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:210)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88)
    at com.sun.proxy.$Proxy10505.getAllReservationByDate(Unknown Source)
    at ch.plugin.plugin.BusinessService.ReservationManager.getAllReservationByDate(ReservationManager.java:797)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1081)
    at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1153)
    at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:4786)
    at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:656)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:608)
    at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:46)
    at org.jboss.weld.ejb.SessionBeanInterceptor.aroundInvoke(SessionBeanInterceptor.java:52)
    at sun.reflect.GeneratedMethodAccessor2154.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:608)
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doCall(SystemInterceptorProxy.java:163)
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:140)
    at sun.reflect.GeneratedMethodAccessor2155.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
    at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:369)
    at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:4758)
    at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:4746)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88)
    at com.sun.proxy.$Proxy10481.getAllReservationByDate(Unknown Source)
    at service.ReservationFacadeREST.getAllReservationByDate(ReservationFacadeREST.java:281)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1081)
    at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1153)
    at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:4786)
    at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:656)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:608)
    at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:46)
    at org.jboss.weld.ejb.SessionBeanInterceptor.aroundInvoke(SessionBeanInterceptor.java:52)
    at sun.reflect.GeneratedMethodAccessor2154.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:608)
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doCall(SystemInterceptorProxy.java:163)
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:140)
    at sun.reflect.GeneratedMethodAccessor2155.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
    at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:369)
    at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:4758)
    at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:4746)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88)
    at com.sun.proxy.$Proxy10551.getAllReservationByDate(Unknown Source)
    at service.__EJB31_Generated__ReservationFacadeREST__Intf____Bean__.getAllReservationByDate(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:151)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:171)
    at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:195)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:104)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:387)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:331)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:103)
    at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:271)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:297)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:254)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1028)
    at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:372)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
    at java.lang.Thread.run(Thread.java:745)

Entity :

@Entity
@Table(name = "reservationlight")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "getAllReservationByDateSS", query = "SELECT r FROM Reservationlight_SS r LEFT JOIN r.personne p WHERE r.iDSport =:IDSport AND r.iDSaison =:IDSaison AND r.dateReservation =:date GROUP BY r.numeroReservation, r.heureDebutReservation, r.iDTerrain")
})
public class Reservationlight_SS implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Column(name = "DateReservation")
    @Temporal(TemporalType.DATE)
    private Date dateReservation;
    @Id
    @Column(name = "HeureDebutReservation")
    @Temporal(TemporalType.TIME)
    private Date heureDebutReservation;
    @Id
    @Column(name = "IDTerrain")
    private int iDTerrain;
    @Size(max = 7)
    @Column(name = "CouleurReservation")
    private String couleurReservation;
    @Column(name = "NumeroReservation")
    private Integer numeroReservation;
    @Id
    @Column(name = "IDSport")
    private int iDSport;
    @Id
    @Column(name = "IDSaison")
    private int iDSaison;
    @Column(name = "IDCours")
    private Integer IDCours;
    @Column(name = "isPaye")
    private int isPaye;
    @Column(name = "Etat")
    private boolean etat;
    @Size(max = 25)
    @Column(name = "Nom")
    private String nom;
    @Size(max = 25)
    @Column(name = "Prenom")
    private String prenom;
    @Column(name = "IDUser")
    private Integer iDUser;
    @Column(name = "IDPersonne")
    private Integer iDPersonne;
    @Size(max = 65535)
    @Column(name = "Texte")
    private String texte;
    @Column(name = "Prix")
    private float prix;
    @Size(max = 65535)
    @Column(name = "Notification")
    private String notification;
    @Column(name = "JEtat")
    private String jEtat;
    @ManyToOne(optional = true)
    @JoinColumns({
        @JoinColumn(name="IDRecurrence", referencedColumnName="IDRecurrence"),
    })
    private Recurrencelight recurrence;
    @OneToMany(fetch = FetchType.EAGER)
    @OrderBy(value = "NumReservation")
    @JoinColumns({
        @JoinColumn(name="NumReservation", referencedColumnName="NumeroReservation")
    })
    private List<Personnereservationlight> personne;

    public Reservationlight_SS() {
    }

    public Reservationlight_SS(Reservationlight_SS rh)
    {
        this.IDCours = rh.getIDCours();
        this.couleurReservation = rh.getCouleurReservation();
        this.dateReservation = rh.getDateReservation();
        this.etat = rh.getEtat();
        this.heureDebutReservation = rh.getHeureDebutReservation();
        this.iDPersonne = rh.getIDPersonne();
        this.iDSaison = rh.getIDSaison();
        this.iDSport = rh.getIDSport();
        this.iDTerrain = rh.getIDTerrain();
        this.iDUser = rh.getIDUser();
        this.isPaye = rh.getIsPaye();
        this.jEtat = rh.getjEtat();
        this.nom = rh.getNom();
        this.prenom = rh.getPrenom();
        this.notification = rh.getNotification();
        this.numeroReservation = rh.getNumeroReservation();
        this.prix = rh.getPrix();
        this.texte = rh.getTexte();
        this.recurrence = rh.getRecurrence();
        this.personne = rh.getPersonne();
    }

    public Date getDateReservation() {
        return dateReservation;
    }

    public void setDateReservation(Date dateReservation) {
        this.dateReservation = dateReservation;
    }

    public Date getHeureDebutReservation() {
        return heureDebutReservation;
    }

    public void setHeureDebutReservation(Date heureDebutReservation) {
        this.heureDebutReservation = heureDebutReservation;
    }

    public int getIDTerrain() {
        return iDTerrain;
    }

    public void setIDTerrain(int iDTerrain) {
        this.iDTerrain = iDTerrain;
    }

    public String getCouleurReservation() {
        return couleurReservation;
    }

    public void setCouleurReservation(String couleurReservation) {
        this.couleurReservation = couleurReservation;
    }

    public Integer getNumeroReservation() {
        return numeroReservation;
    }

    public void setNumeroReservation(Integer numeroReservation) {
        this.numeroReservation = numeroReservation;
    }

    public int getIDSport() {
        return iDSport;
    }

    public void setIDSport(int iDSport) {
        this.iDSport = iDSport;
    }

    public int getIDSaison() {
        return iDSaison;
    }

    public void setIDSaison(int iDSaison) {
        this.iDSaison = iDSaison;
    }

    public Recurrencelight getRecurrence() {
        return recurrence;
    }

    public void setRecurrence(Recurrencelight recurrence) {
        this.recurrence = recurrence;
    }

    public Integer getIDCours() {
        return IDCours;
    }

    public void setIDCours(Integer IDCours) {
        this.IDCours = IDCours;
    }

    public int getIsPaye() {
        return isPaye;
    }

    public void setIsPaye(int isPaye) {
        this.isPaye = isPaye;
    }

    public boolean getEtat() {
        return etat;
    }

    public void setEtat(boolean etat) {
        this.etat = etat;
    }

    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    public String getPrenom() {
        return prenom;
    }

    public void setPrenom(String prenom) {
        this.prenom = prenom;
    }

    public Integer getIDUser() {
        return iDUser;
    }

    public void setIDUser(Integer iDUser) {
        this.iDUser = iDUser;
    }

    public Integer getIDPersonne() {
        return iDPersonne;
    }

    public void setIDPersonne(Integer iDPersonne) {
        this.iDPersonne = iDPersonne;
    }

    public String getTexte() {
        return texte;
    }

    public void setTexte(String texte) {
        this.texte = texte;
    }

    public float getPrix() {
        return prix;
    }

    public void setPrix(float prix) {
        this.prix = prix;
    }

    public String getNotification() {
        return notification;
    }

    public void setNotification(String notification) {
        this.notification = notification;
    }

    public String getjEtat() {
        return jEtat;
    }

    public void setjEtat(String jEtat) {
        this.jEtat = jEtat;
    }

    public List<Personnereservationlight> getPersonne() {
        return personne;
    }

    @Transactional
    public void setPersonne(List<Personnereservationlight> personne) {
        this.personne = personne;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 17 * hash + Objects.hashCode(this.dateReservation);
        hash = 17 * hash + Objects.hashCode(this.heureDebutReservation);
        hash = 17 * hash + this.iDTerrain;
        hash = 17 * hash + this.iDSport;
        hash = 17 * hash + this.iDSaison;
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Reservationlight_SS other = (Reservationlight_SS) obj;
        if (!Objects.equals(this.dateReservation, other.dateReservation)) {
            return false;
        }
        if (!Objects.equals(this.heureDebutReservation, other.heureDebutReservation)) {
            return false;
        }
        if (this.iDTerrain != other.iDTerrain) {
            return false;
        }
        if (this.iDSport != other.iDSport) {
            return false;
        }
        if (this.iDSaison != other.iDSaison) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Reservationlight_SS{" + "dateReservation=" + dateReservation + ", heureDebutReservation=" + heureDebutReservation + ", iDTerrain=" + iDTerrain + ", couleurReservation=" + couleurReservation + ", numeroReservation=" + numeroReservation + ", iDSport=" + iDSport + ", iDSaison=" + iDSaison + ", isPaye=" + isPaye + ", etat=" + etat + ", nom=" + nom + ", prenom=" + prenom + ", iDUser=" + iDUser + ", iDPersonne=" + iDPersonne + ", texte=" + texte + ", prix=" + prix + ", notification=" + notification + '}';
    }

}

Code throw the exception : JTA

@Stateless
public class ReservationFacade extends AbstractFacade<Reservation> implements ReservationFacadeLocal {
    @EJB
    EntityMan EntityMan;

    protected EntityManager getEntityManager() {
        return EntityMan.getEM();
    }

    public ReservationFacade() {
        super(Reservation.class);
    }

    public List<Reservationlight_SS> getAllReservationByDate(Integer idSport, Integer idSaison, Date date) {
        try{
            return getEntityManager().createNamedQuery("getAllReservationByDateSS").setParameter("IDSport", idSport).setParameter("IDSaison", idSaison).setParameter("date", date).getResultList();
        }catch(Throwable th){
            System.out.println(th.getMessage());
            throw new SportReservExceptions(getClass().getName() + " -> " + th);
        }
    }

}

Chatis
  • 47
  • 2
  • 11
  • 1
    When asking about an exception, always post the complete stack trace of the exception, and the code causing the exception. – JB Nizet Jun 29 '17 at 21:57
  • 1
    It would be helpful to share all your relevant code, including namedQuery definition. – cнŝdk Jun 29 '17 at 21:58
  • Thank you for your help, as you can see, I edit to add all the code, but I don't think there is more help on it :D – Chatis Jun 30 '17 at 07:35

3 Answers3

1

In most case exception

org.hibernate.HibernateException: collection is not associated with any session

show you that you try modify collection without any Transaction, and fixed for example following code

@Transactional
public ... methodToChangeCollection(...) {
   // you code
}

More info, see or this

Update:

You could try to use transaction manually, for example:

public List<Reservationlight_SS> getAllReservationByDate(Integer idSport, Integer idSaison, Date date) {
        try{
            EntityManager em = getEntityManager();
            em.getTransaction().begin();
            List<Reservationlight_SS> result = em.createNamedQuery("getAllReservationByDateSS").setParameter("IDSport", idSport).setParameter("IDSaison", idSaison).setParameter("date", date).getResultList();
             em.getTransaction().commit(); 
            return result;
        }catch(Throwable th){
            System.out.println(th.getMessage());
            throw new SportReservExceptions(getClass().getName() + " -> " + th);
        }
    }
Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59
1

As far as I can tell, your Entity Reservationlight_SS is not the owner of the relationship. This means that your table for the entity Personnereservationlighthas a foreign key for Reservationlight_SS. In this case, you most likely want to use mappedBy element with the OneToMany annotation.

If you were using Spring, then you would also want to check that transaction management is properly setup in your config with @EnableTransactionManagement or <tx:annotation-driven/>.

Toni Vanhala
  • 1,352
  • 6
  • 16
  • I'm not using Spring :D – Chatis Jun 30 '17 at 08:35
  • and using the `mappedBy` has the same trouble : `org.hibernate.HibernateException: collection is not associated with any session` And `Personnereservationlight` is a view and not a table. So no foreign key for sur... But you are sure that a relation `@OneToMany` could be empty ? – Chatis Jun 30 '17 at 08:40
  • Your update with `@JoinTable` annotation specifies that you have a separate DB table for the relation. Is this really the case? If not, then this is why your SQL query is not as expected. – Toni Vanhala Jun 30 '17 at 10:31
  • Yes each entity has is own table – Chatis Jun 30 '17 at 11:22
  • `JoinTable` refers to a separate (i.e., third) table for mapping the two entities together; join table is also know as a [junction table or associative table](https://en.wikipedia.org/wiki/Associative_entity). This SO answer explains it well: https://stackoverflow.com/a/5482838/8123584 – Toni Vanhala Jun 30 '17 at 12:23
  • Ooooo ok so i forget it.. and still have trouble – Chatis Jun 30 '17 at 12:58
0

in my case problem was FetchType.EAGER.

you should use @Transactional in combination with FetchType.LAZY if findBy mapped association in JPA repository

slfan
  • 8,950
  • 115
  • 65
  • 78
jivanic
  • 11
  • 1