Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JSF/Spring/JPA/Hibernate应用程序中type=PersistenceContextType.EXTENDED和javax.persistence.OptimisticLockException之间的冲突_Spring_Hibernate_Jsf_Jpa_Transactions - Fatal编程技术网

JSF/Spring/JPA/Hibernate应用程序中type=PersistenceContextType.EXTENDED和javax.persistence.OptimisticLockException之间的冲突

JSF/Spring/JPA/Hibernate应用程序中type=PersistenceContextType.EXTENDED和javax.persistence.OptimisticLockException之间的冲突,spring,hibernate,jsf,jpa,transactions,Spring,Hibernate,Jsf,Jpa,Transactions,我正在尝试将一个纯JSF/Hibernate web应用程序移植到JSF/Spring3.0/Hibernate/JPA应用程序。 我使用依赖注入将EntityManager的一个实例注入到我的DAO类中。 如果我在@PersistenceContext注释上使用type=PersistenceContextType.EXTENDED属性, 状态是跨浏览器共享的,并且不会显示登录页面。我尝试了不同的浏览器,而不仅仅是不同的窗口 就像 如果我没有指定EXTENDED,我将无法在第一次更新后更新记录

我正在尝试将一个纯JSF/Hibernate web应用程序移植到JSF/Spring3.0/Hibernate/JPA应用程序。 我使用依赖注入将EntityManager的一个实例注入到我的DAO类中。 如果我在@PersistenceContext注释上使用type=PersistenceContextType.EXTENDED属性, 状态是跨浏览器共享的,并且不会显示登录页面。我尝试了不同的浏览器,而不仅仅是不同的窗口 就像 如果我没有指定EXTENDED,我将无法在第一次更新后更新记录并获得javax.persistence.OptimisticLockException

我最近发现了一个叫做视图范围的东西,但不确定这是否是解决方案。请提出实现这两个目标的方法 A.避免后续更新时出现OptimisticLockException B避免跨浏览器共享状态

谢谢 萨加尔R.卡帕迪亚

我的配置和源文件如下

DataSource.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

 <bean 
   class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   <property name="location">
     <value>WEB-INF/classes/config/database/db.properties</value>
   </property>
</bean>

  <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" >
    <property name="driverClass" value="${jdbc.driverClassName}" />
    <property name="jdbcUrl" value="${jdbc.url}" />
    <property name="user" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
  </bean>

</beans>

WEB-INF/classes/config/database/db.properties
HibernateSessionFactory.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<!-- Hibernate session factory -->
<bean id="sessionFactory" 
     class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >

    <property name="dataSource">
      <ref bean="dataSource"/>
    </property>

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect</prop>

            <prop key="transaction.factory_class">
                net.sf.hibernate.transaction.JDBCTransactionFactory
            </prop>
            <!-- 
            <prop key="hibernate.transaction.factory_class">
                net.sf.hibernate.transaction.JDBCTransactionFactory
            </prop>
             -->
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.cglib.use_reflection_optimizer">false</prop>
            <prop key="hibernate.jdbc.batch_size">0</prop>

            <prop key="hibernate.c3p0.min_size" >2</prop>
            <prop key="hibernate.c3p0.max_size"> 5</prop>
            <prop key="hibernate.c3p0.timeout">600</prop>
            <prop key="hibernate.c3p0.max_statements">0</prop>
            <prop key="hibernate.c3p0.idle_test_period" >300</prop>
            <prop key="hibernate.c3p0.acquire_increment">1</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
           <prop key="hibernate.show_sql">true</prop>
       </props>
    </property>

    <property name="mappingResources">
    <list>  
        <value>com/compucloud/galleryadmin/entity/Period.hbm.xml</value>
        <value>com/compucloud/galleryadmin/entity/ApplicationUser.hbm.xml</value>
        <value>com/compucloud/galleryadmin/entity/Device.hbm.xml</value>
        <value>com/compucloud/galleryadmin/entity/Deal.hbm.xml</value>
        <value>com/compucloud/galleryadmin/entity/ComboDealItem.hbm.xml</value>
        <value>com/compucloud/galleryadmin/entity/City.hbm.xml</value>
        <value>com/compucloud/galleryadmin/entity/DiscountDeal.hbm.xml</value>
        <value>com/compucloud/galleryadmin/entity/State.hbm.xml</value>
        <value>com/compucloud/galleryadmin/entity/FreeDeal.hbm.xml</value>
        <value>com/compucloud/galleryadmin/entity/OrderDetail.hbm.xml</value>
        <value>com/compucloud/galleryadmin/entity/Product.hbm.xml</value>
        <value>com/compucloud/galleryadmin/entity/Country.hbm.xml</value>
        <value>com/compucloud/galleryadmin/entity/Category.hbm.xml</value>
        <value>com/compucloud/galleryadmin/entity/Administrator.hbm.xml</value>
        <value>com/compucloud/galleryadmin/entity/Company.hbm.xml</value>
        <value>com/compucloud/galleryadmin/entity/ComboDeal.hbm.xml</value>
        <value>com/compucloud/galleryadmin/entity/Order.hbm.xml</value>
    </list>
     </property>    

</bean>
</beans>

net.sf.hibernate.dialogue.mysqldialogue
net.sf.hibernate.transaction.JDBCTransactionFactory
假的
假的
0
2.
5.
600
0
300
1.
org.hibernate.dialogue.mysqldialogue
真的
com/compucloud/galleryadmin/entity/Period.hbm.xml
com/compucloud/galleryadmin/entity/ApplicationUser.hbm.xml
com/compucloud/galleryadmin/entity/Device.hbm.xml
com/compucloud/galleryadmin/entity/Deal.hbm.xml
com/compucloud/galleryadmin/entity/ComboDealItem.hbm.xml
com/compucloud/galleryadmin/entity/City.hbm.xml
com/compucloud/galleryadmin/entity/DiscountDeal.hbm.xml
com/compucloud/galleryadmin/entity/State.hbm.xml
com/compucloud/galleryadmin/entity/FreeDeal.hbm.xml
com/compucloud/galleryadmin/entity/OrderDetail.hbm.xml
com/compucloud/galleryadmin/entity/Product.hbm.xml
com/compucloud/galleryadmin/entity/Country.hbm.xml
com/compucloud/galleryadmin/entity/Category.hbm.xml
com/compucloud/galleryadmin/entity/Administrator.hbm.xml
com/compucloud/galleryadmin/entity/Company.hbm.xml
com/compucloud/galleryadmin/entity/ComboDeal.hbm.xml
com/compucloud/galleryadmin/entity/Order.hbm.xml
faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
    version="2.0">

    <application>
        <el-resolver>
            org.springframework.web.jsf.el.SpringBeanFacesELResolver
        </el-resolver>
         <variable-resolver>
            org.springframework.web.jsf.DelegatingVariableResolver
        </variable-resolver>

    </application>
<!--Navigation rules omitted-->
</faces-config>

org.springframework.web.jsf.el.SpringBeanFacesELResolver
org.springframework.web.jsf.DelegatingVariableResolver
web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    id="WebApp_ID" version="2.5">

  <display-name>HelloJSF</display-name>

  <!-- Add Support for Spring -->
  <listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>
  <listener>
    <listener-class>
        org.springframework.web.context.request.RequestContextListener
    </listener-class>
  </listener>

  <!-- Change to "Production" when you are ready to deploy -->
  <context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
  </context-param>

  <!-- Welcome page -->
  <welcome-file-list>
    <welcome-file>faces/login.xhtml</welcome-file>
  </welcome-file-list>

  <!-- JSF mapping -->
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <!-- Map these files with JSF -->
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.faces</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
  </servlet-mapping>



 <filter>
    <filter-name>Extensions Filter</filter-name>
    <filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>Extensions Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

</web-app>

HelloJSF
org.springframework.web.context.ContextLoaderListener
org.springframework.web.context.request.RequestContextListener
javax.faces.PROJECT_阶段
发展
faces/login.xhtml
Facesservlet
javax.faces.webapp.FacesServlet
1.
Facesservlet
/面孔/*
Facesservlet
*.jsf
Facesservlet
*.面孔
Facesservlet
*.xhtml
扩展过滤器
org.apache.myfaces.webapp.filter.ExtensionsFilter
扩展过滤器
Facesservlet
applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 <aop:aspectj-autoproxy proxy-target-class="true"/>
 <context:component-scan base-package="com.compucloud.galleryadmin" />

 <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="database" value="MYSQL"></property>
        <property name="showSql" value="true"></property>
        <property name="generateDdl" value="false"></property>
        <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect"></property>
    </bean>



    <bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >
        <property name="dataSource" ref="dataSource"></property>
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter"></property>

    </bean>
    <!-- 
    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor">

    </bean>
    <tx:annotation-driven />
    -->
    <tx:annotation-driven transaction-manager="demoTxManager"/>
    <context:component-scan base-package="com.compucloud.galleryadmin" />
    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"></bean>


    <bean id="demoTxManager" class="org.springframework.orm.jpa.JpaTransactionManager" >
        <property name="entityManagerFactory" ref="emf" />
    </bean>

    <aop:config>
        <aop:pointcut id="jpaDaoMethods" 
            expression="execution(* com.compucloud.galleryadmin.databaseutil.Database.*.*(..))" />
        <aop:advisor advice-ref="demoTxAdvice" pointcut-ref="jpaDaoMethods" />
    </aop:config>

    <tx:advice id="demoTxAdvice" transaction-manager="demoTxManager" >
    <!-- 
        <tx:attributes>
            <tx:method name="persist*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="reset*" propagation="SUPPORTS" />
            <tx:method name="load*" propagation="SUPPORTS" read-only="true" />


        </tx:attributes>
         -->
    </tx:advice>

        <!-- 
            <tx:method name="retrieve*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="query*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="find*" propagation="SUPPORTS" read-only="false" />
             -->


    <!-- Database Configuration -->
    <import resource="classes/config/spring/beans/DataSource.xml"/>
    <import resource="classes/config/spring/beans/HibernateSessionFactory.xml"/>



    <bean id="database" class="com.compucloud.galleryadmin.databaseutil.Database" scope="session" ></bean>

    <bean id="country" class="com.compucloud.galleryadmin.entity.Country" scope="request"></bean>
    <bean id="state" class="com.compucloud.galleryadmin.entity.State" scope="request"></bean>
    <bean id="city" class="com.compucloud.galleryadmin.entity.City" scope="request"></bean>
    <bean id="administrator" class="com.compucloud.galleryadmin.entity.Administrator" scope="request" ></bean>
    <bean id="companyAdministrator" class="com.compucloud.galleryadmin.entity.Administrator" scope="request"></bean>
    <bean id="currentAdministrator" class="com.compucloud.galleryadmin.entity.Administrator" scope="session"></bean>
    <bean id="currentCompany" class="com.compucloud.galleryadmin.entity.Company" scope="session"></bean>
    <bean id="company" class="com.compucloud.galleryadmin.entity.Company" scope="request" ></bean>
    <bean id="category" class="com.compucloud.galleryadmin.entity.Category" scope="request"></bean>
    <bean id="product" class="com.compucloud.galleryadmin.entity.Product" scope="request"></bean>
    <bean id="deal" class="com.compucloud.galleryadmin.entity.Deal" scope="request" ></bean>
    <bean id="discountDeal" class="com.compucloud.galleryadmin.entity.DiscountDeal" scope="request" ></bean>
    <bean id="freeDeal" class="com.compucloud.galleryadmin.entity.FreeDeal" scope="request"></bean>
    <bean id="comboDeal" class="com.compucloud.galleryadmin.entity.ComboDeal" scope="request"></bean>

</beans>

Database.java

@Repository
@Transactional

public class Database  implements Serializable{

    @PersistenceContext()
    private EntityManager em;


    private Exception lastException;
    private Category category;
    private Country country;  
    private State state;
    private City city;
    private Administrator administrator;
    private Administrator companyAdministrator;
    private Administrator currentAdministrator;
    private Company company;
    private Company currentCompany;
    private Deal deal;
    private DiscountDeal discountDeal;
    private FreeDeal freeDeal;
    private ComboDeal comboDeal;

    //private Deal currentDeal;
    private List<Company>listOfCompanies;
    private List<Country>listOfCountries;
    private List<State>listOfStates;
    private List<City>listOfCities;
    private List<Administrator>listOfAdministrators;
    private List<Administrator>listOfCompanyAdministrators;
    private List<Category> listOfCategories;
    private List<Category> listOfChildCategories;
    private Category currentCategory;
    private Product product;
    private Product productForComboDeal;

    private List<Product>listOfProducts;


    private List<Deal> listOfDeals;
    private List<DiscountDeal> listOfDiscountDeals;
    private List<FreeDeal> listOfFreeDeals;
    private List<ComboDeal> listOfComboDeals;
    /*
    private Session m_Session;
    private SessionFactory sessionFactory;
    */
    /*
    private UploadedFile uploadedFileCategory;
    private UploadedFile uploadedFileProduct;
    private UploadedFile uploadedFileProductLarge;*/

    public Database(){

    }
    public void persistWithUpdatedFlag(Object obj){
        obj=em.merge(obj);
        em.flush();
        refreshBeans(obj);
        }

//Other Methods omitted
}
@存储库
@交易的
公共类数据库实现可序列化{
@PersistenceContext()
私人实体管理者;
私人例外;
私人类别;
私人国家;
私营国家;
私人城市;
私人管理员;
私人管理人公司管理人;
私人管理员;
私营公司;
私营公司;
私人交易;
私人折扣交易折扣交易;
私人自由交易;
私人组合交易;
//私人交易;
私人公司名单;
国家私人名单;
私人国家名单;
私人城市名单;
私人行政人员名单;
公司管理人员的私人名单;
类别的私人名单;
儿童类别私人名单;
私人类;
私人产品;
私人产品交易;
私人产品清单;
私人药物清单;
披露交易的私人清单;
私人自由交易清单;
私人上市公司;
/*
非公开会议m_会议;
私人会话工厂会话工厂;
*/
/*
私有上传文件上传文件类别;
私有上传文件上传文件产品;
私有上传文件上传文件产品大*/
公共数据库(){
}
public void persistWithUpdatedFlag(对象obj){
obj=em.merge(obj);
em.flush();
咖啡豆(obj);
}
//省略了其他方法
}

EXTENDED意味着在您显式关闭持久性上下文之前,它不会被关闭。由于您的
数据库是一个单例bean,因此EntityManager将和您的bean一样存在(也就是说,永远存在)。这就是为什么在所有线程中都会看到相同的事务管理器,因为在扩展场景中实际上只创建了一个事务管理器

您需要的是“在视图中打开实体管理器”模式,因此实体管理器(持久性上下文)在请求期间保持打开状态