0

2014-12-02 修订 - eclipselink/mysql 的 persistence.xml 和 wildfly 配置 2014-12-02 修订 - 更好的问题信息、建议的完整代码和屏幕截图。

我在一对多关系中有两个 MYSQL 表。我使用绑定到关联 JPAContainer(productsContainer、priceContainer)的两个表(tbl_products、tbl_prices)在 Vaadin 中显示它们。在 tbl_prices 中,我使用 priceContainer.addNestedContainerProperty() 显示第一个表中的一个字段。

问题是,当我更改 tbl_products 中的 QTY 字段值时,更改会立即反映到数据库中,但是 tbl_prices 中的嵌套属性在浏览器刷新之前永远不会知道更改。(pricesContainer.getItem(itemId).getProperty("product.qty") 返回旧值)。清楚地显示在帖子末尾的数量更新后的屏幕截图中。

tbl_prices.refreshRowCache() 和 pricesContainer.refresh() 没有帮助,尽管容器刷新应该刷新数据库中的值)

对于如何解决此问题,我将不胜感激。无法在 vaadin.com 论坛上获得答案。以下是实体的配置方式:

@Entity
@Table(name="products")
@NamedQuery(name="Product.findAll", query="SELECT p FROM Product p")
public class Product implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(unique=true, nullable=false)
    private int id;

    @Column(nullable=false, length=255)
    private String name;

    @Column(nullable=false)
    private int qty;

    //bi-directional many-to-one association to Price
    @OneToMany(mappedBy="product")
    private List<Price> prices;

    ... constructor, getters and setters
}

@Entity
@Table(name="prices")
@NamedQuery(name="Price.findAll", query="SELECT p FROM Price p")
public class Price implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(unique=true, nullable=false)
    private int id;

    @Column(nullable=false, length=255)
    private String description;

    @Column(nullable=false, precision=10, scale=2)
    private BigDecimal price;

    //bi-directional many-to-one association to Product
    @ManyToOne
    @JoinColumn(name="prodid", nullable=false)
    private Product product;

    @Transient
    private BigDecimal value;
    public BigDecimal getValue() {
        return this.price.multiply(new BigDecimal(product.getQty()));
    }

    ... constructor, getters and setters
}

public class UI_Tables_Test extends CustomComponent {
    public static final String PERSISTENCE_UNIT = "vaadin_sandbox";
    public JPAContainer<Product> productsContainer;
    public JPAContainer<Price> pricesContainer;
    private FieldGroup formFieldGroup;

    public UI_Tables_Test() {
        buildMainLayout();
        setCompositionRoot(mainLayout);

        // Containers
        productsContainer = JPAContainerFactory.make(Product.class, PERSISTENCE_UNIT);
        pricesContainer   = JPAContainerFactory.make(Price.class, PERSISTENCE_UNIT);
        pricesContainer.addNestedContainerProperty("product.*");

        // Tables
        tbl_products.setContainerDataSource(productsContainer);
        tbl_products.setSelectable(true);
        tbl_products.setImmediate(true);

        tbl_prices.setContainerDataSource(pricesContainer);
        tbl_prices.setSelectable(true);
        tbl_prices.setImmediate(true);

        // Columns
        tbl_products.setVisibleColumns(new Object[] {"id","name", "qty"});
        tbl_prices.setVisibleColumns(new Object[] {"id", "product.name", "product.qty", "price", "value"});

        // Form
        formFieldGroup = new FieldGroup(new BeanItem<Product>(new Product()));
        formFieldGroup.setBuffered(false);
        formFieldGroup.bind(tf_name, "name");
        formFieldGroup.bind(tf_qty, "qty");

        // ValueChangeListener to set form data source
        tbl_products.addValueChangeListener(new Property.ValueChangeListener() {
            private static final long serialVersionUID = 7133249924369468095L;

            @Override
            public void valueChange(ValueChangeEvent event) {
                Object selectedrow = event.getProperty().getValue();
                if (selectedrow != null) {
                    formFieldGroup.setItemDataSource(tbl_products.getItem(selectedrow));
                }
            }
        });

        // Property ValueChangeListener to refresh Prices table
        productsContainer.getItem(2).getItemProperty("qty").addValueChangeListener(new Property.ValueChangeListener() {
            @Override
            public void valueChange(ValueChangeEvent event) {
                refreshTable2();
            }
        });
    }

    public void refreshTable2() {
        // trying various refresh methodologies 
        pricesContainer.refresh();
        pricesContainer.getEntityProvider().refreshEntity(pricesContainer.getItem(3).getEntity());
        tbl_prices.refreshRowCache();

        // trying removing and adding nested container property again
        pricesContainer.removeContainerProperty("product.*");
        pricesContainer.addNestedContainerProperty("product.*");        

        // trying resetting table2 container data source
        tbl_prices.setContainerDataSource(pricesContainer);
        pricesContainer.addNestedContainerProperty("product.*");        
        tbl_prices.setVisibleColumns(new Object[] {"id", "product.name", "product.qty", "price", "value"});

        // popup message to display the values read from the containers
        String msg = "Product container Qty= " +
                     String.valueOf(productsContainer.getItem(2).getEntity().getQty()) + "\n" +
                     "Price container Qty= " +
                     String.valueOf(pricesContainer.getItem(3).getEntity().getProduct().getQty()) + "\n" +
                     pricesContainer.getContainerProperty(3, "product.qty");

        Notification.show("Test", msg, Notification.Type.ERROR_MESSAGE);
    }

    ... build layout
}

这是两个屏幕截图(保管箱链接,因为声誉点不足以发布图像)

数量更新前: https ://www.dropbox.com/s/ej459r0v80popsb/before_quantity_update.png?dl=0

数量更新后: https ://www.dropbox.com/s/ryl358pyymguonl/after_quantity_update.png?dl=0

这是persistence.xml和相关的wildfly配置

persistence.xml
---------------
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" 
    xmlns="http://xmlns.jcp.org/xml/ns/persistence" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="vaadin_sandbox" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <non-jta-data-source>java:/jdbc/vaadin_sandbox</non-jta-data-source>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <shared-cache-mode>NONE</shared-cache-mode>
        <properties>
            <property name="eclipselink.deploy-on-startup" value="True" />
            <property name="eclipselink.target-server" value="JBoss"/>
            <property name="eclipselink.logging.level" value="FINE" />
        </properties>
    </persistence-unit>
</persistence>

wildfly config:
--------------
eclipselink.jar in /usr/local/wildfly-8.1.0.Final/modules/system/layers/base/org/eclipse/persistence/main
corresponding module.xml
<module xmlns="urn:jboss:module:1.1" name="org.eclipse.persistence">
    <resources>
        <resource-root path="jipijapa-eclipselink-1.0.1.Final.jar"/>
        <resource-root path="eclipselink.jar"/>
    </resources>
    <dependencies>
    ...
    </dependencies>
</module>

workaround for  issueid=414974
$ bin/jboss-cli.sh —connect
[standalone@localhost:9990 /] /system-property=eclipselink.archive.factory:add(value=org.jipijapa.eclipselink.JBossArchiveFactoryImpl)

mysql-connector-java-5.1.30-bin.jar in /usr/local/wildfly-8.1.0.Final/modules/system/layers/base/com/mysql/main
corresponding module.xml
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="com.mysql">
    <resources>
        <resource-root path="mysql-connector-java-5.1.30-bin.jar"/>
    </resources>
    <dependencies>
        <module name="javax.api"/>
        <module name="javax.transaction.api"/>
    </dependencies>
</module>

$ bin/jboss-cli.sh --connect
[standalone@localhost:9990 /] /subsystem=datasources/jdbc-driver=mysql:add(driver-name=mysql,driver-module-name=com.mysql,driver-class-name=com.mysql.jdbc.Driver)
[standalone@localhost:9990 /] /subsystem=datasources/data-source=vaadin_sandbox:add(driver-name=mysql, user-name=secret, password=secret, connection-url=jdbc:mysql://localhost:3306/vaadin_sandbox?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8, min-pool-size=5, max-pool-size=15, jndi-name=java:/jdbc/innodron, enabled=true, validate-on-match=true, valid-connection-checker-class-name=org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker, exception-sorter-class-name=org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter)
4

1 回答 1

0

修订:在这个问题上花费几个小时后记录我的发现

常见:Wildfly 8.1.0.Final、MySQL 5.6.20、Vaadin 7.3.5、EclipseLink 2.5.2、javax.persistence 2.1.0、mysql-connector-java 5.1.34

案例一)persistence.xml transaction-type="RESOURCE_LOCAL", shared_cache_mode="NONE"

在这种情况下,清除 EntityManagerFactory 缓存似乎是解决问题的唯一方法。

public void refreshTable2() {
    //Evict Entity Manager Cache for the specific item
    em.getEntityManagerFactory().getCache().evict(Price.class, 3);
    // if container is not refreshed, tbl_prices is not updated in the UI.
    pricesContainer.refresh(); 

    // popup message to display the values read from the containers
    String msg = "Product container Qty= " +
                 String.valueOf(productsContainer.getItem(2).getEntity().getQty()) + "\n" +
                 "Price container Qty= " +
                 String.valueOf(pricesContainer.getItem(3).getEntity().getProduct().getQty()) + "\n" +
                 pricesContainer.getContainerProperty(3, "product.qty");

    Notification.show("Test", msg, Notification.Type.ERROR_MESSAGE);
}

案例2)persistence.xml transaction-type="RESOURCE_LOCAL", shared_cache_mode="ALL"

在这种情况下,不需要逐出 EntityManagerFactory 缓存,但 pricesContainer 需要手动刷新嵌套属性以刷新其值。

public void refreshTable2() {
    // if container is not refreshed, tbl_prices is not updated in the UI.
    pricesContainer.refresh(); 
}

案例 3) persistence.xml transaction-type="JTA"、shared_cache_mode="ALL" 或 "NONE"。(查看此博客条目以了解如何使 Vaadin 与 JTA 事务一起使用)

对于这两种情况,无需逐出 EntityManagerFactory 缓存,但 pricesContainer 需要手动刷新嵌套属性以刷新其值。上边是 shared_cache_mode 可以设置为“NONE”,这似乎是 Vaadin JPAContainer 玩得好所需要的(根据 Vaadin 论坛中的几个建议)

public void refreshTable2() {
    // if container is not refreshed, tbl_prices is not updated in the UI.
    pricesContainer.refresh(); 
}

欢迎任何关于如何在没有人工干预的情况下刷新嵌套属性的建议。在实际项目中,手动刷新将需要一个 Container.addItemSetChangeListener() 用于源容器,一个 Container.getItem(i).addValueChangeListener() 用于容器中的每个项目 - 这将产生巨大的开销。

于 2014-12-02T00:59:47.840 回答