gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 13:10:05 24 4
gpt4 key购买 nike

我有两个表,即具有 join 的用户和组织。我必须编写一个方法,该方法将根据包含输入字符串的组织名称向用户提供信息。

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

}

组织.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);

但它不起作用,因为我想根据组织名称(组织表中的列)进行搜索。

最佳答案

您在组织中没有name属性,您有organization我认为这就是您所指的。你可以做这样的事情

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

下划线(_)表示嵌套字段。但要使其发挥作用,您需要急切地获取组织。

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

关于java - 如何为 findBy Join 表列制定 JPA 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43995757/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com