Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Issues with remove jta, eclipselink with a relational object..

The entityManager is just injected ..

public BaseEclipselinkDAO
{
   protected EntityManager getEntityManager() throws DAOException
    {
        return entityManager;
    }
   @PersistenceContext(unitName="PersistenceEl")
    private EntityManager entityManager = null ;
}


and we use spring to inject the entity manager

 <bean id="entityManagerFactory"
		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="dataSource" ref="ourDataSource" />
		<property name="persistenceUnitName" value="PersistenceEl" />
		<property name="jpaDialect" ref="eclipseLinkDialect" />
		<property name="persistenceXmlLocation"
			value="classpath:META-INF/persistence-eclipse.xml" />
		<property name="jpaVendorAdapter">
			<bean
				class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
				<property name="showSql" value="true" />
				<property name="generateDdl" value="false" />
			</bean>
		</property>

	   <property name="jpaPropertyMap">
			<map>
                <entry key="eclipselink.target-server" value="JBOSS"/>
                <entry key="eclipselink.target-database"
value="Oracle"/>
                <entry
key="eclipselink.persistence-context.flush-mode" value="AUTO"/>
		<entry key="eclipselink.jdbc.native-sql" value="false" />
		<entry key="eclipselink.weaving" value="false" />
		<entry key="eclipselink.logging.logger"
value="org.eclipse.persistence.logging.DefaultSessionLog" />
                <entry key="eclipselink.logging.level" value="FINEST" />
		<entry key="eclipselink.logging.parameters" value="true"/>				
                <entry key="eclipselink.logging.exceptions" value="true" />
		<entry key="eclipselink.orm.throw.exceptions" value="true" />
		<entry key="eclipselink.session.customizer"
value="com.dst.hps.persistence.dao.eclipselink.SessionCustomizers" />
			</map>
		</property>
	</bean>
	
 	<bean id="eclipseLinkDialect"
class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect" />


    <tx:jta-transaction-manager />


> Your first example did not use deleteObject method at all but I see it in
> exception stack so this was just your simplification of real code, am I
> right?
>

Yes - this is just a simplification of the real code. I did get some
ideas from other folks who are having similar issues and i just
simplified the code .

Cheers
Vaidya
	


On Wed, Jul 11, 2012 at 10:22 AM, Rodion Gushchin <graymagi@xxxxxxxxx> wrote:
> Is there a possibility that getEntityManager() return different entity
> manager instance every call or e.g. only inside getObjects or deleteObject
> or somehow implicitly starts new transaction or nested transaction?  Can you
> trace that? May be you can also show source code for getEntityManager()
> method.
> Your first example did not use deleteObject method at all but I see it in
> exception stack so this was just your simplification of real code, am I
> right?
>
>
> On Wed, Jul 11, 2012 at 4:58 PM, vaidya nathan <vaidyaatdst@xxxxxxxxx>
> wrote:
>>
>> Thx for the reply.. Following is the info. that you requested..
>>
>> > 1) What getObjects(UserRole.class, exp) doing? Can you provide source
>> > code
>> > for this call?
>>
>> getObjects(UserRole.class,exp) is as follows
>> {
>>     ReadAllQuery query = (exp == null ? new ReadAllQuery(clazz) : new
>> ReadAllQuery(clazz, exp));
>>     query.setShouldFilterDuplicates(true) ;
>>     Query jpaq = JpaHelper.createQuery(query, getEntityManager()) ;
>>     List<T> result = jpaq.getResultList() ;
>>     return result;
>> }
>>
>>
>> > 2) Why are you doing getEntityManager().merge(urole)? Why not use
>> > entityManager variable and why you are merging entity before removing it
>> > do
>> > you expect it to be detached?
>>
>>
>> We could use the entityManager variable itself .. There is no reason
>> to do a getEntityManager() again .
>> We tried to do a merge of the entity because
>> 1. From various posts that was the way in which we can reattach the entity
>> back
>> 2. Thats what the error asked us to do.
>>
>> Also i modified the deleteObject as follows :
>>
>>     public <T extends PersistableObject> void deleteObject(T entity)
>> throws DAOException
>>     {
>>         try
>>         {
>>             if (getEntityManager().contains(entity)) {
>>                  System.out.println("Succeeding contains");
>>                 getEntityManager().remove(entity);
>>               } else {
>>                 entity = (T)
>> getEntityManager().find(entity.getClass(), entity.getId());
>>                 if (getEntityManager().contains(entity))
>>                 {
>>                     System.out.println("Succeeding contains");
>>                 }
>>                 getEntityManager().remove(entity);
>>               }
>>
>>         }
>>         catch (PersistenceException e)
>>         {
>>             e.printStackTrace() ;
>>             throw new DAOException(e.getMessage()) ;
>>         }
>>     }
>>
>> it never prints    "Succeeding contains"  - So we are not able to
>> remove the entity because the entity is never getting managed/attached
>> to UOW .
>>
>> Cheers
>> Vaidya
>>
>>
>>
>>
>>
>> On Wed, Jul 11, 2012 at 8:08 AM, Rodion Gushchin <graymagi@xxxxxxxxx>
>> wrote:
>> > Two questions:
>> >
>> > 1) What getObjects(UserRole.class, exp) doing? Can you provide source
>> > code
>> > for this call?
>> > 2) Why are you doing getEntityManager().merge(urole)? Why not use
>> > entityManager variable and why you are merging entity before removing it
>> > do
>> > you expect it to be detached?
>> >
>> > Rodion
>> >
>> > On Wed, Jul 11, 2012 at 2:50 PM, vaidya nathan <vaidyaatdst@xxxxxxxxx>
>> > wrote:
>> >>
>> >> Sorry to repost but anyone having a solution for this problem .. ? I
>> >> am totally struck ..
>> >>
>> >> On Tue, Jul 10, 2012 at 1:05 PM, vaidya nathan <vaidyaatdst@xxxxxxxxx>
>> >> wrote:
>> >> > Greetings,
>> >> >
>> >> > I am having issues with delete on a table which connects two entities
>> >> > but otherwise is independant(No cascade,etc).  I keep getting
>> >> > Caused by: java.lang.IllegalArgumentException: Entity must be managed
>> >> > to call remove: UserRole@595, try merging the detached and try the
>> >> > remove again.
>> >> >
>> >> > Following is my model.I have a class UserRole which is defined as
>> >> > follows
>> >> >
>> >> > @Entity
>> >> > @Table(name="USER_ROLE",
>> >> > uniqueConstraints={@UniqueConstraint(columnNames={"USER_ID",
>> >> > "ROLE_ID"})})
>> >> > public class UserRole extends PersistableObject implements
>> >> > Effectivity<EffectivePeriodWithEnable>
>> >> > {
>> >> >     @OneToOne(fetch=FetchType.EAGER)
>> >> >     @JoinColumn(name="USER_ID", nullable=false)
>> >> >     private User user ;
>> >> >     @OneToOne(fetch=FetchType.EAGER)
>> >> >     @JoinColumn(name="ROLE_ID", nullable=false)
>> >> >     private Role role ;
>> >> >     @Column(name="DEFAULT_ROLE", nullable=false)
>> >> >     private Boolean defaultRole ;
>> >> >     @Embedded
>> >> >     private EffectivePeriodWithEnable span ;
>> >> > }
>> >> > which basically is a relation between User and Role with some
>> >> > attributes defined in the relation like the defaultRole and a span
>> >> > when the relation would be active/inactive.
>> >> >
>> >> >
>> >> > User is like this
>> >> > public class User extends PersistableObject
>> >> > {
>> >> >         @Column(name="LOGIN", nullable=false, unique=true, length=20)
>> >> >         private String loginName;
>> >> >     @Column(name="FIRST_NM", nullable=false, length=35)
>> >> >     private String firstName;
>> >> >     @Column(name="MID_NM", nullable=true, length=25)
>> >> >     private String middleName;
>> >> >      ...etc...etc(Not relevant to this discussion)
>> >> > }
>> >> >
>> >> > @Entity
>> >> > @Table(name="ROLE")
>> >> > public class Role extends PersistableObject
>> >> > {
>> >> >     @Column(name="NAME", nullable=false, unique=true, length=50)
>> >> >         private String name;
>> >> >
>> >> >     @Column(name="DESCRIPTION", nullable=true, length=255)
>> >> >         private String description;
>> >> > ....etc...etc(Not relevant to this discussion)
>> >> > }
>> >> >
>> >> >  I have a method defined in the DAO something like this
>> >> >
>> >> > public void setRoles(User user, List<Role> roles,
>> >> > EffectivePeriodWithEnable p) throws DAOException
>> >> >     {
>> >> >         try
>> >> >         {
>> >> >             ExpressionBuilder eb = new
>> >> > ExpressionBuilder(UserRole.class)
>> >> > ;
>> >> >             Expression exp = eb.get("user").equal(user) ;
>> >> >             List<UserRole> uroles = (List<UserRole>)
>> >> > getObjects(UserRole.class, exp);
>> >> >             EntityManager entityManager = getEntityManager() ;
>> >> >
>> >> >             if (uroles != null)
>> >> >             {
>> >> >                 for (UserRole urole: uroles)
>> >> >                 {
>> >> >                     // I have tried this with and without merge . I
>> >> > dont think that i should do a merge since the getobjects (which
>> >> > internally uses ReadAllQuery to fetch
>> >> >                     // the object is just got here in this method..
>> >> >
>> >> > getEntityManager().remove(getEntityManager().merge(urole));
>> >> >                 }
>> >> >              }
>> >> >             }
>> >> >         }
>> >> >
>> >> > I am running in a jta container and these methods are called within
>> >> > an
>> >> > ejb method call . When it runs and tries to remove , following is
>> >> > what
>> >> > i get
>> >> >
>> >> >
>> >> > Caused by: java.lang.IllegalArgumentException: Entity must be managed
>> >> > to call remove: com.abc.hps.vvv.security.bom.helpers.UserRole@595,
>> >> > try
>> >> > merging the detached and try the remove again.
>> >> >         at
>> >> >
>> >> > org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.performRemove(UnitOfWorkImpl.java:3559)
>> >> >         at
>> >> >
>> >> > org.eclipse.persistence.internal.jpa.EntityManagerImpl.remove(EntityManagerImpl.java:518)
>> >> >         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native
>> >> > Method)
>> >> >         at
>> >> >
>> >> > sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
>> >> >         at
>> >> >
>> >> > sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
>> >> >         at java.lang.reflect.Method.invoke(Method.java:597)
>> >> >         at
>> >> >
>> >> > org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365)
>> >> >         at $Proxy640.remove(Unknown Source)
>> >> >         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native
>> >> > Method)
>> >> >         at
>> >> >
>> >> > sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
>> >> >         at
>> >> >
>> >> > sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
>> >> >         at java.lang.reflect.Method.invoke(Method.java:597)
>> >> >         at
>> >> >
>> >> > org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
>> >> >         at $Proxy640.remove(Unknown Source)
>> >> >         at
>> >> >
>> >> > com.abc.hps.vvv.common.dao.eclipselink.EclipseLinkDAO.deleteObject(EclipseLinkDAO.java:179)
>> >> >         at
>> >> >
>> >> > com.abc.hps.vvv.dao.eclipselink.EclipseLinkSecurityDAO.setRoles(EclipseLinkSecurityDAO.java:212)
>> >> >         at
>> >> >
>> >> > com.abc.hps.vvv.security.impls.vvvSecurityImpl.setRoles(vvvSecurityImpl.java:401)
>> >> >         at
>> >> >
>> >> > com.abc.hps.vvv.security.impls.vvvSecurityImpl.setRoles(vvvSecurityImpl.java:412)
>> >> >         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native
>> >> > Method)
>> >> >         at
>> >> >
>> >> > sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
>> >> >         at
>> >> >
>> >> > sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
>> >> >         at java.lang.reflect.Method.invoke(Method.java:597)
>> >> >         at
>> >> >
>> >> > org.jboss.aop.joinpoint.MethodInvocation.invokeTarget(MethodInvocation.java:122)
>> >> >         at
>> >> >
>> >> > org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:111)
>> >> >         at
>> >> >
>> >> > org.jboss.ejb3.EJBContainerInvocationWrapper.invokeNext(EJBContainerInvocationWrapper.java:69)
>> >> >         at
>> >> >
>> >> > org.jboss.ejb3.interceptors.aop.InterceptorSequencer.invoke(InterceptorSequencer.java:73)
>> >> >         at
>> >> >
>> >> > org.jboss.ejb3.interceptors.aop.InterceptorSequencer.aroundInvoke(InterceptorSequencer.java:59)
>> >> >         at sun.reflect.GeneratedMethodAccessor369.invoke(Unknown
>> >> > Source)
>> >> >         at
>> >> >
>> >> > sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
>> >> >         at java.lang.reflect.Method.invoke(Method.java:597)
>> >> >         at
>> >> >
>> >> > org.jboss.aop.advice.PerJoinpointAdvice.invoke(PerJoinpointAdvice.java:174)
>> >> >         at
>> >> >
>> >> > org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
>> >> >         at
>> >> >
>> >> > org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor.fillMethod(InvocationContextInterceptor.java:72)
>> >> >         at
>> >> >
>> >> > org.jboss.aop.advice.org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor_z_fillMethod_1937287906.invoke(InvocationContextInterceptor_z_fillMethod_1937287906.java)
>> >> >         at
>> >> >
>> >> > org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
>> >> >         at
>> >> >
>> >> > org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor.setup(InvocationContextInterceptor.java:88)
>> >> >         at
>> >> >
>> >> > org.jboss.aop.advice.org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor_z_setup_1937287906.invoke(InvocationContextInterceptor_z_setup_1937287906.java)
>> >> >         at
>> >> >
>> >> > org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
>> >> >         at
>> >> >
>> >> > org.jboss.ejb3.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:62)
>> >> >         at
>> >> >
>> >> > org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
>> >> >         at
>> >> >
>> >> > org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:56)
>> >> >         at
>> >> >
>> >> > org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
>> >> >         at
>> >> >
>> >> > org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
>> >> >         at
>> >> >
>> >> > org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
>> >> >         at
>> >> > org.jboss.ejb3.tx.NullInterceptor.invoke(NullInterceptor.java:42)
>> >> >         at
>> >> >
>> >> > org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
>> >> >         at
>> >> >
>> >> > org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:68)
>> >> >         at
>> >> >
>> >> > org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
>> >> >         at
>> >> > org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:79)
>> >> >         ... 96 more
>> >> > 12:51:28,665 INFO  [STDOUT]:152 12:51:28,664 TRACE
>> >> > [ClassPathXmlApplicationContext]:301 Publishing event in
>> >> > ApplicationContext 'vvvWebBeanFactory':
>> >> >
>> >> >
>> >> > org.springframework.security.access.event.PublicInvocationEvent[source=FilterInvocation:
>> >> > URL: /abcrw/js/abc/assets/set2/delete_16.png]
>> >> >
>> >> > We are using eclipselink 2.4.0 and are running in a jboss 5.1.0.GA
>> >> > container.  Please help !!! I just dont understand why it is not able
>> >> > to remove .. May be i am missing something
>> >> >
>> >> > Cheers
>> >> > Vaidya
>> >> _______________________________________________
>> >> eclipselink-users mailing list
>> >> eclipselink-users@xxxxxxxxxxx
>> >> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
>> >
>> >
>> >
>> > _______________________________________________
>> > eclipselink-users mailing list
>> > eclipselink-users@xxxxxxxxxxx
>> > https://dev.eclipse.org/mailman/listinfo/eclipselink-users
>> >
>> _______________________________________________
>> eclipselink-users mailing list
>> eclipselink-users@xxxxxxxxxxx
>> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
>
>
>
> _______________________________________________
> eclipselink-users mailing list
> eclipselink-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
>


Back to the top