我正在使用 Spring Data JPA + Hibernate。在将实体添加到其他实体列表并保存后,我需要获取实体 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?