Java 找到具有给定外键的多行Spring hibernate
我在使用JPA存储库的Java 找到具有给定外键的多行Spring hibernate,java,spring,hibernate,spring-boot,spring-data-jpa,Java,Spring,Hibernate,Spring Boot,Spring Data Jpa,我在使用JPA存储库的findByProfileId()方法时遇到问题 这是我数据库中的示例条目(行) 当我尝试通过REST()请求时,21234是上表中的配置文件id 这是我的服务 @RequestMapping(path="/{id}", method = RequestMethod.GET) public ResponseEntity<?> get(@PathVariable Long id){ try { logger.info("get({})",
findByProfileId()
方法时遇到问题
这是我数据库中的示例条目(行)
当我尝试通过REST()请求时,21234是上表中的配置文件id
这是我的服务
@RequestMapping(path="/{id}", method = RequestMethod.GET)
public ResponseEntity<?> get(@PathVariable Long id){
try {
logger.info("get({})",id);
logger.info("get({})",Lists.transform(wishlistService.get(id), toRestWishlist));
return new ResponseEntity<>(Lists.transform(wishlistService.get(id), toRestWishlist), HttpStatus.OK);
} catch (Exception e) {
// TODO: handle exception
logger.error("{}",e.getMessage());
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
}
}
每次发送请求时,我都会在日志中看到此错误
2017-12-08 14:15:54.854 INFO 2620 --- [nio-8080-exec-2] c.s.s.web.controller.WishlistController : get(21234)
2017-12-08 14:15:54.858 INFO 2620 --- [nio-8080-exec-2] o.h.e.internal.DefaultLoadEventListener : HHH000327: Error performing load command : org.hibernate.HibernateException: More than one row with the given identifier was found: 21234, for class: com.secret.santa.core.models.Wishlist
2017-12-08 14:15:54.858 ERROR 2620 --- [nio-8080-exec-2] c.s.s.web.controller.WishlistController : More than one row with the given identifier was found: 21234, for class: com.secret.santa.core.models.Wishlist; nested exception is org.hibernate.HibernateException: More than one row with the given identifier was found: 21234, for class: com.secret.santa.core.models.Wishlist
将
存储库中的方法签名声明为:
List<Wishlist> findAllByProfile(Profile profile);
列出findAllByProfile(配置文件);
您必须传递类似于新概要文件(profileId)
的内容
此外,您可能必须将字段概要文件中的@OneToOne
注释更改为@ManyToOne
在存储库中声明方法签名,如下所示:
List<Wishlist> findAllByProfile(Profile profile);
列出findAllByProfile(配置文件);
您必须传递类似于新概要文件(profileId)
的内容
此外,您可能必须将字段Profile
中的@OneToOne
注释更改为@ManyToOne
@Query('select w from Wishlist w where w.Profile\u id=?1')
您可以尝试查询方法@Query('select w from Wishlist w where w.Profile\u id=?1'))
您可以尝试使用查询方法将参数从id
更改为profileId
。错误仍然是使用findAllByProfileId
测量的,仍然出现多行错误更新答案。尝试应用答案。将我的存储库更改为接受Profile
类,在我的服务中,我将使用它的方式更改为返回repo.findAllByProfile(新配置文件(id))代码>。错误仍然是一样的,我相信错误来自@OneToOne
注释。可能您必须将其更改为@ManyToOne
。将参数从id
更改为profileId
。错误仍然是使用findAllByProfileId
测量的,仍然出现多行错误更新答案。尝试应用答案。将我的存储库更改为接受Profile
类,在我的服务中,我将使用它的方式更改为返回repo.findAllByProfile(新配置文件(id))代码>。错误仍然是一样的,我相信错误来自@OneToOne
注释。也许你必须把它改成@ManyToOne
。
2017-12-08 14:15:54.854 INFO 2620 --- [nio-8080-exec-2] c.s.s.web.controller.WishlistController : get(21234)
2017-12-08 14:15:54.858 INFO 2620 --- [nio-8080-exec-2] o.h.e.internal.DefaultLoadEventListener : HHH000327: Error performing load command : org.hibernate.HibernateException: More than one row with the given identifier was found: 21234, for class: com.secret.santa.core.models.Wishlist
2017-12-08 14:15:54.858 ERROR 2620 --- [nio-8080-exec-2] c.s.s.web.controller.WishlistController : More than one row with the given identifier was found: 21234, for class: com.secret.santa.core.models.Wishlist; nested exception is org.hibernate.HibernateException: More than one row with the given identifier was found: 21234, for class: com.secret.santa.core.models.Wishlist
List<Wishlist> findAllByProfile(Profile profile);