Reputation: 2421
i have a problem using findByProfileId()
method of JPA repository
this is my sample entries (rows) on my database.
When i try to request via REST (http://localhost:8080/rest/test/21234) where 21234 is the profile_id in the above table.
Here is my RESTservice.
@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);
}
}
Here is the WishlistServiceImpl
@Override
public List<Wishlist> get(Long id) {
// TODO Auto-generated method stub
return repo.findByProfileId(id);
}
Here is the Repository
@Repository
public interface WishlistRepository extends JpaRepository<Wishlist, Long> {
List<Wishlist> findByProfileId(Long id);
}
Here is my Wishlist
entity
@Entity
@Table(name = "wishlist")
public class Wishlist {
@Id
@GeneratedValue
@Column(name="wish_Id")
private Long wishId;
@OneToOne(cascade= CascadeType.DETACH)
@JoinColumn(name = "profile_id")
private Profile profile;
@OneToMany(
mappedBy = "wishlist",
cascade = CascadeType.DETACH,
orphanRemoval = true
)
private List<Comment> comments;
@NotNull
@Column(name="description")
private String description;
@NotNull
@Column(name="images")
private String images;
@Column(name="created_at")
private Long createAt;
@Column(name="updated_at")
private Long updatedAt;
/* Getters and Setters and toString() */
}
Everytime i send the request i get this error on the logs
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
Upvotes: 0
Views: 1017
Reputation: 2910
Declare method signature in Repository
as:
List<Wishlist> findAllByProfile(Profile profile);
And you have to pass something like new Profile(profileId)
.
Also, you might have to change @OneToOne
annotation in field Profile
to @ManyToOne
.
Upvotes: 2