这是我的第一个实体:
@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,反之亦然?谢谢!
答案 0 :(得分:-1)
是的它可以理解因为基于属性。通过fromBranch的ID搜索的正确示例是:
public List<BillEntity> findByFromBranch_id(Long fromBranchId);