gpt4 book ai didi

java - EmbeddedId 属性上的 Hibernate Criteria 抛出 org.hibernate.QueryException : could not resolve property

转载 作者:行者123 更新时间:2023-11-30 07:39:09 32 4
gpt4 key购买 nike

使用 Hibernate Criteria API 时,我正在努力解决 Hibernate 的一个非常奇怪的行为。我正在使用 hibernate 4.3.6 Final。我有一个像这样的AnnouncementAttribute类:

@Entity
@Table(name = "announcement_attribute")
public class AnnouncementAttribute implements Serializable {

@EmbeddedId
protected AnnouncementAttributePK id;

@Column(name = "attribute_value")
private String attributeValue;
}

以及AnnouncementAttributePK类下面:

@Embeddable
public class AnnouncementAttributePK implements Serializable {
@ManyToOne
@JoinColumn(name = "attribute_id")
private Attribute attr;

@ManyToOne
@JoinColumn(name = "announcement_id")
private Announcement announcement;
}

和属性类:

@Entity
@Table(name = "attribute")
public class Attribute implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Column(name = "attribute_type")
private Integer attributeType;

@Column(name = "attribute_name")
private String attributeName;
}

我在这里省略了 getter/setter。

问题是当我尝试通过 Criteria API 访问 @EmbeddedId 属性 attr 时:

Criteria ac = session().createCriteria(AnnouncementAttribute.class);
ac.add(Restrictions.and(
Restrictions.eq("id.attr.attributeName", "someKey"),
Restrictions.eqOrIsNull("attributeValue", "someValue")));

我得到:

org.hibernate.QueryException: could not resolve property: id.attr.attributeName of: com.example.app.domain.AnnouncementAttribute

我已经尝试为EmbeddedId 创建别名 - 仍然没有成功。

最佳答案

好吧,虽然我不相信我找到的解决方案,但无论如何我都会发布它,也许有人会发现它有用,或者也许它将成为进一步讨论的好主题。

我从AnnouncementAttributePK中删除了@Embedded

public class AnnouncementAttributePK implements Serializable {
@ManyToOne
@JoinColumn(name = "attribute_id")
private Attribute attr;

@ManyToOne
@JoinColumn(name = "announcement_id")
private Announcement announcement;
}

,然后在AnnouncementAttribute中我将@EmbeddedId更改为@IdClass和@Id的组合:

@Entity
@Table(name = "announcement_attribute")
@IdClass(AnnouncementAttributePK.class)
public class AnnouncementAttribute implements Serializable {

@Id
private Attribute attr;
@Id
private Announcement announcement;

@Column(name = "attribute_value")
private String attributeValue;
}

最后是标准:

Criteria ac = session().createCriteria(AnnouncementAttribute.class)
.createAlias("attr", "atrib");
ac.add(Restrictions.and(
Restrictions.eq("atrib.attributeName", "someKey"),
Restrictions.eqOrIsNull("attributeValue", "someValue")));

经过这些更改后,一切正常。我不知道为什么......无论如何我都会进一步调查它,因为它至少很奇怪。也许这与我的应用程序在 Spring Context 中运行、使用 LocalContainerEntityManagerFactoryBean 和 spring-data-jpa 以及通过解包 EntityManager 获得 org.hibernate.Session 对象有关? :

@PersistenceContext
private EntityManager entityManager;

和 session :

private Session session(){
return entityManager.unwrap(Session.class);
}

关于java - EmbeddedId 属性上的 Hibernate Criteria 抛出 org.hibernate.QueryException : could not resolve property,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34941909/

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