有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Hibernate集合初始化问题

我刚开始冬眠,我面临一个问题。有一个现有类正在使用Hibernate加载。我想给那个类添加一个集合。并在现有父hbm中添加此块。xml

<set name="assocication" table="AssociationLinks"  lazy="false" access="field" cascade="lock">
      <cache usage="read-only" region="UpstreamLink.AssociationLinks"/>
        <key column="COUNTRY_TKEY" foreign-key="COUNTRY_ID" property-ref="countryID"/>
        <one-to-many class="com.AssociationLinks" />
    </set> 

儿童Hbm。xml

<class name="AssociationLinks" table="AssociationLinks">
    <cache usage="read-only" region="UpstreamLink.AssociationLinks" />
    <id name="countryID" type="string" column="COUNTRY_ID" access="field" />

    <property name="entityName" column="ENTITY_NAME" type="java.lang.String" access="field" />


</class>

当我在parent中添加Lazy=true时,我得到一个异常:

  016-07-05 03:49:43,680 ERROR  org.hibernate.LazyInitializationException - failed to lazily initialize a collection of role: com.MainData.association, no session or session was closed
    org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.MainData.association, no session or session was closed
            at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
            at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
            at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343)
            at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)

但是当我将lazy=false设置为false时,我在一个地方看到了这个值,但是在访问父类的父类的地方,我得到了下面的错误

2016-07-05 07:49:00,005 INFO   org.hibernate.event.def.DefaultLoadEventListener - Error performing load command
org.hibernate.HibernateException: collection is not associated with any session
        at org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:449)
        at org.hibernate.engine.StatefulPersistenceContext.initializeNonLazyCollections(StatefulPersistenceContext.java:794)
        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:241)
        at org.hibernate.loader.Loader.loadEntity(Loader.java:1860)

共 (0) 个答案