yonggeweiwubaqi 2020-01-17 15:36 采纳率: 50%
浏览 342
已采纳

Hibernate Criteria 多表查询实体中有另一个实体 查询结果映射异常?

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;
  1. 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中映射异常,有没有大神可以指点一下?谢谢

  • 写回答

1条回答 默认 最新

  • 臭小子帅 2020-01-17 16:59
    关注

    配置下两个对象之间的映射关系,参考下面的
    https://blog.csdn.net/qq_42246118/article/details/82555472

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥25 C语言代码,大家帮帮我
  • ¥15 请问以下文字内容及对应编码是用了什么加密算法或压缩算法呢?
  • ¥50 关于#html5#的问题:H5页面用户手机返回的时候跳转到指定页面例如(语言-javascript)
  • ¥15 无法使用此凭据登录,因为你的域不可用,如何解决?(标签-Windows)
  • ¥15 yolov9的训练时间
  • ¥15 二叉树遍历没有报错但无法正常运行
  • ¥15 在linux系统下vscode运行robocup3d上场球员报错
  • ¥15 Python语言实验
  • ¥15 SAP HANA SQL 增加合计行
  • ¥20 用C#语言解决一个英文打字练习器,有偿