Reputation: 411
I want to fetch data from subclass via Query Method in spring data jpa. not want to use jpql or criteria api. I want to get all Connections with specified accountType and user. I know jpql as well as criteria api will do the job but i want to achieve this with JPA Query Method. Below is my code of entities and repos.
This is main entity class
@Entity
public class Connection {
@Id
@GeneratedValue(generator = "UUID")
@GenericGenerator(
name = "UUID",
strategy = "org.hibernate.id.UUIDGenerator"
)
@Column(name = "Connection_Id", nullable = false, updatable = false)
private String id;
@Column(name = "User_Id", nullable = false)
private String userId;
@Column(name = "Name", nullable = false, length = 64)
private String name;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "Connection_Type_Id")
private ConnectionType connectionType;
// default constructor
// getter and setter
}
this is abstract and concrete entity classes
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class ConnectionType {
@Id
@GeneratedValue(generator = "UUID")
@GenericGenerator(
name = "UUID",
strategy = "org.hibernate.id.UUIDGenerator"
)
@Column(name = "Id", nullable = false)
private String id;
@OneToOne(mappedBy = "connectionType")
private Connection connection;
// default constructor
// getter setter
}
@Entity
public class EmailAccount extends ConnectionType {
@Column(name = "Username", nullable = false)
private String username;
@Column(name = "Password", nullable = false)
private String password;
@Column(name = "Account_Type", nullable = false)
@Enumerated(value = EnumType.STRING)
private AccountType accountType;
@Column(name = "FullName")
private String fullName;
// default constructor
// getter setter
}
@Entity
public class OtehrAccount extends ConnectionType {
@Column(name = "Client_Id", nullable = false)
private String clientId;
@Column(name = "Client_Secret", nullable = false)
private String secret;
// default constructor
// getter and setter
}
below is repository
public interface ConnectionRepository extends JpaRepository<Connection, String> {
// Query Method that i want to write but is not valid.
List<Connection> findByUserIdAndconnectionTypeAccountType(String userId, AccountType accountType);
}
findByUserIdAndconnectionTypeAccountType method in repo is incorrect so i just want to know weather or not is it possible to write method that will get me result based on subclass property?
Upvotes: 0
Views: 1743
Reputation: 16452
Just to clarify, "JPA Query Method" is actually "Spring Data JPA Query Methods", so this is not a Hibernate question. In JPQL/HQL this kind of subtype access usually works by using the TREAT
operator, or in case of HQL, it also works implicitly. Since the Spring Data JPA documentation does not mention the treat operator, I would say it's not possible: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-creation
Upvotes: 1
Reputation: 51
findByUserIdAndconnectionTypeAccountType
Means You Want To Fetch A Connection
By userId And The AccountType Where Is Field Of Entity ConnectionType .
Upvotes: 0