jpa @Inheritance(strategy = InheritanceType.JOINED)如何选择不包含子的超实体

时间:2016-11-09 13:44:16

标签: spring-data-jpa

我有疑问:我们假设我有两个类用户和人,人扩展用户。在用户中我有@Inheritance(strategy=InheritanceType.JOINED)。有时我只想选择不包含子表信息的用户信息,

User.java(超类)

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name = "user_type")
@Table(name = "users")
public class User {.........}

Person.java

@Entity
public class Person extends User {......}

Company.java

@Entity
public class Company extends User {.............}

UserRepository.java

@Transactional
public interface UserRepository extends UserBaseRepository<User> {

//@Query(value="select u from User u where u.email=?1")
@Query(value="select u.id,u.email from User u where u.email=?1")
User getUser(String email);

@Query("select u from User u where u.email=?1")
User getUser2(String email);

@Query(value="select * from Users where email=:email",nativeQuery=true)
User getUser3(String email);
}

发生以下异常:

Hibernate: select user0_.id as col_0_0_, user0_.email as col_1_0_ from users user0_ where user0_.email=?
org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.Object[] to type netgloo.models.User for value '{1, 635@qq.com}';
nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type java.lang.Long to type netgloo.models.User
  1. 如何获取用户信息不包含Person,Company
  2. 如何获取人员信息不包含用户,公司

1 个答案:

答案 0 :(得分:1)

要仅选择您可以使用JPQL TYPE()运算符的用户,请执行以下操作:

SELECT u FROM User u WHERE TYPE(u) := User

以下是完整的解释:https://stackoverflow.com/a/3766541/534877