-1
告訴我正確我明白,當你調用getOrders()時,必須返回完整集?休眠@OneToMany空集
訂單表中的FK。
或者你只需要使用HQL(JPQL)來獲得訂單對象?
我得到空集。 當我打電話給getter Set orderSet = bid.getOrders();在調試中我不顯示任何操作。
@Entity
@Table(name = "bid")
public class Bid {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany(mappedBy = "bid_id",cascade = CascadeType.ALL,fetch = FetchType.EAGER)
private Set<Order> orders = new HashSet<>();
@Column(name = "title")
private String title;
@Column(name = "description")
private String description;
public Set<Order> getOrders() {
return orders;
}
public void setOrders(Set<Order> orders) {
this.orders = orders;
}
@Entity
@Table(name = "orders")
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "title")
private String title;
@ManyToOne(optional = true,fetch = FetchType.EAGER,cascade =
CascadeType.ALL)
@JoinColumn(name = "bid")
private Bid bid_id;
@Repository
@Transactional
public class BidDao {
@PersistenceContext
private EntityManager entityManager;
public void create(Bid bid){
entityManager.persist(bid);
}
}
是的,它應該返回完整的set.Pls顯示你如何得到位在道平? – xyz
mmm,在道,我做只創建,但getter調用控制器設置 orderSet = bid.getOrders(); –
對'Entity'的Create/Read/Update/Delete操作應該在'Repository'中,而不是'Controller'中。 –