2016-11-21 46 views
0

我有User類和Recipe類。 Recipe類有一個顯示哪個用戶擁有哪個配方的字段。用戶有一個ID。我認爲這應該是一個ManyToOne連接,因爲一個用戶可以有很多食譜。我的問題是他,我怎麼能在Java春天編寫這個代碼,這些註釋我覺得自己有點失落。這是我想出來的,但它不起作用。試圖創建冬眠和java彈簧表

@Entity 
public class Recipe { 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private int recipe_id; 
    private String name; 
    @JoinColumn(name="user_id" , table="User") 
    private int user_id; 
    private LocalDate dateUploaded; 
    private String description; 

    public int getId() { 
     return recipe_id; 
    } 

    public void setId(int recipe_id) { 
     this.recipe_id = recipe_id; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public int getUser() { 
     return user_id; 
    } 

    public void setUser(int user_id) { 
     this.user_id = user_id; 
    } 

    public LocalDate getDateUploaded() { 
     return dateUploaded; 
    } 

    public void setDateUploaded(LocalDate dateUploaded) { 
     this.dateUploaded = dateUploaded; 
    } 

    public String getDescription() { 
     return description; 
    } 

    public void setDescription(String description) { 
     this.description = description; 
    } 
} 

和用戶類

@Entity 
public class User { 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private int user_id; 
    private String firstName; 
    private String lastName; 
    private String userName; 
    private String password; 

    public User(int user_id, String firstName, String lastName, String userName, String password, int[] favoriteRecipes, int[] myRecipes) { 
     this.user_id = user_id; 
     this.firstName = firstName; 
     this.lastName = lastName; 
     this.userName = userName; 
     this.password = password; 
    } 

    public int getId() { 
     return user_id; 
    } 

    public void setId(int user_id) { 
     this.user_id = user_id; 
    } 

    public String getFirstName() { 
     return firstName; 
    } 

    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 

    public String getLastName() { 
     return lastName; 
    } 

    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 

    public String getUserName() { 
     return userName; 
    } 

    public void setUserName(String userName) { 
     this.userName = userName; 
    } 

    public String getPassword() { 
     return password; 
    } 

    public void setPassword(String password) { 
     this.password = password; 
    } 
} 
+0

是否確定問題中的關聯,或者您想在用戶類中添加一個列表屬性,其中包含用戶上傳的所有配方?使關聯成雙向。 – Ubercool

+0

不,我們不想有一個列表,因爲那時我們制動了第一範式。 – Ermehtar

回答

0
You should add one more field in user table that is getRecieps and add this annotation. 

@OneToMany(cascade = CascadeType.ALL) 
@JoinTable(name = "USER_RECEIPE", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = { @JoinColumn(name = "receipe_id") }) 

    public Set<receipe> getReceipes() { 
     return this.receipes; 
    } 

    public void setreceipes(Set<receipe> receipes) { 
     this.receipes= receipes; 
    } 
0

據我所知,你正在努力實現RecipeUser之間多對一的關係。

所有你需要做的就是下面的註釋添加到user_id領域

食譜類

@ManyToOne(optional = false) 
@JoinColumn(name="user_id") 
private int user_id; 

試試這個,讓我知道,如果你仍然面臨的問題。

+0

你在問題中得到更新有什麼錯誤。 – Ubercool

+0

當我在該區域執行ManyToOne註釋或其他註釋,並在JoinColumn中包含(name =「user_id」,table =「User」)時,出現錯誤:無法找到期望的輔助表:no用戶可用於com .einar.inthefridge.model.Recipe – Ermehtar

+0

交叉檢查表名稱,因爲您沒有在實體類文件中明確提到表名,所以應該有名稱爲「user」和「recipe」的表 – Ubercool