本文介绍了如何毫无问题地使用 JPA 2.0 @ManyToMany的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在开发中使用 JPA 2.0Spring.我的实体类包含两个 @ManyToMany 关系.

@Entity("支付")公共类 PaymentData 实现 Serializable{私长pk;私人收藏<PaymentItemData>付款项;/*** 次要付款项**/私人收藏<MinorPayItemData>次要付款项;@ManyToMany(fetch=FetchType.EAGER)@JoinTable(name = "payitem_m_assig",加入列 =@JoinColumn(name = "pay_item_id", nullable = false),inverseJoinColumns =@JoinColumn(name = "minor_pay_item_id", nullable = false))公共集合<MinorPayItemData>getMinorPaymentItem(){返回minorPaymentItem;}/*** @param minorPaymentItem 要设置的 minorPaymentItem*/公共无效 setMinorPaymentItem(最终集合 值){this.minorPaymentItem = 价值;}@ManyToMany(fetch=FetchType.EAGER)@JoinTable(name = "payitem_assigned",加入列 =@JoinColumn(name = "pay_item_id", nullable = false),inverseJoinColumns =@JoinColumn(name = "pay_item_id", nullable = false))公共集合<PaymentItemData>获取付款项(){返回付款项;}/*** 设置属性paymentItem** @param 值 -paymentItem**/public void setPaymentItem(final Collection value){this.paymentItem = 价值;}}

当我运行查询以从数据库中的支付表中检索记录时,例如

Query q = manager.createQuery("select a from PaymentData a");q.getResultList();

如果我允许 @ManyToMany 上的 fetch=FetchType.EAGER,我会收到以下错误

引起:org.hibernate.loader.MultipleBagFetchException:不能同时获取多个包在 org.hibernate.loader.BasicLoader.postInstantiate(BasicLoader.java:94)在 org.hibernate.loader.entity.EntityLoader.<init>(EntityLoader.java:119)在 org.hibernate.loader.entity.EntityLoader.<init>(EntityLoader.java:71)在 org.hibernate.loader.entity.EntityLoader.<init>(EntityLoader.java:54)在 org.hibernate.loader.entity.BatchingEntityLoader.createBatchingEntityLoader(BatchingEntityLoader.java:133)在 org.hibernate.persister.entity.AbstractEntityPersister.createEntityLoader(AbstractEntityPersister.java:1914)在 org.hibernate.persister.entity.AbstractEntityPersister.createEntityLoader(AbstractEntityPersister.java:1937)在 org.hibernate.persister.entity.AbstractEntityPersister.createLoaders(AbstractEntityPersister.java:3205)在 org.hibernate.persister.entity.AbstractEntityPersister.postInstantiate(AbstractEntityPersister.java:3191)在 org.hibernate.persister.entity.SingleTableEntityPersister.postInstantiate(SingleTableEntityPersister.java:728)在 org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:348)在 org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1845)在 org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:906)... 39 更多

但如果我删除 fetch=FetchType.EAGER 并将其保留为 @ManyToMany,我将遇到异常

org.hibernate.LazyInitializationException: 延迟初始化角色集合失败:com.data.PaymentData.paymentItem,没有会话或会话被关闭在 org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383)在 org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375)在 org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:368)在 org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111)在 org.hibernate.collection.PersistentBag.toString(PersistentBag.java:506)在 java.lang.String.valueOf(String.java:2826)在 java.lang.StringBuilder.append(StringBuilder.java:115)在 com.niu.util.Util.toString(Util.java:131)在 com.niu.util.data.BaseData.toString(BaseData.java:107)在 java.lang.String.valueOf(String.java:2826)在 java.lang.StringBuilder.append(StringBuilder.java:115)在 java.util.AbstractCollection.toString(AbstractCollection.java:422)在 java.lang.String.valueOf(String.java:2826)在 java.io.PrintStream.println(PrintStream.java:771)在 org.apache.tomcat.util.log.SystemLogHandler.println(SystemLogHandler.java:269)在 sun.reflect.GeneratedMethodAccessor1861.invoke(未知来源)在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)在 java.lang.reflect.Method.invoke(Method.java:597)在 com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452)在 com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291)在 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254)在 com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)在 com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)在 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)在 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)在 com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:133)在 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)在 com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)在 com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)在 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)在 com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)在 com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)在 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)在 com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)在 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)在 org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)在 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)在 org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)在 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)在 com.niu.web.common.interceptor.ApplicationModelDrivenInterceptor.intercept(ApplicationModelDrivenInterceptor.java:31)在 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)在 com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)在 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)在 org.apache.struts2.interceptor.ProfilingActivationInterceptor.intercept(ProfilingActivationInterceptor.java:104)在 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)在 org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:270)在 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)在 com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)在 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)在 com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)在 com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)在 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)在 com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)在 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)在 org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)在 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)在 com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:190)在 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)在 com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)在 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)在 org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)在 org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:498)在 org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)在 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)在 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)在 org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)在 org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)在 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)在 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)在 org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)在 org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)在 org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317)在 org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204)在 org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311)在 java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)在 java.lang.Thread.run(Thread.java:662)

我做错了什么

解决方案

为了避免MultipleBagFetchException,不要使用FetchType.EAGER,尝试使用@LazyCollection(LazyCollectionOption.FALSE) 如本例所示:

@ManyToMany@LazyCollection(LazyCollectionOption.FALSE)@JoinTable(name = "payitem_m_assig", joinColumns = @JoinColumn(name = "pay_item_id", nullable = e), inverseJoinColumns = @JoinColumn(name = "minor_pay_item_id", nullable = false))公共集合<MinorPayItemData>getMinorPaymentItem(){返回minorPaymentItem;}

这是来自 docs 的简短描述:

  • @LazyCollection:定义@ManyToMany 和@OneToMany 关联.LazyCollectionOption 可以是 TRUE(集合是惰性的,将在访问其状态时加载),EXTRA(集合是惰性的,所有操作都会尽量避免集合加载,这对于大型集合特别有用当不需要加载所有元素时)和 FALSE(关联不懒)

  • @Fetch:定义用于加载关联的获取策略.FetchMode可以是SELECT(关联时触发一个select需要加载),SUBSELECT(仅适用于集合,使用子选择策略 - 请参考 Hibernate Reference有关更多信息的文档)或 JOIN(使用 SQL JOIN 加载加载所有者实体时的关联).JOIN 覆盖任何惰性属性(通过 JOIN 策略加载的关联不能懒惰).

希望对你有帮助

I am using JPA 2.0 and Spring in my development. My entity class contains two @ManyToMany relationships.

@Entity("payment")
 public class PaymentData implements Serializable
{
    private Long pk;

    private Collection<PaymentItemData> paymentItem;
    /**
     *  minorPaymentItem
     *
     */
    private Collection<MinorPayItemData> minorPaymentItem;

    @ManyToMany(fetch=FetchType.EAGER)
    @JoinTable(name = "payitem_m_assig",
    joinColumns =
    @JoinColumn(name = "pay_item_id", nullable = false),
    inverseJoinColumns =
    @JoinColumn(name = "minor_pay_item_id", nullable = false))
    public Collection<MinorPayItemData> getMinorPaymentItem()
    {
        return minorPaymentItem;
    }

    /**
     * @param minorPaymentItem the minorPaymentItem to set
     */
    public void setMinorPaymentItem(final Collection<MinorPayItemData> value)
    {
        this.minorPaymentItem = value;
    }

    @ManyToMany(fetch=FetchType.EAGER)
    @JoinTable(name = "payitem_assigned",
    joinColumns =
    @JoinColumn(name = "pay_item_id", nullable = false),
    inverseJoinColumns =
    @JoinColumn(name = "pay_item_id", nullable = false))
    public Collection<PaymentItemData> getPaymentItem()
    {
        return paymentItem;
    }

    /**
     * Set the property paymentItem
     *
     * @param value -paymentItem
     *
     */
    public void setPaymentItem(final Collection<PaymentItemData> value)
    {
        this.paymentItem = value;
    }

}

When i run a query to retrieve records from the payment table in the database, like

Query q = manager.createQuery("select a from PaymentData a");
q.getResultList();

If i allow the fetch=FetchType.EAGER on the @ManyToMany, I get the following errors

Caused by: org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags
    at org.hibernate.loader.BasicLoader.postInstantiate(BasicLoader.java:94)
    at org.hibernate.loader.entity.EntityLoader.<init>(EntityLoader.java:119)
    at org.hibernate.loader.entity.EntityLoader.<init>(EntityLoader.java:71)
    at org.hibernate.loader.entity.EntityLoader.<init>(EntityLoader.java:54)
    at org.hibernate.loader.entity.BatchingEntityLoader.createBatchingEntityLoader(BatchingEntityLoader.java:133)
    at org.hibernate.persister.entity.AbstractEntityPersister.createEntityLoader(AbstractEntityPersister.java:1914)
    at org.hibernate.persister.entity.AbstractEntityPersister.createEntityLoader(AbstractEntityPersister.java:1937)
    at org.hibernate.persister.entity.AbstractEntityPersister.createLoaders(AbstractEntityPersister.java:3205)
    at org.hibernate.persister.entity.AbstractEntityPersister.postInstantiate(AbstractEntityPersister.java:3191)
    at org.hibernate.persister.entity.SingleTableEntityPersister.postInstantiate(SingleTableEntityPersister.java:728)
    at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:348)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1845)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:906)
    ... 39 more

But if i remove fetch=FetchType.EAGER and leave it as @ManyToMany, i will have the exception

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.data.PaymentData.paymentItem, no session or session was closed
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383)
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375)
    at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:368)
    at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111)
    at org.hibernate.collection.PersistentBag.toString(PersistentBag.java:506)
    at java.lang.String.valueOf(String.java:2826)
    at java.lang.StringBuilder.append(StringBuilder.java:115)
    at com.niu.util.Util.toString(Util.java:131)
    at com.niu.util.data.BaseData.toString(BaseData.java:107)
    at java.lang.String.valueOf(String.java:2826)
    at java.lang.StringBuilder.append(StringBuilder.java:115)
    at java.util.AbstractCollection.toString(AbstractCollection.java:422)
    at java.lang.String.valueOf(String.java:2826)
    at java.io.PrintStream.println(PrintStream.java:771)
    at org.apache.tomcat.util.log.SystemLogHandler.println(SystemLogHandler.java:269)
    at sun.reflect.GeneratedMethodAccessor1861.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452)
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254)
    at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:133)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at com.niu.web.common.interceptor.ApplicationModelDrivenInterceptor.intercept(ApplicationModelDrivenInterceptor.java:31)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at org.apache.struts2.interceptor.ProfilingActivationInterceptor.intercept(ProfilingActivationInterceptor.java:104)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:270)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:190)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:498)
    at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
    at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)

What am i doing wrong

解决方案

To avoid the MultipleBagFetchException, instead of using FetchType.EAGER, try using the @LazyCollection(LazyCollectionOption.FALSE) as in this example:

@ManyToMany
@LazyCollection(LazyCollectionOption.FALSE)
@JoinTable(name = "payitem_m_assig", joinColumns = @JoinColumn(name = "pay_item_id", nullable = e), inverseJoinColumns = @JoinColumn(name = "minor_pay_item_id", nullable = false))
public Collection<MinorPayItemData> getMinorPaymentItem()
{
    return minorPaymentItem;
}

Here is a small description of from the docs:

I hope it helps

这篇关于如何毫无问题地使用 JPA 2.0 @ManyToMany的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-22 05:06