问题描述
我是Spring JPA的新手,所以如果我的问题听起来很简单,我会提前道歉.我有2个实体对象:OrderInfo和PersonInfo.这些类如下:
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;
}
}
还有Person实体:
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;
}
}
这两个类是使用JPA的create entityfromtable选项在Eclipse中自动生成的.
These two classes were auto-generated in Eclipse using JPA's create entityfromtable option.
现在,我正在尝试编写一个CRUDRepository以获取给定orderNumber和personId的OrderInfo.如果我在OrderInfo对象中有一个personId字段,我可能会写类似
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);
}
但是,现在OrderInfo实体没有personId.相反,它具有对Person的引用(同样,我没有编写实体类.它们是由Eclipse自动生成的).我现在应该如何编写findBy()方法?
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?
谢谢.
推荐答案
我找到了另一个解决方案,假设我们有一个User实体和Message实体.
I've found another solution, let's assume we have a User entities and Message entities.
@Entity
public class Message {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotNull
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
private DateTime date;
@ManyToOne
@JoinColumn(name = "id_user_sender")
private User userSender;
@NotNull
private Long idUserReceiver;
@NotNull
@Length(min = 10)
private String message;
@NotNull
private String idConversation;
@NotNull
Boolean userHasRead;
//Getters and Setters
}
@Entity
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
//Some other properies like email,name etc
//Getters and setters
}
现在我需要按id_user_sender查找所有消息.
Now i need to find all messages by id_user_sender.
public interface MessageRepository extends JpaRepository<Message,Long>,TransactionRepositoryCustom {
List<Message> findByUserSenderId(Long idUser);
}
外植体:在消息实体中,我有一个名为"userSender"的用户对象. userSender有一个名为"id"的字段.
Explantion:In the Message entity i have a User Object named 'userSender'. The userSender has a field named 'id'.
因此'findByUserSenderId'在userSender对象中搜索ID.
So 'findByUserSenderId' search for the id in the userSender Object.
另一种可能性是将完整的User对象传递到消息存储库,如以下示例所示:(User对象必须仅包含id)
Another possibilty is to pass a full User object to the message repository as in the example below: (The User object must contain only the id)
public interface MessageRepository extends JpaRepository<Message,Long>,TransactionRepositoryCustom {
List<Message> findByUserSender(User user);
}
这篇关于在由JoinColumn注释的字段上编写CRUDRepository的findBy()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!