Using StringProperty or DoubleProperty with Hibernate

3

Is it possible to create a class in Java to access with hibernate a database table with primary key compound (two or more fields), whose attributes with the @Column are of type StringProperty or DoubleProperty of JavaFX?

A simple example of what I'm asking:

PersonPK Class


import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

@Embeddable
public class PersonPK implements Serializable{

    private static final long serialVersionUID = 1L;

    private StringProperty firstname; 
    private StringProperty lastname;

    public PersonPK(){
        this.firstname= new SimpleStringProperty();
        this.lastname = new SimpleStringProperty();
    }

    public StringProperty firstnameProperty() {
        return firstname;
    }

    @Column(name = "FIRSTNAME")
    public String getfirstname(){
        return this.firstname.get();
    }

    public void setFirstname(String firstname) {
        this.firstname.set(firstname);
    }

    public StringProperty lastnameProperty() {
        return lastname;
    }

    @Column(name = "LASTNAME")
    public String getLastname(){
        return this.lastname.get();
    }

    public void setLastname(String lastname) {
        this.lastname.set(lastname);
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((firstname == null) ? 0 : firstname.get().hashCode());
        result = prime * result + ((lastname == null) ? 0 : lastname.get().hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        PersonPK other = (PersonPK) obj;
        if (firstname == null) {
            if (other.firstname != null)
                return false;
        } else if (!firstname.get().equals(other.firstname.get()))
            return false;
        if (lastname == null) {
            if (other.lastname != null)
                return false;
        } else if (!lastname.get().equals(other.lastname.get()))
            return false;
        return true;
    }

}

Class Person


import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;
import javafx.beans.property.StringProperty;

@Entity
@Table(name = "PERSON")
public class Person implements Serializable {

    private static final long serialVersionUID = 1L;

    @EmbeddedId
    PersonPK personPK;

    StringProperty comentario;

    public Person(){
        this.personPK=new PersonPK();
    }

    public PersonPK getPersonPK(){
        return this.personPK;
    }

    public void setPersonPK(PersonPK personPK){
        this.personPK=personPK;
    }

    @Column(name="COMMENTARY")
    public String getComentario(){
        return this.comentario.get();
    }

    public StringProperty comentarioProperty() {
        return comentario;
    }

    public void setComentario(String comentario) {
        this.comentario.set(comentario);
    }
}

The error I get is the following:

  

Caused by: org.hibernate.MappingException: Could not determine type   for: javafx.beans.property.StringProperty, at table: PERSON, for   columns: [org.hibernate.mapping.Column (comment)] at   org.hibernate.mapping.SimpleValue.getType (SimpleValue.java:455) at   org.hibernate.mapping.SimpleValue.isValid (SimpleValue.java:422) at   org.hibernate.mapping.Property.isValid (Property.java:226) at   org.hibernate.mapping.PersistentClass.validate (PersistentClass.java:595)     at org.hibernate.mapping.RootClass.validate (RootClass.java:265) at   org.hibernate.boot.internal.MetadataImpl.validate (MetadataImpl.java:329)     at   org.hibernate.boot.internal.SessionFactoryBuilderImpl.build (SessionFactoryBuilderImpl.java:492)     at   org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build (EntityManagerFactoryBuilderImpl.java:878)     ... 26 more

    
asked by Fco Javier Pelegrin 20.10.2016 в 17:15
source

1 answer

1

What the error tells you is that you can not find a way to convert an object of the class StringProperty to an SQL type at the time of mapping between the Java objects and the database elements. For this, you could create a set of converters as explained in the hibernate guide 5 3.7.2. Attribute Converter .

The code here for a converter:

@Entity
@Table(name = "PERSON")
public class Person implements Serializable {

    private static final long serialVersionUID = 1L;

    @EmbeddedId
    PersonPK personPK;

    @Convert( converter = StringPropertyConverter.class )
    StringProperty comentario;
}

@Converter
public class StringPropertyConverter extends implements AttributeConverter<String, StringProperty> {
    public String convertToDatabaseColumn(StringProperty value) {
        if (value == null) {
            return null;
        }
        return value.getValue();
    }

    public StringPropertyconvertToEntityAttribute(String value) {
        if (value == null) {
            return null;
        }
        StringProperty result = new StringProperty();
        result.setValue(value);
        return result;
    }
}

Personally, I do not consider this to be the best solution. JavaFX may at the time of injecting your attribute StringProperty in your instance of class execute more statements as "tie" the property to a field of the view, add the events by listeners, among others. I think that a better design would be to separate the view of the model and find another way to relate them.

    
answered by 20.10.2016 / 23:30
source