0

我有以下实体。接触。人。组织。和电话(英文电话)。

个人和组织都是联系人。他们从联系人继承。

一个联系人可以有一个或多个电话号码。所以来自 Telefoon 的列表。

一个组织也有一个来自 person 类型的 contactPerson。

问题是,如果我坚持个人对象和组织对象,每个对象都有 2 个数字,那么对于某些对象,数据库中只有 1 个电话号码?那个怎么样 ?

我知道这与我的注释和我的类配置有关。

班级联系人

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

类人

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

班级组织:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Organisatie extends Contact implements Serializable{
    protected Organisatie() {}

    @JoinColumn(name = "persoonid")
    @ManyToOne(cascade=CascadeType.ALL)
    private Persoon contactPersoon;

班级电话(英文电话)

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

我以这种方式保留我的对象:

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

但是数据库不会显示所有家庭和手机号码!有些被排除在外!

有什么想法吗 ?

谢谢

4

1 回答 1

0

我找到了答案。Glassfish 在数据库中保存日期时遇到问题。

Person 类的日期属性由我自己编写的类转换。只是为了编辑数据库中日期的标记。

似乎 glassfish 无法处理。我还删除了带有 Organisatie(组织)列表的 oneToMany。

这:

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

现在是这样的:

@Entity
@XmlRootElement
public class Persoon extends Contact implements Serializable {


    @Temporal(DATE)
    private Date geboorteDatum;

    public Persoon() {
    }
}

现在一切正常!谢谢

于 2015-04-22T18:25:17.427 回答