Vaadin JpaContainer

Ale*_*nov 12 hibernate vaadin

我正在使用JPAContainer + Hibernate,加载需要很长时间.例如,SQLContainer加载60ms的页面和JPA Container加载1.30s的相同页面.

在控制台中使用JPAContainer,我看到许多SQL查询 - 对于每个实体 - 查询; 实体人员没有其他表格的链接;

使用jpacontainer的代码:

JPAContainer<Person> container = JPAContainerFactory.make(Person.class,
            "persistence-unit");
table.setContainerDataSource(container);
Run Code Online (Sandbox Code Playgroud)

使用SQLContainer的代码:

JDBCConnectionPool pool = null;
    try {
        pool = new SimpleJDBCConnectionPool("org.postgresql.Driver",
                "jdbc:postgresql://127.0.0.1:5432/postgres", "postgres",
                "pwd");
    } catch (SQLException e) {
        e.printStackTrace();
    }
    TableQuery tq = new TableQuery("Person", pool);
    SQLContainer sqlContainer = null;
    try {
        sqlContainer = new SQLContainer(tq);
    } catch (SQLException e) {
        e.printStackTrace();
    }
table.setContainerDataSource(sqlContainer);
Run Code Online (Sandbox Code Playgroud)

我的persistence.xml文件:

<persistence-unit name="persistence-unit" transaction-type="RESOURCE_LOCAL">

  <jta-data-source>java:jboss/datasources/mfc-frontendDS</jta-data-source>

  <properties>
     <!-- Properties for Hibernate -->
    <property name="hibernate.archive.autodetection" value="class"/>
    <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
    <property name="hibernate.show_sql" value="true"/>
    <property name="hibernate.format_sql" value="true"/>
    <property name="hibernate.use_sql_comments" value="true"/>
    <property name="hibernate.hbm2ddl.auto" value="update"/>
    <property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>
    <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform" /> 
  </properties>
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Ren*_*nov 6

停止战斗JPAContainer,背后有太多的抽象层.

在此输入图像描述

SQLContainer足够好,快速而稳定.我不是说SQLContainer是JPAContainer的替代品,但实际价格似乎太高了.从可用性的角度来看,响应性是一个非常重要的因素,因此最好不要在持久层上花费几秒钟.

无论如何,如果你真的想继续使用JPAContainer,有几种选择:

使用CachingLocalEntityProvider

经验法则:访问速度慢 - 使用缓存

如果应减少数据库往返次数,则应使用CachingLocalEntityProvider.它维护实体和查询结果的本地缓存,因此如果数据库往返很慢,它应该比LocalEntityProvider执行得更快.但是,它还需要比LocalEntityProvider更多的内存.

使用分页(PagedTable)

它将大大减少查询数量,因为页面是延迟加载的.

PagedTable是一个与Vaadin核心表行为相同的组件,除了它有多个页面而不是滚动以显示更多条目.

在此输入图像描述

使用JPAContainer过滤器

所有过滤都在数据库级别使用查询完成,而不是在容器中完成.过滤实现透明地使用JPA 2.0 Criteria API.由于过滤是在数据库级别完成的,因此使用Filterable API的自定义过滤器不起作用.

另请查看:JPAContainer使用和性能问题