2017-04-26 94 views
1

我有Order's其中包含一堆Product's和數量。多列多列僅返回一行

Order實體:

@Entity(name = "orders") // Select * from order order by... NO! 
public class Order { 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private long id; 

    @OneToMany(mappedBy = "product", cascade = ALL, orphanRemoval = true, fetch = EAGER) 
    private List<OrderEntity> orderEntities = new ArrayList<>(); 
... 

ordersproducts之間的關係如下:

@Entity 
public class OrderEntity implements Serializable { 
    @Id 
    @ManyToOne 
    @JsonIgnore 
    private Order order; 

    @Id 
    @ManyToOne(fetch = EAGER) 
    private Product product; 

    private int quantity; 
... 

product如下:

@Entity 
public class Product { 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private long id; 

    private String name; 
    @OneToMany(mappedBy = "order", cascade = CascadeType.ALL, orphanRemoval = true) 
    private List<OrderEntity> orderEntity; 
... 

的問題是,當我選擇了order由它的id我只收到一個OrderEntity回來,即使我可以通過查看數據庫表來看到有幾個與order關聯。任何人都知道我做錯了什麼?

回答

1

我認爲你配置mappedBy incroattly ..實際上是另一回合。

所以,你應該有這樣的:

訂單:

@Entity(name = "orders") // Select * from order order by... NO! 
public class Order { 

    @OneToMany(mappedBy = "order", cascade = ALL, orphanRemoval = true, fetch = EAGER) 
    private List<OrderEntity> orderEntities = new ArrayList<>(); 

產品:

@Entity 
public class Product { 

    @OneToMany(mappedBy = "product", cascade = CascadeType.ALL, orphanRemoval = true) 
    private List<OrderEntity> orderEntity;