You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by JuneG <pg...@wernervas.com> on 2007/12/04 23:24:02 UTC

Casade Persist

we have an entity has one-to-many relationship to several other entities, we
use EntityManager's persist method to do cascade persist. Occasionally one
of the entities in this graph did not get persisted while all the others
were persisted successfully, and there is no exception raised why the entity
did not get persisted. If we re run the process all entities will be
persisted successfully. any idea why this is happening? is that all entities
should be persisted in one transaction? we are using weblogic 10
any input will be appreciated. thanks!! 
-- 
View this message in context: http://www.nabble.com/Casade-Persist-tf4946103.html#a14160955
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Re: Casade Persist

Posted by Craig L Russell <Cr...@Sun.COM>.
Hi JuneG,

There's nothing obviously wrong in your example. Can you run with SQL  
tracing on and see if there is a difference between when it works and  
when it doesn't?

Craig

On Dec 4, 2007, at 3:00 PM, JuneG wrote:

>
>
>
> Craig L Russell wrote:
>>
>> Hi,
>>
>> I can't tell from your description what might be going wrong, but
>> there is one clue. You ask if all entities should be persisted in one
>> transaction. Not necessarily. But if you have several transactions,
>> it's possible that you are using detached instances and the problem
>> might be that if you assign an instance to a field of a detached
>> instance and then commit the transaction, nothing happens!
>>
>> You need to make sure you merge all detached instances or nothing is
>> supposed to happen.
>>
>> Can you attach a test case?
>>
>> Craig
>>
>>
>
> Thanks for your reply!
>
> here is a simple version of what we are doing.  we are creating all  
> new
> objects and then persist them in one transaction by a session bean.
> occasionally A&C got stored without B.  if we re run the process  
> all A, B
> and C will be saved.  what might be wrong?
>
> @Entity
> public class ClassA {
>
> 	//fields definition
>
> 	@OneToMany(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER,  
> mappedBy
> = "classA")
> 	private Collection<ClassB> classBs = new HashSet<ClassB>();
>
> 	@OneToMany(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER,  
> mappedBy
> = "classA)
> 	private Collection<classC> classCs = new HashSet<ClassC>();
> }
>
> @Entity
> public class ClassB{
>
> 	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
> 	@JoinColumn(name = "class_a_id")
> 	private ClassA classA;
> 	
> 	//fields defintions
> }
>
> @Entity
> public class ClassC{
>
> 	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
> 	@JoinColumn(name = "class_a_id")
> 	private ClassA classA;
> 	
> 	//fields defintions
> }
>
>
> //by default all the methods in this session bean has required tx  
> attribute
> @Stateless
> public class SessionBean{
>
> 	@PersistenceContext(unitName = "test")
> 	private EntityManager em;
>
> 	public buildA{
> 		ClassA a = new ClassA();
> 		
> 		ClassB b = new ClassB();
> 		Collection<ClassB> classBs = new ArrayList<ClassB>();
> 		b.setClassA(a);
> 		classBs.add(a);
> 		a.setClassBs(classBs);
> 		
> 		ClassC c = new ClassC();
> 		Collection<ClassC> classCs = new ArrayList<ClassC>();
> 		c.setClassA(a);
> 		classCs.add(a);
> 		a.setClassCs(classCs);
> 		
> 		em.persist(a);
> 	}
> }
>
> -- 
> View this message in context: http://www.nabble.com/Casade-Persist- 
> tf4946103.html#a14161450
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>

Craig Russell
Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
408 276-5638 mailto:Craig.Russell@sun.com
P.S. A good JDO? O, Gasp!


Re: Casade Persist

Posted by JuneG <pg...@wernervas.com>.


Craig L Russell wrote:
> 
> Hi,
> 
> I can't tell from your description what might be going wrong, but  
> there is one clue. You ask if all entities should be persisted in one  
> transaction. Not necessarily. But if you have several transactions,  
> it's possible that you are using detached instances and the problem  
> might be that if you assign an instance to a field of a detached  
> instance and then commit the transaction, nothing happens!
> 
> You need to make sure you merge all detached instances or nothing is  
> supposed to happen.
> 
> Can you attach a test case?
> 
> Craig
> 
> 

Thanks for your reply!

here is a simple version of what we are doing.  we are creating all new
objects and then persist them in one transaction by a session bean. 
occasionally A&C got stored without B.  if we re run the process all A, B
and C will be saved.  what might be wrong?

@Entity
public class ClassA {

	//fields definition

	@OneToMany(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER, mappedBy
= "classA")
	private Collection<ClassB> classBs = new HashSet<ClassB>();

	@OneToMany(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER, mappedBy
= "classA)
	private Collection<classC> classCs = new HashSet<ClassC>();
}

@Entity
public class ClassB{

	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
	@JoinColumn(name = "class_a_id")
	private ClassA classA;
	
	//fields defintions
}

@Entity
public class ClassC{

	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
	@JoinColumn(name = "class_a_id")
	private ClassA classA;
	
	//fields defintions
}


//by default all the methods in this session bean has required tx attribute
@Stateless
public class SessionBean{

	@PersistenceContext(unitName = "test")
	private EntityManager em;

	public buildA{
		ClassA a = new ClassA();
		
		ClassB b = new ClassB();
		Collection<ClassB> classBs = new ArrayList<ClassB>();
		b.setClassA(a);
		classBs.add(a);
		a.setClassBs(classBs);
		
		ClassC c = new ClassC();
		Collection<ClassC> classCs = new ArrayList<ClassC>();
		c.setClassA(a);
		classCs.add(a);
		a.setClassCs(classCs);
		
		em.persist(a);
	}
}

-- 
View this message in context: http://www.nabble.com/Casade-Persist-tf4946103.html#a14161450
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Re: Casade Persist

Posted by Craig L Russell <Cr...@Sun.COM>.
Hi,

I can't tell from your description what might be going wrong, but  
there is one clue. You ask if all entities should be persisted in one  
transaction. Not necessarily. But if you have several transactions,  
it's possible that you are using detached instances and the problem  
might be that if you assign an instance to a field of a detached  
instance and then commit the transaction, nothing happens!

You need to make sure you merge all detached instances or nothing is  
supposed to happen.

Can you attach a test case?

Craig

On Dec 4, 2007, at 2:24 PM, JuneG wrote:

>
> we have an entity has one-to-many relationship to several other  
> entities, we
> use EntityManager's persist method to do cascade persist.  
> Occasionally one
> of the entities in this graph did not get persisted while all the  
> others
> were persisted successfully, and there is no exception raised why  
> the entity
> did not get persisted. If we re run the process all entities will be
> persisted successfully. any idea why this is happening? is that all  
> entities
> should be persisted in one transaction? we are using weblogic 10
> any input will be appreciated. thanks!!
> -- 
> View this message in context: http://www.nabble.com/Casade-Persist- 
> tf4946103.html#a14160955
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>

Craig Russell
Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
408 276-5638 mailto:Craig.Russell@sun.com
P.S. A good JDO? O, Gasp!