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:
This is my dao: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 applicationContext.xml: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); } }
I try to remove an entity from a list that feed using this method in dao: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>
And this is my JSF ManagedBean which I try these within it: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; }
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?Code:@ManagedBean(name = "entityBean") @SessionScoped public class EntityBean implements Serializable {


Reply With Quote
