对于父对象和继承对象

时间:2016-02-17 11:54:28

标签: sql jpa jpa-2.0 jpql

Entities/Model:

@Inheritance(strategy=InheritanceType.JOINED)
public class UserAccount implements CommonUserAccount {
    @Id
    private Long id;

    private String email;

    @Embedded
    private PersonalInfo personalInfo = new PersonalInfo(); // name/surname - regular stuff

    @ElementCollection
    @CollectionTable(name = "UserAccountTags", joinColumns = @JoinColumn(name = "accountId", nullable = false))
    @Column(name = "tag")
    //@Transient
    private Set<String> tags = new HashSet<String>();

    @ElementCollection
    @CollectionTable(name = "UserAccountRoles", joinColumns = @JoinColumn(name = "accountId", nullable = false))
    @Enumerated(EnumType.STRING)
    @Column(name = "userRole")
    private Set<UserAccountRole> userRoles = new HashSet<UserAccountRole>();

    // regular getters/setters
}

@Entity
@Table
@PrimaryKeyJoinColumn(name = "useraccountid")
public class DemoUserAccount extends UserAccount implements CommonUserAccount {

    @Column
    private String passwordHash;

    @Column
    private Long failedLogins;

    @Column
    @Temporal(TemporalType.TIMESTAMP)
    Date lockedAt;

    // regular getters/setters
}

问题: 是否可以使用JPQL(对于JPA2.0)构建查询,该JPQL将返回在父表上加入的DemoUserAccounts - UserAccounts?这样做可以假设我也可以过滤标签/ user_roles。通常,某些记录不会填写DemoUserAccount特定字段。

1 个答案:

答案 0 :(得分:0)

当您从DemoUserAccount执行SELECT时,您已经可以使用UserAccount字段进行查询。

因此,如果您想按emailfailedLogins进行过滤:

SELECT d FROM DemoUserAccount d WHERE d.email = 'you@you.com' AND d.failedLogins > 3