gpt4 book ai didi

java - Hibernate 与 createCriteria 和 createAlias 的关联

转载 作者:行者123 更新时间:2023-11-29 09:35:09 38 4
gpt4 key购买 nike

我有两个简单的映射:

<class name="org.testing.Person" table="PERSON">
<id name="personId" type="long" column="PERSON_ID">
<generator class="native"/>
</id>
<property name="personName" type="string" not-null="true" column="PERSON_NAME"/>
<many-to-one name="personAddress" class="org.testing.Address" column="PERSON_ADDRESS" not-null="true" cascade="all" unique="true"/>
</class>

<class name="org.testing.Address" table="ADDRESS">
<id name="addressId" type="long" column="ADDRESS_ID">
<generator class="native" />
</id>
<property name="street" column="ADDRESS_STREET" type="string" />
<property name="city" column="ADDRESS_CITY" type="string" />
<property name="state" column="ADDRESS_STATE" type="string" />
</class>

我尝试像这样获取个人地址的属性:

session.createCriteria(Person.class)
.add(Restrictions.eq("personName", "Frodo"))
.createAlias("personAddress", "pa")
.setProjection(Projections.property("pa.street"))
.list();

并且有效。比这样:

session.createCriteria(Person.class)
.add(Restrictions.eq("personName", "Frodo"))
.createCriteria("personAddress")
.setProjection(Projections.property("street"))
.list();

它抛出:org.hibernate.QueryException:无法解析属性:街道:org.testing.Person。我认为两者应该给出相同的结果。我哪里错了?提前致谢!

最佳答案

.setProjection(Projections.property("street")) 始终对根条件进行操作。要投影连接的项目,您必须使用您发布的第一个版本。

关于java - Hibernate 与 createCriteria 和 createAlias 的关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8719877/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com