我的多对一映射中存在性能问题。当我在日志文件中调试SQL查询时,主要查询没问题,但在我有其他查询表示多对一对象映射之后。
Entity.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="com.omb.database.mapping.MyEntity" table="MY_ENTITY">
<id name="id" type="java.lang.Integer">
<column name="ENTITY_ID"/>
<generator class="sequence">
<param name="sequence">SEQ_MY_ENTITY</param>
</generator>
</id>
<property name="prop1" type="string" column="PROP1" />
<many-to-one name="object1" column="OBJECT1_ID" class="com.omb.database.mapping.Object1" />
<many-to-one name="object2" column="OBJECT2_ID" class="com.omb.database.mapping.Object2" />
</class>
</hibernate-mapping>
Object1.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping default-lazy="true">
<class name="com.omb.database.mapping.Object1" table="TABLE_OBJECT_1">
<id name="id" type="java.lang.Integer" column="OBJECT1_ID" />
<property name="label" type="string" column="LABEL_OBJECT_1" length="15" />
</class>
</hibernate-mapping>
Object2.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping default-lazy="true">
<class name="com.omb.database.mapping.Object2" table="TABLE_OBJECT_2">
<id name="id" type="java.lang.Integer" column="OBJECT2_ID" />
<property name="label" type="string" column="LABEL_OBJECT_2" length="15" />
</class>
</hibernate-mapping>
查询HBM:
public List<Entity> findByObject1Id(Integer object1Id) throws DataAccesException {
List<Entity> results = null;
try {
Query query = this.getSession().createQuery(
"from Entity ent where ent.object1.id = :object1Id");
query.setParameter("object1Id", object1Id);
results = query.list();
} catch (HibernateException hbe) {
throw new DataAccesException(hbe);
}
return results;
}
在pom.xml中
<!-- Hibernate 3 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>3.2.6.ga</version>
<exclusions>
<exclusion>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
</exclusion>
<exclusion>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</exclusion>
<exclusion>
<groupId>asm</groupId>
<artifactId>asm-attrs</artifactId>
</exclusion>
</exclusions>
</dependency>
答案 0 :(得分:3)
您的映射似乎没问题。正如此处5.1.1. Entity
所述 <class>
属性 lazy 默认为true
<many-to-one>
:5.1.7.1. Using a foreign key or an association table lazy 属性相同:
lazy="no-proxy"
指定在首次访问实例变量时应该懒惰地获取属性。这需要构建时字节码检测。 lazy="false"
指定始终紧急提取关联。那么问题出在哪里?
我会在你的调试窗口中说。因为您确实有对列表的引用,并且您正在观察结果 - 在执行它的那一刻 - 也会加载引用。懒惰 - 但装满了。事实上,这就是我们想要的。代理 - 首次调试时 - 正在强制加载。
尝试将其从手表中删除。或者关闭会话,然后把它放在手表中...你应该看到,上面使用的查询 - 不加载引用...只有在真正访问时...甚至通过调试窗口
答案 1 :(得分:2)
您是否尝试使用FetchMode.SELECT,就像这样;
<many-to-one name="object1" column="OBJECT1_ID" class="com.omb.database.mapping.Object1" fetch="select" />