【问题标题】:JPA annotations for entity mapping用于实体映射的 JPA 注释
【发布时间】:2014-11-25 04:38:27
【问题描述】:

我有两个实体 User 和 Contact 。用户对象包含联系人。如何使用 JPA 注释 这样当我保存用户对象时,联系人表应该有用户的 id 作为它的主要键?

            Public class User{
            public String username;
            public long id;
            public Contact contact;

            }

            public class Contact {
            public long id;
            public string phone;
            public string email;
            }

【问题讨论】:

标签: jpa-2.0


【解决方案1】:

类似的东西

@Entity  
public class User{
    @Column
    public String username;
    @Id
    public long id;
    @OneToMany(mappedBy="id")
    public Contact contact;

}

@Entity 
public class Contact {
    @Id
    public long id;
    @Column
    public string phone;
    @Column
    public string email;
}

【讨论】:

  • 那行不通。我认为我们必须使用一些@PrimaryKeyJoinColumn ,如此处所示(techferry.com/articles/hibernate-jpa-annotations.html#OneToOne)。 ..但是在我实现这种方式之后,我的联系人 ID 与用户 ID 不同。
  • 这只是给你一个想法,它不是完整的解决方案。
  • 我需要 OnetoOne 映射,其中用户 ID 作为外键存在于联系人表中。
猜你喜欢
  • 2017-02-11
  • 1970-01-01
  • 2011-07-13
  • 1970-01-01
  • 2015-08-16
  • 1970-01-01
  • 2015-10-03
  • 2016-02-14
  • 2023-03-22
相关资源
最近更新 更多