2016-02-29 89 views
-1

在我的代碼中,我有customer類和item類之間的oneToMany關係。這意味着,客戶可以有一個或多個項目。 下面是客戶代碼:Java休眠:@OneToMany協會不起作用

@Entity 
@Data 

public class customer { 

    @Id 
    @GeneratedValue 
    int id; 

    String name; 
    String lastname; 

    @Embedded 
    Address address; 

    @OneToMany 
    @Column(name="ITEM_ID") 
    List<item> item;  
} 

,它是項目類:

@Entity 
@Data 

public class item { 

    @Id 
    @GeneratedValue 
    int id; 

    String name; 
    String Serialnumber; 
    int price; 

    @ManyToOne 
    customer customer; 

} 

然後我做了一些測試,嘗試在模型中我的查詢。

insert into item(id,name,Serialnumber,price) values(1,'bike','123',200); 
insert into item(id,name,Serialnumber,price) values(2,'car','123',200); 


insert into customer(id,name,lastname,Country,City,Street,No,item_id) 
values(1,'Salman','Lashkarara','Iran','Tehran','Shariati','12',1); 

insert into customer(id,name,lastname,Country,City,Street,No,item_id) 
values(2,'Saba','Lashkarara','Iran','Tehran','Shariati','12',2); 


insert into customer(id,name,lastname,Country,City,Street,No,item_id) 
values(3,'Saba','Lashkarara','Iran','Tehran','Shariati','12',1); 

但是當我運行我的代碼,我面臨着以下錯誤:

Column "ITEM_ID" not found; SQL statement: 
insert into customer(id,name,lastname,Country,City,Street,No,item_id) values(1,'Salman','Lashkarara','Iran','Tehran','Shariati','12',1) 

請支付特殊的關注,這是一個java mvc-spring應用程序,我創建一個使用代碼我的車型,所以沒有數據庫檢查字段item_id

正如你可以看到我已經添加了@Column(name="ITEM_ID")來定義列。

+0

這對我沒有意義。一個客戶可以有多個項目。所以這些項目的引用不可能在客戶表中。您在此處發佈的映射最有可能在客戶和項目之間創建n:m映射表。如果您使用mappedBy屬性而不是引用可能在項目表中(作爲客戶的參考) –

回答

1

你必須使用@JoinColumn進行關聯列:

@OneToMany 
@JoinColumn(name="ITEM_ID") 
List<item> item; 
+0

新的錯誤是什麼? –

+0

它仍然是一樣的錯誤。 – Salman

+0

如果我是正確的Item_id列表比在項目表中,你querry試圖將其插入到客戶表。 –

0

一些其他的選擇

@OneToMany(cascade=CascadeType.All, fetch=FetchType.EAGER) 
@JoinColumn(name="ITEM_ID") 
List<item> item; 

項類

@ManyToOne(mappedBy="item") 
customer customer; 
0

你能做到這一點我也有userbcr類,一個用戶有很多bcr等下面的代碼將幫助您

bcr.java

@ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "user_who_enter_demand", nullable = false) 
    public User getUserByUserWhoEnterDemand() { 
    return this.userByUserWhoEnterDemand; 
    } 

    public void setUserByUserWhoEnterDemand(User userByUserWhoEnterDemand) { 
    this.userByUserWhoEnterDemand = userByUserWhoEnterDemand; 
    } 

user.java

@OneToMany(fetch = FetchType.LAZY, mappedBy = "userByUserWhoEnterDemand") 
    public Set<BudgetControlRegister> getBudgetControlRegistersForUserWhoEnterDemand() { 
    return this.budgetControlRegistersForUserWhoEnterDemand; 
    } 

    public void setBudgetControlRegistersForUserWhoEnterDemand(Set<BudgetControlRegister> budgetControlRegistersForUserWhoEnterDemand) { 
    this.budgetControlRegistersForUserWhoEnterDemand = budgetControlRegistersForUserWhoEnterDemand; 
    } 
0

你不能沒有表映射表列:

@Entity 
@Table(name = "ITEM_TABLE") 
public class item { 
... 
@ManyToOne(fetch=FetchType.LAZY) 
@JoinColumn(name = "CUSTOMER_ID_ITEM_TABLE") 
private customer customer; 
... 
} 

@Entity 
@Table(name = "CUSTOMER_TABLE") 
public class customer { 
... 
    @OneToMany 
    @Column(name="ITEM_ID") 
    List<item> item;  
}