samedi 28 février 2015

Writing CRUDRepository's findBy() method on a field annotated by JoinColumn

I'm kind of new to Spring JPA, so I apologize in advance if my question sounds basic. I have 2 entity objects: OrderInfo, and PersonInfo. The classes are as below:



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


}


And the Person entity:



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

}


These two classes were auto-generated in Eclipse using JPA's create entityfromtable option.


Now, I'm trying to write a CRUDRepository to get OrderInfo given an orderNumber, and personId. If I had a personId field in the OrderInfo object, I could have written something like



@Repository
public interface OrderRepository extends CrudRepository<OrderInfo, Integer>{
public OrderInfo findByPersonIdAndOrderNumber(@Param("personId") Long personId, @Param("orderNumber") String orderNumber);

}


However, now the OrderInfo entity does not have a personId. Instead, it has a reference to a Person (Again, I did not write the entity classes. They were auto-generated by Eclipse). How should I write the findBy() method now?


Thanks in advance.


Aucun commentaire:

Enregistrer un commentaire