2013-10-23 188 views
0

所以我有兩個實體,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(); 
} 
} 

回答

0

我有一個例子多對多關係,看看和你的代碼適應這一點,我會覺得會工作。

我有一個表ST_Product和St_partner_subsidiary和我有一個表ST_Product_subsidiary(持有一個外鍵st_product等來st_partner_subsidiary),所以我必須只映射表st_product和st_partner_subsidiary,然後我有:

@Entity 
@Table(name = "ST_PRODUCT") 
public class Product { 
// OTHER columns 
    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL) 
    @JoinTable(name = "ST_PRODUCT_SUBSIDIARY", joinColumns = { 
     @JoinColumn(
     name = "PRODUCT_ID", 
     nullable = false, 
     updatable =   false) }, 
    inverseJoinColumns = { @JoinColumn(name = "PARTNER_SUBSIDIARY_ID", 
       nullable = false, updatable = false) }) 

public List<ProductSubsidiary> getProductSubsidiaries() { 
    return productSubsidiaries; 
} 
//Other columns 
} 

您不映射中間表,您只能在兩個實體中引用它,在這種情況下您可以看到ST_PRODUCT_SUBSIDIARY僅被引用。

希望它有幫助。

+0

我想我和你一樣,兩個@JoinColumn名稱屬性就是你給自己的名字嗎? (他們不是指任何外鍵或什麼?) –

+0

我修好了!我愚蠢地在兩個引用彼此的表中都有外鍵 –