我有两张表,一张是客户,另一张是客户部门。Customer 与 customerDepartment 具有一对多的关系。
我有一个特定的搜索条件,我需要在其中搜索部门名称,如果它等于我需要检索包括客户在内的所有客户部门行。
这就是我试图得到的结果
public interface CustomerRepository extends JpaRepository<Customer,Integer>{
@Query(value="select DISTINCT c from Customer c left join c.custDept cd where cd.deptName like %?1% ")
Page<Customer> findByName(String name, Pageable pageable);
}
顾客
@Entity
@Table(name="customer")
public class Customer implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@Column(name= "customer_no",updatable = false, nullable = false)
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private int customerNo;
@Column(name= "customer_name")
private String customerName;
@Column(name= "industry")
private String industry;
@JsonManagedReference
@OneToMany(mappedBy = "customer", cascade = CascadeType.ALL,
fetch=FetchType.LAZY)
private Set<CustomerDepartment> custDept;
}
客户部:
@Entity
@Table(name = "customer_department")
public class CustomerDepartment implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@Column(name = "dept_id",updatable = false, nullable = false)
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private int depId;
@Column(name = "dept_name")
private String deptName;
@Column(name = "primary_contact")
private String primaryContact;
@JsonBackReference
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "customer_no", nullable = false)
private Customer customer;
}
如果这在 JPA 中是不可能的,我有什么办法可以做到这一点。谢谢您的帮助
元芳怎么了
相关分类