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

发布于 2024-12-12 06:07:14 字数 5133 浏览 0 评论 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 并得到错误,它找不到列 speler.. I'我对此感到非常困惑,因为我只想将 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

I'm having trouble getting my code to work, I've tried about everything and nothing seems to work;

My Code :
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;
}

}

Error I get when I run my main class, I make a new Speler, then I try to insert a SpelerOpstelling and get the error that it can't find the column speler.. I'm really confused about this since I just want the ID of the Speler into my database.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你另情深 2024-12-19 06:07:14

您不能随机将注释应用于方法或字段。通常,您应该以与 @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;
}

You can't apply annotations to methods or fields randomly. Normally, you should apply your annotations the same way as @Id.

In your case in class SpelerOpstelling you applied @Id to getter method but

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

is applied to field. So Hibernate just ignores this annotation.

Just move these annotation one line lower to apply to getter as well:

private Speler sp;

@ManyToOne(cascade = CascadeType.ALL )
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
@JoinColumn( name = "Speler_ID" )
public Speler getSpeler() {
    return speler;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文