我正在尝试使用Hibernate在一个查询中获取大量数据。
我有这个命名查询:
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
当我使用“LEFT JOIN”的“JOIN”时,我只有匹配r.personne的行。 但有时候,r.personne是空的。所以我决定使用“LEFT JOIN”和Hibernate给我这个错误:
org.hibernate.HibernateException: collection is not associated with any session
我尝试搜索是否可以将@OneToMany
作为可选项,但这是不可能的。但我很确定这可能是一种不这样做的方法吗?
这是我现在在代码中的关系:
@OneToMany(fetch = FetchType.EAGER)
@OrderBy(value = "NumReservation")
@JoinColumns({
@JoinColumn(name="NumReservation", referencedColumnName="NumeroReservation")
})
private List<Personnereservationlight> personne;
我成功通过删除“JOIN”使所有Reservationlight_SS没有personne,并且成功地让所有具有r.personne的Reservationlight_SS在另一个表中具有“JOIN”。但是当我把“LEFT JOIN”放在同一个请求中时,我不能同时拥有这两个。
有人有想法吗?
感谢您的帮助
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
@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 + '}';
}
}
@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);
}
}
}
答案 0 :(得分:1)
在大多数情况下例外
org.hibernate.HibernateException: collection is not associated with any session
向您展示您尝试修改集合而没有任何事务,并修复了例如代码
@Transactional
public ... methodToChangeCollection(...) {
// you code
}
<强>更新强>
您可以尝试手动使用交易,例如:
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);
}
}
答案 1 :(得分:1)
据我所知,您的实体Reservationlight_SS
不是该关系的所有者。这意味着实体Personnereservationlight
的表具有Reservationlight_SS
的外键。在这种情况下,您最有可能想要使用mappedBy
element with the OneToMany
annotation。
如果您使用的是Spring,那么您还需要使用@EnableTransactionManagement
或<tx:annotation-driven/>
检查配置中的transaction management is properly setup。
答案 2 :(得分:0)
在我的案例中问题是FetchType.EAGER
。
如果在JPA存储库中找到findBy映射关联,则应将@Transactional
与FetchType.LAZY
结合使用