xf 李
xf 李

Reputation: 21

jpa @Inheritance(strategy=InheritanceType.JOINED) how to select super entity not contain child

I have question: Let's assume I have two classes User and person, person extends User. In User I have @Inheritance(strategy=InheritanceType.JOINED). Sometimes I only want to select the user info not contain the child table info,

User.java(super class)

@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);
}

The following exception occurs:

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, [email protected]}';
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. how to get the user info not contain Person,Company
  2. how to get the Person info not contain user ,Company

Upvotes: 2

Views: 3192

Answers (1)

Robert Niestroj
Robert Niestroj

Reputation: 16131

To select only the user you can use the JPQL TYPE() operator like this:

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

Here is complete explaination: https://stackoverflow.com/a/3766541/534877

Upvotes: 2

Related Questions