我有A,B类
class A{
@Embedded
private B objB;
}
@Embeddable
class B{
Integer x;
Integer y;
float z;
}
现在我有一堆A类objs集,我想查询数据库,以便在行B中包含相同的x和y(在这种情况下,z不重要)应该被选择,我如何实现呢?
就像SQL中的“ In”一样,但是由于我正在比较嵌入式对象,因此该如何处理?非常感谢!!
最佳答案
给定的类:
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 (?)