开始使用JavaFX和hibernate,但无法解决此异常:
Caused by: org.hibernate.MappingException: Could not determine type for: javafx.beans.property.StringProperty, at table: User, for columns: [org.hibernate.mapping.Column(answerProperty)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:336)
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:310)
at org.hibernate.mapping.Property.isValid(Property.java:241)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:496)
at org.hibernate.mapping.RootClass.validate(RootClass.java:270)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1360)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1851)
at t093760.diploma.MainApp.start(MainApp.java:48)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863)
at com.sun.javafx.application.LauncherImpl$$Lambda$53/86243779.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/186276003.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/73799535.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/237061348.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/2117255219.run(Unknown Source)
... 1 more
这是我的型号代码:
import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
@Entity
@Table(name="User")
@Access(value = AccessType.PROPERTY)
public class User {
private long id;
private StringProperty login;
private StringProperty email;
private StringProperty password;
private StringProperty picture;
private StringProperty answer;
public User(){
//this(null,null);
}
public User(String login,String email,String password,String picture,String answer){
this.login=new SimpleStringProperty(login);
this.email=new SimpleStringProperty(email);
this.password=new SimpleStringProperty(password);
this.picture=new SimpleStringProperty(picture);
this.answer=new SimpleStringProperty(answer);
}
public StringProperty getLoginProperty() {
return login;
}
public void setLogin(String login) {
this.login = new SimpleStringProperty(login);
}
@Column(name="login")
public String getLogin(){
return this.login.get();
}
public StringProperty getEmailProperty() {
return email;
}
public void setEmail(String email) {
this.email = new SimpleStringProperty(email);
}
@Column(name="email")
public String getEmail() {
return this.email.get();
}
public StringProperty getPasswordProperty() {
return password;
}
public void setPassword(String password) {
this.password = new SimpleStringProperty(password);
}
@Column(name="password")
public String getPassword() {
return this.password.get();
}
public StringProperty getPictureProperty() {
return picture;
}
public void setPicture(String picture) {
this.picture = new SimpleStringProperty(picture);
}
@Column(name="picture")
public String getPicture() {
return this.picture.get();
}
public StringProperty getAnswerProperty() {
return answer;
}
public void setAnswer(String answer) {
this.answer = new SimpleStringProperty(answer);
}
@Column(name="answer")
public String getAnswer() {
return this.answer.get();
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="user_id")
public long getId(){
return id;
}
}
在stackoverflow上找到的建议:尝试使用AccessType.PROPERTY
并尝试注释字段/ getter,但它没有任何区别。我正在使用SQLite数据库。
答案 0 :(得分:2)
您需要正确实施JavaFX Property pattern。您的get
方法应返回基础类型,而不是属性类型。
例如:
public class User {
private final StringProperty login ;
public User(String login) {
this.login = new SimpleStringProperty(login);
}
public StringProperty loginProperty() {
return login ;
}
public final String getLogin() {
return loginProperty().get();
}
public final void setLogin(String login) {
loginProperty().set(login);
}
}
如果您确实需要通过get方法公开属性本身(这完全是非标准的),则必须强制它不要通过将其注释为@Transient
来持久化到数据库。 / p>