entitymanager不会保留所有手机进行联系

时间:2015-04-19 00:57:51

标签: hibernate jpa javadb hibernate-entitymanager

我有以下实体。 联系。人。组织。和Telefoon(英文电话)。

人员和组织都是联系人。他们继承自联系。

联系人可以有一个或多个语音。所以来自Telefoon的名单。

组织也有类型人员的联系人。

问题是如果我持久化Person对象和组织对象, 每个对象都有2个数字,对于某些对象,只有1个phoneNumber在数据库中存在?那怎么样?

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

班主任

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

但数据库不会显示所有家庭和手机号码! 有些人被遗漏了!

有什么想法吗?

感谢名单

1 个答案:

答案 0 :(得分:0)

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

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

似乎玻璃鱼无法处理。 我还使用Organisatie(组织)列表删除了oneToMany。

这样:

YearMonth ym = YearMonth.of(2000, 2);
System.out.println(ym.lengthOfMonth()); //29 as expected

现在是这样的:

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

现在一切正常! 感谢名单