无法确定类型错误,Hibernate 使用注释
我无法让我的代码正常工作,我已经尝试了一切,但似乎没有任何效果;
我的代码: 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能随机将注释应用于方法或字段。通常,您应该以与
@Id
相同的方式应用注释。在您的情况下,在类
SpelerOpstelling
中,您将@Id
应用于 getter 方法,但应用于字段。所以 Hibernate 就忽略这个注解。
只需将这些注释移低一行即可应用到 getter:
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 butis applied to field. So Hibernate just ignores this annotation.
Just move these annotation one line lower to apply to getter as well: