Java 嵌入id成员值的休眠条件

Java 嵌入id成员值的休眠条件,java,hibernate,criteria,Java,Hibernate,Criteria,我想找到一个实体,它使用一个限制第二个实体属性值的准则,该属性是我的第一个实体的嵌入id的成员 第一实体: @Entity public class Car { @EmbeddedId private Id id = new Id(); private String color; @Embeddable public static class Id implements Serializable { private static f

我想找到一个实体,它使用一个限制第二个实体属性值的准则,该属性是我的第一个实体的嵌入id的成员

第一实体:

@Entity
public class Car {

    @EmbeddedId
    private Id id = new Id();

    private String color;

    @Embeddable
    public static class Id implements Serializable {

        private static final long serialVersionUID = -8141132005371636607L;

        @ManyToOne
        private Owner owner;

        private String model;

        // getters and setters...
        // equals and hashcode methods

    }

    // getters and setters...

}
第二实体:

@Entity
public class Owner {

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

    @OneToMany (mappedBy = "id.owner")
    private List<Car> cars;

    // getters and setters...

}
获得我想要的东西的正确方法是什么

更新

我在hql中尝试过:

String hql = "FROM Car as car where car.color = :color and car.id.model = :model and car.id.owner.firstname = :firstname";

Query query = em.createQuery(hql);
query.setParameter("color", "black");
query.setParameter("model", "batmobile");
query.setParameter("firstname", "Bruce");

List<Car> cars = query.getResultList();
String hql=“FROM Car as Car where Car.color=:color and Car.id.model=:model and Car.id.owner.firstname=:firstname”;
Query Query=em.createQuery(hql);
query.setParameter(“颜色”、“黑色”);
query.setParameter(“model”、“batmobile”);
setParameter(“firstname”、“Bruce”);
List cars=query.getResultList();

这是可行的,但是有没有一种方法可以通过标准来做到这一点?

您忘记在firstname和lastname字段(以及Car中的color字段)的顶部添加
@列
注释。在hibernate中,如果一个字段没有注释,它就不会将其识别为数据库字段

注意:您可以在getter上使用列注释,但没有显示getter。两个地方都可以


看看HQL返回的是什么,特别是语句(格式化以便于阅读):

看起来hibernate正在按照别名的指示将字段“id.owner”转换为“o”,但由于某些原因,它没有按预期写入“id.owner=o”。您可能想研究一下它为什么会这样做。

因为有一个解决办法

您必须将
@EmbeddedId
owner
在本例中)所需的关系属性复制到主实体(
Car
),并使用
insertable=false,updateable=false
,如下所示

@Entity
public class Car {

    @EmbeddedId
    private Id id = new Id();

    private String color;

    @ManyToOne
    @JoinColumn(name = "column_name", insertable = false, updatable = false)
    private Owner owner;

    @Embeddable
    public static class Id implements Serializable {

        private static final long serialVersionUID = -8141132005371636607L;

        @ManyToOne
        private Owner owner;

        private String model;

        // getters and setters...
        // equals and hashcode methods

    }

    // getters and setters...

}
然后直接创建别名,而不是使用复合id属性

List<Car> cars = session.createCriteria(Car.class)
      .add(Restrictions.eq("color", "black"))
      .add(Restrictions.eq("id.model", "batmobile"))

      .createAlias("owner", "o")

      .add(Restrictions.eq("o.firstname", "Bruce"))
      .list();
List cars=session.createCriteria(Car.class)
.添加(限制.eq(“颜色”、“黑色”))
.add(Restrictions.eq(“id.model”、“batmobile”))
.createAlias(“所有者”、“o”)
.add(Restrictions.eq(“o.firstname”、“Bruce”))
.list();

您是否尝试过删除别名并仅使用
.add(Restrictions.eq(“id.owner.firstname”、“Bruce”)
?事实上,这是我第一次尝试的。但它也不起作用
org.hibernate.QueryException:无法解析属性:id.owner.firstname of:my.package.domain.Car
我有类似的问题(hibernate 3.3.2),可能是一个bug。这不是注释问题。我不知道这是否取决于hibernate(或JPA)的版本,但@Column注释不是必须的。
select 
    this_.model as model1_0_0_, 
    this_.owner_id as owner_id3_0_0_, 
    this_.color as color2_0_0_ 
from Car this_ 
where 
    this_.color=? 
    and this_.model=? 
    and o1_.firstname=?
@Entity
public class Car {

    @EmbeddedId
    private Id id = new Id();

    private String color;

    @ManyToOne
    @JoinColumn(name = "column_name", insertable = false, updatable = false)
    private Owner owner;

    @Embeddable
    public static class Id implements Serializable {

        private static final long serialVersionUID = -8141132005371636607L;

        @ManyToOne
        private Owner owner;

        private String model;

        // getters and setters...
        // equals and hashcode methods

    }

    // getters and setters...

}
List<Car> cars = session.createCriteria(Car.class)
      .add(Restrictions.eq("color", "black"))
      .add(Restrictions.eq("id.model", "batmobile"))

      .createAlias("owner", "o")

      .add(Restrictions.eq("o.firstname", "Bruce"))
      .list();