所以我有兩個實體,Medewerker和Taak之間的多對多關係,這裏我有兩個密碼:Jointable多對多休眠
@Entity
@Table(name = "T_MEDEWERKER")
public class Medewerker {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String naam;
private String functie;
@ManyToOne
@Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
@JoinColumn(name = "festivalId", nullable = false)
private Festival festival;
@ManyToMany
@Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
@JoinTable(name="T_MEDEWERKER_TAAK",
joinColumns = {@JoinColumn(name="medewerkerId")},
inverseJoinColumns = {@JoinColumn(name="taakId")})
private List<Taak> taken = new ArrayList<Taak>();
public Medewerker(){
}
public Medewerker(String naam, String functie) {
this.naam = naam;
this.functie = functie;
}
public void addTaak(Taak t) {
taken.add(t);
}
public void setFestival(Festival festival) {
this.festival = festival;
}
}
@Entity
@Table(name = "T_TAAK")
public class Taak {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String omschrijving;
@ManyToMany
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
@JoinTable(name="T_MEDEWERKER_TAAK",
joinColumns = {@JoinColumn(name="taakId")},
inverseJoinColumns = {@JoinColumn(name="medewerkerId")})
private List<Medewerker> medewerkers = new ArrayList<Medewerker>();
public Taak(){
}
public Taak(String omschrijving) {
this.omschrijving = omschrijving;
}
private void addMedewerker(Medewerker m){
medewerkers.add(m);
}
}
現在,我想有中間表(T_MEDEWERKER_TAAK)通過jointable自動生成休眠。但是,我不斷收到以下錯誤: 21:49:08,157 WARN SqlExceptionHelper:145 - SQL錯誤:1364,SQLState:HY000 21:49:08,158錯誤SqlExceptionHelper:147 - 字段'taakId'沒有默認值
但是,taakid字段確實有一個自動生成的值,所以我真的不明白我在這裏做錯了什麼?另外,映射雙方的多方關係還不好嗎?因爲老實說,我不知道把它放在一邊或把它放在兩邊之間有什麼區別.. Mysql確實創建了複合表,但並沒有放入任何數據。
如果需要的話,這是我testcode:
public class TestMedewerker {
public static void main(String[] args) {
Medewerker m = new Medewerker("Jos", "Cameraman");
m.setFestival(new Festival("Rock Wercher", 3, "Werchter", 75000));
m.addTaak(new Taak("Filmen"));
m.addTaak(new Taak("Geluidsman Spelen"));
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx = session.beginTransaction();
session.saveOrUpdate(m);
tx.commit();
}
}
我想我和你一樣,兩個@JoinColumn名稱屬性就是你給自己的名字嗎? (他們不是指任何外鍵或什麼?) –
我修好了!我愚蠢地在兩個引用彼此的表中都有外鍵 –