users@glassfish.java.net

Transactions taking too long in _at_OneToMany relation

From: <glassfish_at_javadesktop.org>
Date: Fri, 11 Dec 2009 07:36:11 PST

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'm using EJB and JPA, and when I try to access `PhoneNumber` objects in `phoneNumbers` attribute of `Contact contact`, it sometimes take several minutes for it to actually return data. It just returns no phoneNumbers, not even null, and then, after some time, when i call it again, it magically appears.

This is how I access data:

    for (Contact c : contactFacade.findAll()) {
        System.out.print(c.getName()+" "+c.getSurname()+" : ");
        for (PhoneNumber pn : c.getPhoneNumbers()) {
            System.out.print(pn.getNumber()+" ("+pn.getDescription()+"); ");
        }
    }

I'm using facade session ejb generated by netbeans (basic CRUD methods).
It always prints correct name and surname, phonenumbers and description are only printed after some time (it varies) from creating it via facade. I'm guessing it has something to do with transactions. How to solve this?

These are my JPA entities:

contact

    @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>();

phonenumber

    @Entity
    public class PhoneNumber implements Serializable {
        private static final long serialVersionUID = 1L;
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;
        private String number;
        private String description;
        @ManyToOne()
        @JoinColumn(name="CONTACT_ID")
        private Contact contact;

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.

I'm using GlassFish 2.1, persistence provider TopLink and Apache Derby.

If you need additional information or source code, please let me know.
[Message sent by forum member 'mkcm' ]

http://forums.java.net/jive/thread.jspa?messageID=375985