1
我正在使用Spring Data JPA + Hibernate。在添加到其他實體列表並保存後,我需要獲取實體的ID。代碼如下:休眠:添加到列表中的實體的ID
@Entity
public class Product extends AbstractAuditable<Long> {
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "product_id", nullable = false)
private List<Feedback> feedbacks = new ArrayList<Feedback>();
...
}
@Entity
public class Feedback extends AbstractPersistable<Long> {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "product_id", insertable = false, updatable = false, nullable = false)
private Product product;
...
}
public interface ProductRepository extends JpaRepository<Product, Long> {
}
Feedback feedback = new Feedback();
product.getFeedbacks().add(feedback);
productRepository.saveProduct(product);
feedback.getId(); // returns null
如何在保存後正確得到反饋ID?