简体   繁体   中英

Hibernate/JPA querying embeddable property of a joined subclass

I have the following entities: ShoppingCart, abstract class User and EndUser that extends User. AddressDetails is an embedable that is embeded into EndUser entity.

My query looks like this: SELECT sc FROM ShoppingCart sc JOIN sc.endUser as endUser WHERE endUser.name EQ someName and endUser.addressDetails.zip EQ 1234

When I remove the second part of the WHERE clause, and leave just the endUser.name part, everything works fine (name is a property of endUser entity class which is a subclass of User entity class). However, when I try the whole query I get:

 org.hibernate.QueryException: could not resolve property: zip of:

ShoppingCart:

@Entity
public class ShoppingCart {

   ...

   @ManyToOne(fetch = FetchType.LAZY)
   @JoinTable
   private EndUser endUser;
}

User:

@Entity
public abstract class User {
...
}

EndUser:

@Entity
public class EndUser extends User {
   ...
   @Column
   private String name;

   @Embeded
   private AddressDetails addressDetails;
   ...
}

Address Details:

@Embeddable
public class AddressDetails {
   ...
   private int zip;
   ...
}

I actually found the problem.

When I change FetchType to EAGER on @ManyToOne reladtionship between ShoppingCart and endUser the query works.

So it should be:

   @ManyToOne(fetch = FetchType.EAGER)
   @JoinTable
   private EndUser endUser;

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