如何在Hibernate中查询嵌入对象?

时间:2016-03-25 03:46:49

标签: java hibernate hql hibernate-criteria

我有A,B类

inSampleSize

现在我有一堆A类objs Set,我想查询数据库,以便在B类中包含相同的x和y(在这种情况下z比较重要)应该选择,我该怎么办实现那个?

它就像SQL中的“In”,但由于我在比较嵌入式对象,我该怎么做呢?非常感谢!!

1 个答案:

答案 0 :(得分:1)

鉴于课程:

public class RandomEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Embedded
    private EmbeddableEntity embedded;
}

@Embeddable
public class EmbeddableEntity {
    private Long valueA;
    private Long valueB;
}

您可以像下面这样查询:

session.createQuery( "select a from RandomEntity a where a.embedded.valueA=:value" ).setParameter( "value", 1L ).list();

Hibernate: select randomenti0_.id as id1_2_, randomenti0_.valueA as valueA2_2_, randomenti0_.valueB as valueB3_2_ from RandomEntity randomenti0_ where randomenti0_.valueA=?

或:

session.createQuery( "select a from RandomEntity a where a.embedded.valueA in (:value)" ).setParameterList( "value", Arrays.asList( 1L, 2L ) ).list();

Hibernate: select randomenti0_.id as id1_2_, randomenti0_.valueA as valueA2_2_, randomenti0_.valueB as valueB3_2_ from RandomEntity randomenti0_ where randomenti0_.valueA in (?)