简体   繁体   中英

HQL strategy to equal embedded entities

i'm curious about how HQL would assert equality between an entity instances.

Let's say I have a Entity called Person

@Entity
public class Person{

  @Id
  private Long id;

  private String name;
}

and Department

@Entity
public class Department {

  @Id
  private Long id;

  @ManyToOne
  private Person person;
}

then it's fine if I do the following statement:

Query query = getSession().createQuery("from Department d where d.person = ?");
query.setProperty(0,new Person(1L)); 

but, what if I have an Embedded entity and no pk defined? like

@Embeddable
public class Adress {

 private String email;
 private String street;  
 private Long identifier;

}

@Entity
public class Person{

  @Id
  private Long id;

  private String name;

  @Embedded
  private Address address;

}

would have any way so I could tell JPA to make it work:

Query query = getSession().createQuery("from Person p where p.address = ?");
query.setProperty(0,new Address(1L)); 

even though it's not exactly a primary key? For sure i know i'd work if I tried p.adress.identifier, and then passed just the Long value, but the point is, can I tell JPA provider how it's gonna kind of 'implement' equality my way? Thank you all

No, it is not supported and it would be difficult in general or would not make sense in some situations, like when there are collections in the Embeddable.

If you find that you need this often though, consider converting such Embeddables to custom user types . Then you can perform comparisons the way you described.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM