javahibernatejavafxmappingexception

JavaFX Hibernate MappingException


Starting to work with JavaFX and hibernate, but can't resolve this exception:

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

This is my model code:

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;
}

}

Suggestions found here on stackoverflow: tried to use AccessType.PROPERTY and tried to annotate fields/getters, but it doesn't make any difference. I'm using SQLite database.


Solution

  • You need to implement the JavaFX Property pattern correctly. Your get methods should return the underlying type, not the property type.

    For example:

    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);
        }
    }
    

    If you really have a need to expose the property itself via a get method (and this is completely non-standard), you must force it not to be persisted to the database by annotating it as @Transient.