将JavaFX属性和JPA实体放在一起(无混合模式) [英] Put together JavaFX properties and JPA Entities (NO MIXED MODE)

查看:157
本文介绍了将JavaFX属性和JPA实体放在一起(无混合模式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基础

我有一个由JPA(EclipseLink)(实体和控制器+持久性单元)管理的mysql数据库. GUI是基于JavaFX的.

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

信息

我看了这篇文章:

  • One Bean to Bind Them All
  • JavaFX 2.0 Programming Model
  • JavaFX properties in JPA

和此示例代码

问题

目前,我正在使用 我的适配器类型 (不是真正的适配器模式)将JPAEntity转换为JavaFX Bean

Currently i'm using a my kind of Adapter (not real Adapter pattern) to translate a JPAEntity to JavaFX Bean

public <T, S> Function<T, S> getRefactor() {
    return o -> {
        Object rtn = null;

        //adapt **o** it to JavaFX bean

        return (S) rtn;
    };
}

我认为这不是最好的解决方案.

and it's not the best solution, i think.

问题 没有混合模式! 我相信即使在超级懒惰的实现下,在服务器端使用javafx属性也是疯狂的.

Question NO MIXED MODE ! I believe that using javafx property on the server side, is crazy, even with the super-lazy implementation.

有一个灵活的解决方案可以利用JavaFX Bean的所有优点,例如双向绑定,并保留不变的JPA实体代码?

There is a flexible solution to have all benefits of JavaFX Bean, for example bidirectional binding, and leave unchanged JPA Entities code?

已编辑

即目前,我有JPAEntity + JPAController和FXClass,它们表示" JPAEntity.

i.e. at present i have JPAEntity + JPAController and FXClass, which "represents" the JPAEntity.

JPAEntity是一种旧式POJO,包含要写入DB的数据.

JPAEntity is an old style POJO, contains data to write to DB.

FXClass具有javafx属性,包含要在FX环境中显示的数据.

FXClass has javafx properties, contains data to show in FX environment.

所以...我正在使用中间层来进行两者之间的通信.

So... i'm using an intermediate layer to put in communication the two.

预先感谢

推荐答案

我通常会提倡在JPA实体中使用JavaFX属性:我确实没有明显的理由不这样做.

I would generally advocate using JavaFX properties in JPA entities: I really see no obvious reason not to do so.

但是,如果要避免这样做,可以使用

However, if you want to avoid doing so, you can use JavaBeanPropertyAdapters. These are adapters that create JavaFX observable properties wrapping regular JavaBean properties. So if you have a bean class

@Entity
public class Person {

    private String firstName ;
    private String lastName ;

    @Id
    private Integer id ;

    public String getFirstName() {
        return firstName ;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName ;
    }

    public String getLastName() {
        return lastName ;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName ;
    }
}

然后您可以执行类似的操作

Then you can do something like

TableColumn<Person, String> firstNameCol = new TableColumn<>("First Name");
firstNameCol.setCellValueFactory(cellData -> {
    try {
        return JavaBeanStringPropertyBuilder.create()
            .bean(cellData.getValue())
            .name("firstName")
            .build();
    } catch (NoSuchMethodException exc) {
        throw new RuntimeException(exc);
    }
});

这将创建一个供表中使用的JavaFX属性,并将JavaBean属性单向绑定到该属性:即,如果您更改表中的值,则将更新JavaBean.此设置将不会发生反向绑定,即更改Bean中的值将不会更新表中显示的值.

This will create a JavaFX property for use in the table, and unidirectionally bind the JavaBean property to it: i.e. if you change the value in the table, the JavaBean will be updated. The reverse binding will not occur with this set up, i.e. changing the value in the bean will not update the value displayed in the table.

如果要进行双向绑定,则bean将需要支持属性更改侦听器:

If you want bidirectional binding, your bean will need to support property change listeners:

public class Person {
    private String firstName ;
    private String lastName ;

    private PropertyChangeSupport pcs ;

    public Person() {
        pcs = = new PropertyChangeSupport(this);
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        pcs.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        pcs.removePropertyChangeListener(listener);
    }

    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        String oldName = this.firstName ;
        this.firstName = firstName;
        pcs.firePropertyChange("firstName", oldName, this.firstName);
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        String oldName = this.lastName ;
        this.lastName = lastName;
        pcs.firePropertyChange("lastName", oldName, this.lastName);
    }

}

现在对Bean的更改将传播到表使用的JavaFX属性,反之亦然.

Now changes to the bean will propagate to the JavaFX property used by the table, as well as vice-versa.

这篇关于将JavaFX属性和JPA实体放在一起(无混合模式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆