我知道这是一个老生常谈的问题,但我找不到解决方案。我有两个豆子,其中一个有HashMap
收藏品。尝试阅读此集合时遇到异常。已指定映射配置以急切地加载此集合。
我的环境是:
- 休眠 4.2.0
- mysql-connector-java 5.1.24
我还有两个豆子:
public class FeaturedDoc {
private Long id;
private Map<Feature, Float> features;
public FeaturedDoc() {
features = new HashMap<Feature, Float>();
}
(getters and setters)
}
和
public class Feature {
private Long id;
private String name;
private Long internalId;
(getters and setters)
}
这个bean有映射:
<class name="Feature" table="FEATURE">
<id name="id" type="long" column="ID">
<generator class="increment"/>
</id>
<property name="name" length="255" type="string" unique="true" column="NAME" index="INDEX_NAME"/>
<property name="internalId" type="long" unique="true" not-null="false" column="INTERNAL_ID" index="INDEX_INTID"/>
<sql-insert>insert into FEATURE (NAME, INTERNAL_ID, ID) values (?, ?, ?) on duplicate key update ID = ID</sql-insert>
</class>
<class name="FeaturedDoc" table="FEATURED_DOC">
<id name="id" type="long" column="ID">
<generator class="increment"/>
</id>
<map name="features" table="DOC_FEATURE" cascade="all" lazy="false" fetch="join">
<key column="ID"></key>
<map-key-many-to-many column="FEATURE_ID" class="Feature"/>
<element column="value" type="float"/>
</map>
</class>
我也有带有方法的DAO层:
public FeaturedDoc read(long id) {
FeaturedDoc fd = null;
try {
session.beginTransaction();
fd = session.get(FeaturedDoc.class, id);
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
} finally {
close();
}
return fd;
}
当我试图做这样的事情时:
FeaturedDoc fd = daoService.read(26);
System.out.println(fd.getFeatures());
我遇到了一个例外
org.hibernate.LazyInitializationException:无法初始化代理 - 没有会话
你知道我应该如何解决这个错误吗?