使用Spring Data JPA进行搜索

时间:2018-02-28 09:37:12

标签: java spring postgresql hibernate jpa

这是我的第一个实体:

@Entity
@Table(name = "Bills")
public class BillEntity {
@Id
@GenericGenerator(name = "generator", strategy = "increment")
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;

@ManyToOne
@JoinColumn(nullable = false)
private BranchEntity fromBranch;

@ManyToOne
@JoinColumn(nullable = false)
private BranchEntity toBranch;

//Other columns
}

第二个实体:

@Entity
@Table(name = "Branches")
public class BranchEntity {
@Id
@GenericGenerator(name = "generator", strategy = "increment")
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "fromBranch")
private List<BillEntity> billsSent;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "toBranch")
private List<BillEntity> billsRecived;

//Other columns
}

一般来说,这种关系的方法是否适用于休眠,具有相同实体的双@ManyToOne@OneToMany? 我想使用提供fromBranch ID的Spring Data JPA存储库查找账单。

public interface myBillRepository extends Repository<BillEntity, Long>
{
public List<BillEntity> findByFromBranch(Long fromBranchId);
}

那么,它是否可以理解我想通过fromBranch的ID搜索,而不是使用Branch的ID,反之亦然?谢谢!

1 个答案:

答案 0 :(得分:-1)

是的它可以理解因为基于属性。通过fromBranch的ID搜索的正确示例是:

public List<BillEntity> findByFromBranch_id(Long fromBranchId);