【发布时间】:2012-04-04 18:30:13
【问题描述】:
组件 -->
--> feature 1
--> feature 2
--> feature 3
组件与特征是一对多的关系
即使特征表中有 3 条不同的记录,hibernate Criteria 也只获取最后一条记录并显示 3 次。
我提供了我的 hbm 文件和标准代码。
从组件表中查询很好,但问题仅在于特征表
component.hbm.xml
<class name="com.arv.RelationMapping.component" table="component" >
<id name="componentPK" column="component_pk" type="java.lang.Long"/>
<property name="componentName" column="component_name" type="java.lang.String"/>
<set name="feature" table="feature" inverse="true">
<key>
<column name="component_pk"/>
</key>
<one-to-many class="com.arv.RelationMapping.feature" />
</set>
</class>
features.hbm.xml
<class name="com.arv.RelationMapping.feature" table="feature">
<id name="featurePK" column="feature_pk" type="java.lang.Long"/>
<many-to-one name="component" class="com.arv.RelationMapping.component" fetch="select">
<column name="component_pk"/>
</many-to-one>
<property name='scenarioId' column="scenario_id" type="java.lang.String"/>
<property name='scenarioDesc' column="scenario_desc" type="java.lang.String"/>
<property name='testCaseFile' column="test_case_file" type="java.lang.String"/>
</class>
Java 类
公共类测试{
public static void main(String[] args)
{
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Criteria criteria = session.createCriteria(feature.class);
List summaryList = criteria.list();
feature feature = new feature();
System.out.println(summaryList.size()); // getting size correctly
if(summaryList !=null)
{
for(Object obj:summaryList)
{
feature = (feature)obj;
// getting same values for each loop
System.out.println(feature.getScenarioDesc());
System.out.println(feature.getScenarioId());
System.out.println(feature.getFeaturePK());
}
}
session.close();
}
}
【问题讨论】:
-
SCENARIO_PK来自哪里?
标签: hibernate hibernate-mapping