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?