Hibernate Criteria 多表查询
代码:
1. Affix类
public class Affix {
private int id;
private String name;
private String size;
private Integer width;
private Integer height;
private Role role;
2.Role 类
public class Role {
private int id;
private String name;
private String remark;
- mapping 配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.cn.domain">
<class name="Affix" table="sys_affix" catalog="test">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name" column="name" length="20"></property>
<property name="size" column="size" length="50"></property>
<property name="width" column="width" length="50" type="java.lang.Integer"></property>
<property name="height" column="height" length="50" type="java.lang.Integer"></property>
</class>
<class name="Role" table="sys_role" catalog="test">
<id name="id" column="id">
<!-- 主键生成策略 -->
<generator class="native"></generator>
</id>
<property name="name" column="name" length="20"></property>
<property name="remark" column="remark" length="50"></property>
</class>
</hibernate-mapping>
4.Dao层
public class AffixDao {
public List selectAffix(){
Session session = HibernateUtils.openSession();
session.beginTransaction();
Criteria criteria = session.createCriteria(Affix.class);//1.总的查询容器
criteria.createAlias("role","r");
criteria.add(Restrictions.eq("r.id", 1));
List list = criteria.list();
List<Affix> lists = list;
System.out.println("查询结果条数:"+list.size());
session.close();
return list;
}
}
5.执行结果
org.hibernate.QueryException: could not resolve property: role of: com.cn.domain.Affix
at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:62)
at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:56)
at org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1753)
at org.hibernate.loader.criteria.EntityCriteriaInfoProvider.getType(EntityCriteriaInfoProvider.java:39)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getPathInfo(CriteriaQueryTranslator.java:228)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.createCriteriaEntityNameMap(CriteriaQueryTranslator.java:212)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.(CriteriaQueryTranslator.java:94)
at org.hibernate.loader.criteria.CriteriaLoader.(CriteriaLoader.java:71)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1760)
at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:363)
at com.cn.dao.AffixDao.selectAffix(AffixDao.java:45)
at com.cn.dao.AffixDaoTest.testSelectAffix(AffixDaoTest.java:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
问题:
网上搜过好多类似的实例代码,别人都可以执行成功,我这里一直报role在Affix中映射异常,有没有大神可以指点一下?谢谢