JavaFX Hibernate 映射异常

JavaFX Hibernate MappingException

开始使用 JavaFX 和休眠,但无法解决此异常:

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$launchApplication19(LauncherImpl.java:863)
at com.sun.javafx.application.LauncherImpl$$Lambda/86243779.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait2(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl$$Lambda/186276003.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null0(PlatformImpl.java:295)
at com.sun.javafx.application.PlatformImpl$$Lambda/73799535.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater1(PlatformImpl.java:294)
at com.sun.javafx.application.PlatformImpl$$Lambda/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$null5(WinApplication.java:101)
at com.sun.glass.ui.win.WinApplication$$Lambda/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;
}

}

在 Whosebug 上找到的建议:尝试使用 AccessType.PROPERTY 并尝试注释 fields/getters,但没有任何区别。我正在使用 SQLite 数据库。

您需要正确实施 JavaFX Property pattern。您的 get 方法应该 return 基础类型,而不是 属性 类型。

例如:

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 方法公开 属性 本身(这完全是非标准的),您必须通过将其注释为 [ 来强制不将其持久保存到数据库中=12=].