2017-06-16 200 views
-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); 
} 

}

+0

是的,它應該返回完整的set.Pls顯示你如何得到位在道平? – xyz

+0

mmm,在道,我做只創建,但getter調用控制器設置 orderSet = bid.getOrders(); –

+0

對'Entity'的Create/Read/Update/Delete操作應該在'Repository'中,而不是'Controller'中。 –

回答

0

試試這個

  • 從一個

    到許多方面

    @OneToMany (targetEntity = Order.class, cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER) 
    
    @JoinColumn (name = "order_id", referencedColumnName = "id") 
    
  • 由多到一邊

    @ManyToOne(targetEntity = Bid.class, optional = true,fetch = FetchType.EAGER,cascade = 
         CascadeType.ALL) 
    
    @JoinColumn(name = "order_id", referencedColumnName = "id")