I've got the following entities. Contact. Person. Organisation. and Telefoon (Phone in english).
An Person and an Organisation are both Contacts. they inherit from Contact.
An contact can have one or more phonenumbers. So an List from Telefoon.
An organisation also has an contactPerson from the type person.
The thing is if I persist Person objects and Organisation Objects, which all have 2 numbers per each object, that for some objects only 1 phoneNumber persists in the database? How is that ?
I know it has to do with my annotations and my configuration of my classes.
the class contact
@Entity
@Table(name= "Contact")
@Inheritance(strategy = InheritanceType.JOINED)
@XmlRootElement
public class Contact implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String naam;
@Embedded
private Adres adres;
public Contact() {}
@OneToMany(mappedBy="contact", orphanRemoval=true,cascade = CascadeType.ALL)
@XmlElement
private List<Telefoon> telefoons=new ArrayList<>();
The class Person
@Entity
@Table(name="Persoon")
@Inheritance(strategy= InheritanceType.JOINED)
public class Persoon extends Contact implements Serializable{
@Convert(converter=LocalDatePersistenceConverter.class)
private LocalDate geboorteDatum;
protected Persoon() {}
@OneToMany(mappedBy="contactPersoon",orphanRemoval=true,cascade = CascadeType.ALL)
private List<Organisatie> Organisaties;
The class Organisation:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Organisatie extends Contact implements Serializable{
protected Organisatie() {}
@JoinColumn(name = "persoonid")
@ManyToOne(cascade=CascadeType.ALL)
private Persoon contactPersoon;
class Telefoon (Phone in english)
@Entity
@Table(name = "Telefoon")
public class Telefoon implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "nummer")
private String nummer;
@Column(name = "naam")
private String naam;
@JoinColumn(name = "contactid")
@ManyToOne
private Contact contact;
I persist my objects in this way:
persoon1.addTelefoon(new Telefoon("Huis","092243851"));
persoon1.addTelefoon(new Telefoon("Mobiel","0478451226"));
persoon2.addTelefoon(new Telefoon("Huis","095547812"));
persoon2.addTelefoon(new Telefoon("Mobiel","0425154578"));
persoon3.addTelefoon(new Telefoon("Huis","097784152"));
persoon3.addTelefoon(new Telefoon("Mobiel","0478221144"));
persoon4.addTelefoon(new Telefoon("Huis","095862314"));
persoon4.addTelefoon(new Telefoon("Mobiel","0423887799"));
persoon5.addTelefoon(new Telefoon("Huis","097841526"));
persoon5.addTelefoon(new Telefoon("Mobiel","0478220033"));
contactpersoon1.addTelefoon(new Telefoon("Huis","092261236"));
contactpersoon1.addTelefoon(new Telefoon("Mobiel","0499150327"));
contactpersoon2.addTelefoon(new Telefoon("Huis","097842615"));
contactpersoon2.addTelefoon(new Telefoon("Mobiel","0499369101"));
contactpersoon3.addTelefoon(new Telefoon("Huis","091142563"));
contactpersoon3.addTelefoon(new Telefoon("Mobiel","0452119987"));
organisatie1.addTelefoon(new Telefoon("Huis","094578956"));
organisatie1.addTelefoon(new Telefoon("Mobiel","0488125200"));
organisatie2.addTelefoon(new Telefoon("Huis","091247653"));
organisatie2.addTelefoon(new Telefoon("Mobiel","0487930287"));
organisatie3.addTelefoon(new Telefoon("Huis","09784561346"));
organisatie3.addTelefoon(new Telefoon("Mobiel","0476112233"));
em.getTransaction().begin();
em.persist(persoon1);
em.persist(persoon2);
em.persist(persoon3);
em.persist(persoon4);
em.persist(persoon5);
em.persist(organisatie1);
em.persist(organisatie2);
em.persist(organisatie3);
em.merge(contactpersoon1);
em.merge(contactpersoon2);
em.merge(contactpersoon3);
em.getTransaction().commit();
BUT the database won't show all home and mobile numbers! Some are left out!
Any thoughts ?
Thanx
I found the answer. Glassfish is having problems with saving Dates in databases.
The date property of the class Person is converted By a class I have written my own. Just to edit the markup of the date in the database.
It seems glassfish could not handle that. I also removed the oneToMany with the list of Organisatie (organisation).
this:
@Entity
@Table(name="Persoon")
@Inheritance(strategy= InheritanceType.JOINED)
public class Persoon extends Contact implements Serializable{
@Convert(converter=LocalDatePersistenceConverter.class)
private LocalDate geboorteDatum;
protected Persoon() {}
@OneToMany(mappedBy="contactPersoon",orphanRemoval=true,cascade = CascadeType.ALL)
private List<Organisatie> Organisaties;
Is now this:
@Entity
@XmlRootElement
public class Persoon extends Contact implements Serializable {
@Temporal(DATE)
private Date geboorteDatum;
public Persoon() {
}
}
Everything works fine now! Thanx