Results 1 to 2 of 2

Thread: why OpenEntityManagerInViewFilter doesn't work for me?

  1. #1
    Join Date
    Jun 2011
    Posts
    5

    Default why OpenEntityManagerInViewFilter doesn't work for me?

    In my JSF2-JPA2-Spring3 project, I can insert new entities but can't remove entities. This is the error message: java.lang.IllegalArgumentException: Removing a detached instance entity.Entity#8

    This is my persistence.xml:
    Code:
    <?xml version="1.0" encoding="UTF-8" ?>
    
    http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
    
    <persistence-unit name="myPersistenceUnit"
        transaction-type="RESOURCE_LOCAL">
        <properties>            
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            <property name="hibernate.show_sql" value="true" />
        </properties>   
    </persistence-unit>


    In my JSF2-JPA2-Spring3 project, I can insert new entities but can't remove entities. This is the error message: java.lang.IllegalArgumentException: Removing a detached instance entity.Entity#8

    This is my persistence.xml:

    <?xml version="1.0" encoding="UTF-8" ?>

    http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">

    <persistence-unit name="myPersistenceUnit"
    transaction-type="RESOURCE_LOCAL">
    <properties>
    <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
    <property name="hibernate.show_sql" value="true" />
    </properties>
    </persistence-unit>

    This is my service:

    Code:
    @Service("myService")
    
    public class MyServiceImpl implements MyService {
    
    @Resource(name="MyRepository")
    MyDAO myDao;
    
    @Transactional
    public void deleteEntity(Entity entity) throws DAOException {
        myDao.delete(entity);
    }
    This is my dao:

    Code:
    @Repository("MyRepository")
    
    public class UserDAO{
    
    private EntityManager entityManager;
    
    @PersistenceContext
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }
    
    public void delete(Entity entity) throws Exception {
    
        try {
            entityManager.remove(entity);
        } catch (DataAccessException e) {
            throw new Exception(e);
        }
    }
    This is applicationContext.xml:

    Code:
    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" >
    
            </bean>
        </property>
        <property name="dataSource" ref="myDataSource" />
        <property name="persistenceUnitName" value="myPersistenceUnit"/> 
    </bean>
    
    <bean name="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />     
    </bean>
    
    <tx:annotation-driven />
    <tx:annotation-driven  transaction-manager="transactionManager"/>
    
    
    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
    
    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
    
    <tx:advice id="transactionInterceptor" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" rollback-for="Throwable" />
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:advisor pointcut=" execution(* service.*Service.*(..))"
            advice-ref="transactionInterceptor" />
    </aop:config>
    I try to remove an entity from a list that feed using this method in dao:

    Code:
    public List<Entity> getAll() throws Exception {
        List<Entity> list = null;
    
        try {
              list = entityManager.createQuery("Select e from Entity e").getResultList();
        } catch (DataAccessException e) {
            throw new Exception(e);
        }
    
        return list;
    }
    And this is my JSF ManagedBean which I try these within it:

    Code:
    @ManagedBean(name = "entityBean") @SessionScoped public class EntityBean implements Serializable {
    I'm trying to use org.springframework.orm.jpa.support.OpenEntityMana gerInViewFilter but it doesn't work for me. Also, somewhere I saw that folk say using an open database connection in view pattern is not always a good solution when it comes to performance and memory efficency. If so, what are the alternatives?

  2. #2
    Join Date
    May 2011
    Location
    New Delhi, India
    Posts
    157

    Default

    Check the flush mode. If the working is similar to the Hibernate OpenSessionInViewInterceptor, then the flush mode would have been set to equivalent of FlushMode.NEVER. Look at
    http://static.springsource.org/sprin...terceptor.html.

    You need to check how it is being used by the application, ideally you would only be loading data needed for the views. The other option is not very elegant. You will need to prefetch the data needed by views in your service/DAO layer. This will get messy if the same service layer is used by multiple views. I feel that EntityManagerInViewFilter will be better option.
    Last edited by rishishehrawat; Jun 23rd, 2011 at 10:46 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •