2011-10-26 45 views
0

我無法讓我的代碼工作,我嘗試了一切,似乎沒有任何工作;無法確定類型錯誤,使用註釋休眠

我的代碼: 1. SpelerOpstelling.java

package model; 

import org.hibernate.annotations.Cascade; 

import javax.persistence.*; 

@Entity 
@Table(name = "SpelerOpstelling") 
public class SpelerOpstelling { 

private int SpelerOpstellingID; 
private char positie; 
private String functie; 
private Speler speler; 


public SpelerOpstelling() { 
} 

public SpelerOpstelling(char positie, String functie, Speler speler) { 
    this.positie = positie; 
    this.functie = functie; 
    this.speler = speler; 
} 


@Id 
@GeneratedValue (strategy=GenerationType.IDENTITY) 
@Column(name = "SpelerOpstelling_ID") 
    public int getSpelerOpstellingID() { 
    return SpelerOpstellingID; 
} 

public void setSpelerOpstellingID(int spelerOpstellingID) { 
    SpelerOpstellingID = spelerOpstellingID; 
} 



@Column(name = "functie", nullable = false, length = 100) 
    public String getFunctie() { 
    return functie; 
} 

public void setFunctie(String functie) { 
    this.functie = functie; 
} 



@Column(name= "positie") 
     public char getPositie() { 
    return positie; 
} 

public void setPositie(char positie) { 
    this.positie = positie; 
} 

@ManyToOne(cascade = CascadeType.ALL) 
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE}) 
@JoinColumn(name = "Speler_ID") 
private Speler sp; 
public Speler getSpeler() { 
    return speler; 
} 

public void setSpeler(Speler speler) { 
    this.speler = speler; 
} 

}

Speler.java

package model; 

import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 
import javax.persistence.Table; 

@Entity 
@Table(name = "Speler") 
public class Speler { 
    private int speler_ID; 
private String naam; 
private String adres; 
private String gemeente; 
private String telefoonnummer; 
private String fax; 
private String email; 

public Speler() { 
} 

public Speler(String naam, String adres, String gemeente, String fax, String telefoonnummer, String email) { 
    this.naam = naam; 
    this.adres = adres; 
    this.gemeente = gemeente; 
    this.fax = fax; 
    this.telefoonnummer = telefoonnummer; 
    this.email = email; 
} 



@Id 
@GeneratedValue 
@Column(name = "Speler_ID") 
    public int getSpeler_ID() { 
    return speler_ID; 
} 

public void setSpeler_ID(int speler_ID) { 
    this.speler_ID = speler_ID; 
} 


@Column(name = "naam", nullable = false, length=250) 
public String getNaam() { 
    return naam; 
} 

public void setNaam(String naam) { 
    this.naam = naam; 
} 



@Column(name = "adres", nullable = false, length=50) 
    public String getAdres() { 
    return adres; 
} 

public void setAdres(String adres) { 
    this.adres = adres; 
} 



@Column(name = "gemeente", nullable = false, length=50) 
    public String getGemeente() { 
    return gemeente; 
} 

public void setGemeente(String gemeente) { 
    this.gemeente = gemeente; 
} 

@Column(name = "fax", nullable = false, length=10) 


public String getFax() { 
    return fax; 
} 

public void setFax(String fax) { 
    this.fax = fax; 
} 

@Column(name = "telefoonnummer", nullable = false, length=10) 

public String getTelefoonnummer() { 
    return telefoonnummer; 
} 

public void setTelefoonnummer(String telefoonnummer) { 
    this.telefoonnummer = telefoonnummer; 
} 

@Column(name = "email", nullable = false, length=10) 

public String getEmail() { 
    return email; 
} 

public void setEmail(String email) { 
    this.email = email; 
} 

}

錯誤,當我跑我的主類,我得到,我做了一個新的Speler,然後嘗試插入一個SpelerOpstelling並獲取錯誤th在它無法找到列speler ..我真的很困惑,因爲我只是想要我的數據庫中的Speler的ID。

Initial SessionFactory creation failed.org.hibernate.MappingException: Could not determine type for: model.Speler, at table: SpelerOpstelling, for columns: [org.hibernate.mapping.Column(speler)] 
Exception in thread "main" java.lang.ExceptionInInitializerError 
    at persistence.HibernateUtil.<clinit>(HibernateUtil.java:17) 
    at model.Start.main(Start.java:38) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120) 
Caused by: org.hibernate.MappingException: Could not determine type for: model.Speler, at table: SpelerOpstelling, for columns: [org.hibernate.mapping.Column(speler)] 
    at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:306) 
    at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:290) 
    at org.hibernate.mapping.Property.isValid(Property.java:217) 
    at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:464) 
    at org.hibernate.mapping.RootClass.validate(RootClass.java:235) 
    at org.hibernate.cfg.Configuration.validate(Configuration.java:1362) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1865) 
    at persistence.HibernateUtil.<clinit>(HibernateUtil.java:12) 
    ... 6 more 

回答

1

您不能隨意將註釋應用於方法或字段。通常,您應該使用與@Id相同的方式應用註釋。

在類你的情況SpelerOpstelling你申請@Id吸氣劑的方法,但

@ManyToOne(cascade = CascadeType.ALL) 
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE}) 
@JoinColumn(name = "Speler_ID") 
private Speler sp; 

應用於現場。所以Hibernate只是忽略了這個註釋。

只是將這些註釋一行下適用於吸除,以及:

private Speler sp; 

@ManyToOne(cascade = CascadeType.ALL) 
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE}) 
@JoinColumn(name = "Speler_ID") 
public Speler getSpeler() { 
    return speler; 
} 
+0

如果我這樣做,我得到這個錯誤: 產生的原因:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:未知「字段列表」中的列'Speler_ID' – Yunieke

+0

至少現在您可以看到該映射起作用了。你的兩個表都有'Speler_ID'列嗎?一個是主鍵,另一個是外鍵。 –