Hi Michal,
This is your code snippet,
@Entity public class Contact implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; private String surname; @OneToMany(cascade = CascadeType.REMOVE, mappedBy = "contact") private Collection<PhoneNumber> phoneNumbers = new ArrayList<PhoneNumber>();take it from
http://stackoverflow.com/questions/1858153/ejb-and-jpa-and-onetomany-transaction-too-longI should make the following,@OneToMany(cascade = CascadeType.REMOVE, mappedBy = "contact", targetEntity=PhoneNumber.class, fetch=FetchType.EAGER) Try this and let me know if it does work better. Regards,Jose
From: Michal Kovác
Sent: Friday, December 11, 2009 12:34 AM
To: ejb_at_glassfish.dev.java.net
Subject: Transactions taking too long in @OneToMany relation
Hello,
I'm trying to do simple Java EE application, just to get a hold of EJB3 and I'm stuck for quite a few days. I've posted my problem on stackoverflow:
http://stackoverflow.com/questions/1858153/ejb-and-jpa-and-onetomany-transaction-too-long
One behavior I haven't mentioned in that post is this: when I create and persist a Contact object and want to add a PhoneNumber object to it, it takes long time (which varies) to take effect. When I try to remove that Contact object before the PhoneNumber is returned (it is in DB, it just can't be returned via EntityManager's find method immediately), I get transaction.RollbackException (DELETE on table 'CONTACT' caused a violation of foreign key constraint 'PHNENUMBERCNTACTID' for key (2401). The statement has been rolled back) - even thought removing works well on the same object a little later, when PhoneNumber object is found.
I tried to set fetch method to eager, that didn't work. When I try to run em.refresh right after I added the phoneNumber with em.create, it's saying entity is not managed.
If you need additional information or source code, please let me know.
Thank you