EJB and JPA and @OneToMany - Transaction too long?
Posted
by marioErr
on Stack Overflow
See other posts from Stack Overflow
or by marioErr
Published on 2009-12-07T06:14:51Z
Indexed on
2010/03/13
10:05 UTC
Read the original article
Hit count: 285
Hello. 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;
© Stack Overflow or respective owner