如何为findBy Join表列制定JPA方法

时间:2017-05-16 08:05:48

标签: java hibernate jpa spring-boot

我有两个表,即用户和组织已加入。我必须编写一个方法,根据包含输入字符串的组织名称为用户提供。

User.java

@Entity
@Table(name = "USERS")
@EntityListeners(UsersEntityListener.class)
@NamedQueries({
@NamedQuery(name = "User.findAll", query = "SELECT u FROM User u")})
public class User extends Domain implements Serializable {

    private static final long serialVersionUID = 1L;
    @Basic(optional = false)
    @Column(name = "FIRST_NAME", nullable = false)
    private String firstName;
    @Basic(optional = false)
    @Column(name = "LAST_NAME", nullable = false)
    private String lastName;
    @Column(name = "MIDDLE_NAME")
    private String middleName;
    @Basic(optional = false)
    @Column(name = "GENDER", nullable = false)
    private String gender;
    @Basic(optional = false)
    @Column(name = "MOBILE", nullable = false)
    private String mobile;
    @Basic(optional = false)
    @Column(name = "EMAIL", nullable = false)
    private String email;
    @Basic(optional = false)
    @Column(name = "USERNAME", nullable = false)
    private String username;
    @Basic(optional = false)
    @Column(name = "PASSWORD", nullable = false)
    private String password;
    @Column(name = "URL")
    private String url;

    @PrePersist
    public void encryptPassword(){
        this.setPassword(new BCryptPasswordEncoder().encode(getPassword()));
    }


    @OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
    private List<UserRole> userRoles;
    @OneToOne(cascade = CascadeType.ALL, mappedBy = "user")
    private Organization organization;

    public User() {
    }


    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getMiddleName() {
        return middleName;
    }

    public void setMiddleName(String middleName) {
        this.middleName = middleName;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }


    public Organization getOrganization() {
        return organization;
    }

    public void setOrganization(Organization organization) {
        this.organization = organization;
    }

    public List<UserRole> getUserRoles() {
        return userRoles;
    }

    public void setUserRoles(List<UserRole> userRoles) {
        this.userRoles = userRoles;
    }

}

Organization.java

@Entity
@Table(name = "ORGANIZATIONS", uniqueConstraints = {
@UniqueConstraint(columnNames = {"USER_ID"})})
@NamedQueries({
@NamedQuery(name = "Organization.findAll", query = "SELECT o FROM Organization o")})
public class Organization extends Domain implements Serializable {

    private static final long serialVersionUID = 1L;

    @Basic(optional = false)
    @Column(name = "ORGANIZATION", nullable = false, length = 256)
    private String organization;
    @Column(name = "WEBSITE")
    private String website;

    public String getWebsite() {
        return website;
    }

    public void setWebsite(String website) {
        this.website = website;
    }

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "organization")
    private List<Location> locations;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "senderOrganization")
    private List<PurchaseOrder> sendPurchaseOrders;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "receiverOrganization")
    private List<PurchaseOrder> receivedPurchaseOrders;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "organization")
    private List<Purchaser> purchasing;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "senderOrganization")
    private List<Quotation> sendQuotations;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "receiverOrganization")
    private List<Quotation> receivedQuotations;
    @JoinColumn(name = "USER_ID", referencedColumnName = "ID", nullable = false)
    @OneToOne(optional = false)
    private User user;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "senderOrganization")
    private List<Enquiry> sendEnquiries;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "receiverOrganization")
    private List<Enquiry> receivedEnquiries;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "organization")
    private List<Supplier> supplying;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "senderOrganization")
    private List<Invoice> sendInvoices;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "receiverOrganization")
    private List<Invoice> receivedInvoices;

    public Organization() {
    }

    public String getOrganization() {
        return organization;
    }

    public void setOrganization(String organization) {
        this.organization = organization;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public List<Location> getLocations() {
        return locations;
    }

    public void setLocations(List<Location> locations) {
        this.locations = locations;
    }

    public List<PurchaseOrder> getSendPurchaseOrders() {
        return sendPurchaseOrders;
    }

    public void setSendPurchaseOrders(List<PurchaseOrder> sendPurchaseOrders) {
        this.sendPurchaseOrders = sendPurchaseOrders;
    }

    public List<PurchaseOrder> getReceivedPurchaseOrders() {
        return receivedPurchaseOrders;
    }

    public void setReceivedPurchaseOrders(List<PurchaseOrder> receivedPurchaseOrders) {
        this.receivedPurchaseOrders = receivedPurchaseOrders;
    }

    public List<Purchaser> getPurchasing() {
        return purchasing;
    }

    public void setPurchasing(List<Purchaser> purchasing) {
        this.purchasing = purchasing;
    }

    public List<Quotation> getSendQuotations() {
        return sendQuotations;
    }

    public void setSendQuotations(List<Quotation> sendQuotations) {
        this.sendQuotations = sendQuotations;
    }

    public List<Quotation> getReceivedQuotations() {
        return receivedQuotations;
    }

    public void setReceivedQuotations(List<Quotation> receivedQuotations) {
        this.receivedQuotations = receivedQuotations;
    }

    public List<Enquiry> getSendEnquiries() {
        return sendEnquiries;
    }

    public void setSendEnquiries(List<Enquiry> sendEnquiries) {
        this.sendEnquiries = sendEnquiries;
    }

    public List<Enquiry> getReceivedEnquiries() {
        return receivedEnquiries;
    }

    public void setReceivedEnquiries(List<Enquiry> receivedEnquiries) {
        this.receivedEnquiries = receivedEnquiries;
    }

    public List<Supplier> getSupplying() {
        return supplying;
    }

    public void setSupplying(List<Supplier> supplying) {
        this.supplying = supplying;
    }

    public List<Invoice> getSendInvoices() {
        return sendInvoices;
    }

    public void setSendInvoices(List<Invoice> sendInvoices) {
        this.sendInvoices = sendInvoices;
    }

    public List<Invoice> getReceivedInvoices() {
        return receivedInvoices;
    }

    public void setReceivedInvoices(List<Invoice> receivedInvoices) {
        this.receivedInvoices = receivedInvoices;
    }

    @OneToMany(mappedBy = "organization")
    private List<Tax> taxes;

    public List<Tax> getTaxes() {
        return taxes;
    }

    public void setTaxes(List<Tax> taxes) {
        this.taxes = taxes;
    }

    @OneToMany(mappedBy = "organization")
    private List<Identity> identities;

    public List<Identity> getIdentities() {
        return identities;
    }

    public void setIdentities(List<Identity> identities) {
        this.identities = identities;
    }

}

IUserRepository

@Repository
@RepositoryRestResource
public interface IUserRepository extends JpaRepository<User, UUID> {

   Page<User> findByOrganizationContaining(@Param("organization") String organization, Pageable pageable);

}

我已将此方法创建为

  Page<User> findByOrganizationContaining(@Param("organization") String organization, Pageable pageable);

但它没有成功,因为我想根据组织表中的列组织名称进行搜索。

2 个答案:

答案 0 :(得分:1)

you don't have name attribute in Organization, You have organization I think that is what you are referrring to. you can do something like this

Page<User> findByOrganization_Organization(String organization, Pageable pageable);

underscore(_) refers to nested fields. But for this to work, you need to fetch organization eagerly.

@OneToOne(cascade = CascadeType.ALL, mappedBy = "user", fetch=FetchType.EAGER)
private Organization organization;

答案 1 :(得分:0)

您好,您可以使用查询方法或类似的方法来帮助您

Page<User> findByOrganization_Organization(@Param(organization)String organization, Pageable pageable);