我对Spring JPA还比较陌生,所以如果我的问题听起来很基础,我先道歉了。我有两个实体对象:OrderInfo和PersonInfo。类如下:
@Entity
@Table(name="order_info")
@NamedQuery(name="OrderInfo.findAll", query="SELECT o FROM OrderInfo o")
public class OrderInfo implements Serializable {
@Column(name="order_number")
private String orderNumber;
//bi-directional many-to-one association to PersonInfo
@ManyToOne
@JoinColumn(name="person_id")
private PersonInfo personInfo;
public String getOrderNumber() {
return this.orderNumber;
}
public void setOrderNumber(String orderNumber) {
this.orderNumber = orderNumber;
}
public PersonInfo getPersonInfo() {
return this.personInfo;
}
public void setPersonInfo(PersonInfo personInfo) {
this.personInfo = personInfo;
}
}
并且Person实体:
@Entity
@Table(name="person_info")
@NamedQuery(name="PersonInfo.findAll", query="SELECT p FROM PersonInfo p")
public class PersonInfo implements Serializable {
//bi-directional many-to-one association to OrderInfo
@OneToMany(mappedBy="personInfo")
private List<OrderInfo> orderInfos;
public List<OrderInfo> getOrderInfos() {
return this.orderInfos;
}
public void setOrderInfos(List<OrderInfo> orderInfos) {
this.orderInfos = orderInfos;
}
public OrderInfo addOrderInfo(OrderInfo orderInfo) {
getOrderInfos().add(orderInfo);
orderInfo.setPersonInfo(this);
return orderInfo;
}
public OrderInfo removeOrderInfo(OrderInfo orderInfo) {
getOrderInfos().remove(orderInfo);
orderInfo.setPersonInfo(null);
return orderInfo;
}
}
这两个类是使用Eclipse中JPA的create entityfromtable选项自动生成的。
现在,我正在尝试编写一个CRUDRepository来获取给定订单编号和人员ID的OrderInfo。如果在OrderInfo对象中有personId字段,我可以编写类似以下代码:
@Repository
public interface OrderRepository extends CrudRepository<OrderInfo, Integer>{
public OrderInfo findByPersonIdAndOrderNumber(@Param("personId") Long personId, @Param("orderNumber") String orderNumber);
}
然而,现在OrderInfo实体没有personId了。相反,它引用了一个Person对象(再次强调,我没有编写这些实体类。它们是由Eclipse自动生成的)。现在我该怎么编写findBy()方法呢?
先感谢您的帮助。
blablaId
的属性”的错误。 - gustavohenkefindByUserSender_Id
。参考链接:https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-property-expressions - skryvets