Put together JavaFX properties and JPA Entities (NO MIXED MODE)

前端 未结 2 673
不思量自难忘°
不思量自难忘° 2021-01-03 03:53

Base

I have a mysql DB managed by JPA (EclipseLink) (Entities and Controllers + persistence unit). The GUI is JavaFX based.

Informat

2条回答
  •  隐瞒了意图╮
    2021-01-03 04:15

    Another possible embedded, built-in, AFAIK solution.

    From Adapter pattern :

    ...allows the interface of an existing class to be used from another interface. It is often used to make existing classes work with others without modifying their source code.

    This example is for informational purposes only and is not a confirmed solution, but need to write code well structured and flexibly to changes. So...

    Example:

    if we have a JPAEntity like

    @Entity
    @Table(name="EntityClass", uniqueConstraints = {
        @UniqueConstraint(columnNames = {"ID"})})
    @XmlRootElement
    @NamedQueries({
        @NamedQuery(name = "EntityClass.findAll", query = "SELECT a FROM EntityClass a"),
        @NamedQuery(name = "EntityClass.findById", query = "SELECT a FROM EntityClass a WHERE a.id = :id"),
        @NamedQuery(name = "EntityClass.findByYear", query = "SELECT a FROM EntityClass a WHERE a.year = :year")})
    public class EntityClass implements Serializable {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Basic(optional = false)
        @Column(nullable = false)
        private Integer id;
        @Basic(optional = false)
        @Column(nullable = false, length = 4)
        private String year;
        //...and others
    
        private static final long serialVersionUID = 1L;
    
        @OneToMany(cascade = CascadeType.ALL, mappedBy = "fKYear")
        private Collection some1Collection;
        @OneToMany(cascade = CascadeType.ALL, mappedBy = "fKYear")
        private Collection some2Collection;
    
        public EntityClass() {
        }
    
        public EntityClass(Integer id) {
            this.id = id;
        }
    
        public EntityClass(Integer id, String year) {
            this.id = id;
            this.year = year;
        }
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getYear() {
            return year;
        }
    
        public void setYear(String year) {
            this.year = year;
        }
    
        @XmlTransient
        public Collection getSome1Collection() {
            return some1Collection;
        }
    
        public void setSome1Collection(Collection some1Collection) {
            this.some1Collection = some1Collection;
        }
    
        @XmlTransient
        public Collection getSome2Collection() {
            return some2Collection;
        }
    
        public void setSome2Collection(Collection some2Collection) {
            this.some2Collection = some2Collection;
        }
    
        @Override
        public int hashCode() {
            int hash = 0;
            hash += (id != null ? id.hashCode() : 0);
            return hash;
        }
    
        @Override
        public boolean equals(Object object) {
            // TODO: Warning - this method won't work in the case the id fields are not set
            if (!(object instanceof EntityClass)) {
                return false;
            }
            EntityClass other = (EntityClass) object;
            if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
                return false;
            }
            return true;
        }
    
        @Override
        public String toString() {
            return this.year;
        }
    }
    

    and we created an Interface like this

    public interface IFXEntityClass{
        void setYear(String year);
        String getYear();
        //...
        void setSome1Collection(Collection some1);
        Collection getSome1Collection();
        //...
    }
    

    we can create our FXClass like

    public class FXEntityClass implements IFXEntityClass{
        private final StringProperty yearProperty=new SimpleStringProperty();
    
        public StringProperty getYearProperty(){ return this.yearProperty;}
        public void setYear(String year){ this.year.set(year); }
        public String getYear(){ return this.year.get(); }
        //...
        void setSome1Collection(Collection some1)( //do something)
        Collection getSome1Collection(){ return null;}
        //...
    }
    

    Now we have all we needed. Let's create the Adapter.

    public class EntityClassToFXEntityClassAdaptor implements IFXEntityClass{
    
        private EntityClass entity;
        private final StringProperty yearProperty=new SimpleStringProperty();
    
        public EntityClassToFXEntityClassAdaptor(EntityClass e){
            this.entity=e;
            //adapt it as you want
            this.yearProperty.set(e.getYear());
            //...
        }
    
        public StringProperty getYearProperty(){ return this.yearProperty;}
        public void setYear(String year){ this.year.set(year); }
        public String getYear(){ return this.year.get(); }
        //...
        void setSome1Collection(Collection some1)( //do something)
        Collection getSome1Collection(){ return null;}
        //...
    }
    

    And finally we can use it

    EntityClass ec=something; //get an instance of JPAEntity
    
    EntityClassToFXEntityClassAdaptor adaptor=new EntityClassToFXEntityClassAdaptor(ec);
    adaptor.getYearProperty();
    

    And a clearer example of how to apply this pattern you find in

    import java.awt.*;
    public class CheckboxAdapter extends Checkbox{
        public CheckboxAdapter(String n){
            super(n);
        }
    
        public boolean isSelected(){
            return getState();
        }
    
        //... continue
    }
    

提交回复
热议问题