我附上了我的表数据的快照。表名是orderdetail
。
我希望获得基于客户的数据。就像我的情况一样,数据应该在PurchasedBy
的基础上找到。
@Entity
public class OrderDetail {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@ManyToOne
@JoinColumn(name="purchased_By")
private User purchasedBy;
我正在尝试在OrderDetailDao
存储库
List<OrderDetail> findByPurchasedBy(User user);
但是我收到了一个错误:
Caused by: java.lang.IllegalArgumentException: Unable to locate Attribute with the the given name [purchasedBy] on this ManagedType [com.example.Domain.OrderDetail]
编辑摘要&gt;
当我使用findAll()
并返回json时,purchasedBy
部分看起来像这样:
"purchasedBy":{
"id":15,
"email":"admin@gmail.com",
"password":"$2a$10$jj41EbJZTOBfbKdJ6wAdx.rdQq8qU3OqoSzS5mvDVaiL33G1U4pPC",
"name":"admin",
"lastName":"admin",
"active":1,
"roleselected":null,
"roles":[
{
"id":1,
"role":"admin",
"users":[
],
"new":false
}
],
"new":false
}
}
答案 0 :(得分:3)
我认为使用自定义查询可能会解决问题:
@Query("select od from OrderDetail od where od.PurchasedBy.id = ?1")
List<OrderDetail> findByPurchasedBy(Integer userId);
答案 1 :(得分:1)
不使用自定义查询jpa存储库支持此功能。检查以下代码是否正常工作并进行了测试。
@Entity
@Table(name = "order_details")
public class OrderDetails {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@ManyToOne
@JoinColumn(name = "purchased_By")
private User purchasedBy;
}
以下是存储库类:
public interface OrderDetailsRepository extends JpaRepository<OrderDetails, Integer> {
public List<OrderDetails> findByPurchasedBy(User user);
}
服务类方法如下:
@Autowired
public OrderDetailsRepository orderDetailsRepository;
public List<OrderDetails> getUserOrders(Integer userId) {
User user = userRepository.findOne(userId);
return orderDetailsRepository.findByPurchasedBy(user);
}
使用以下依赖版本:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.10.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
在jpa 1.4检查后,支持许多新功能。