无法确定类型错误,Hibernate使用注释

时间:2011-10-26 22:24:34

标签: hibernate annotations

我无法让我的代码工作,我已经尝试过一切,似乎没有任何工作;

我的代码: 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并得到它无法找到列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 个答案:

答案 0 :(得分:1)

您无法随机将注释应用于方法或字段。通常,您应该使用与@Id相同的方式应用注释。

在课程SpelerOpstelling的情况下,您将@Id应用于getter方法,但

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

适用于字段。所以Hibernate只是忽略了这个注释。

只需将这些注释移动到较低的一行即可应用于getter:

private Speler sp;

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