2014-12-02修订 - eclipselink / mysql的persistence.xml和wildfly配置 修订于2014-12-02 - 更好的问题信息,建议的完整代码和截图。
我有两个一对多关系的MYSQL表。我使用绑定到关联JPAContainers(productsContainer,pricesContainer)的两个表(tbl_products,tbl_prices)在Vaadin中显示它们。在tbl_prices中,我使用pricesContainer.addNestedContainerProperty()显示第一个表中的字段。
问题是,当我更改tbl_products中的QTY字段值时,更改会立即反映到数据库中,但是在浏览器刷新之前,tbl_prices中的嵌套属性永远不会知道更改。 (pricesContainer.getItem(itemId).getProperty(“product.qty”)返回旧值)。显示在帖子末尾的After quantity update屏幕截图中。
tbl_prices.refreshRowCache()和pricesContainer.refresh()没有帮助,虽然容器刷新应该刷新DB中的值)
对于如何解决此问题,我将不胜感激。无法在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)
答案 0 :(得分: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
案例1)persistence.xml transaction-type =&#34; RESOURCE_LOCAL&#34;,shared_cache_mode =&#34; NONE&#34;
在这种情况下,清除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 =&#34; RESOURCE_LOCAL&#34;,shared_cache_mode =&#34; ALL&#34;
在这种情况下,不需要驱逐EntityManagerFactory缓存,但priceContainer需要手动刷新嵌套属性以刷新其值。
public void refreshTable2() {
// if container is not refreshed, tbl_prices is not updated in the UI.
pricesContainer.refresh();
}
案例3)persistence.xml transaction-type =&#34; JTA&#34;,shared_cache_mode =&#34; ALL&#34;或&#34;无&#34;。 (检查this blog entry了解如何使Vaadin与JTA事务一起工作)
对于这两种情况都不需要驱逐EntityManagerFactory缓存,但是PriceContainer需要手动刷新嵌套属性以刷新其值。上边是shared_cache_mode可以设置为&#34; NONE&#34;,这似乎是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() - 这将产生巨大的开销。