实体管理器:同时使用主键和外键

时间:2014-12-10 09:40:29

标签: java jpa entitymanager

我遇到麻烦,我无法创建实体,

用户(ID,用户名,姓氏,...,PWD)

联系人(idUser,idContact)

我想创建实体Contact,其中包含用户和联系人作为主键和外键
参考user.id,用户和联系人都是id的用户

这是我的实体联系方式:

@Entity
@Table(name="contact")
public class Contact {
     
    @Id
    @ManyToOne
    @JoinColumn(name="user-fk")
    private User idUser;
     
    @Id
    @ManyToOne
    @JoinColumn(name="contact-fk")
    private User idContact;
 
    public User getIdUser() {
        return idUser;
    }
 
    public void setIdUser(User idUser) {
        this.idUser = idUser;
    }
 
    public User getIdContact() {
        return idContact;
    }
 
    public void setIdContact(User idContact) {
        this.idContact = idContact;
    }

这是我的实体用户:

@Entity
@Table(name = "user")
public class User {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
private String userName;
private String firstName;
private String lastName;
private String email;
private String password;

@OneToMany(mappedBy="emet")
private List<Message> messageEmet;

@OneToMany(mappedBy="recept")
private List<Message> messageRecept;

@OneToMany(mappedBy="idUser")
private List<Contact> idUser;

@OneToMany(mappedBy="idContact")
private List<Contact> idContact;

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public List<Message> getMessageEmet() {
    return messageEmet;
}

public void setMessageEmet(List<Message> messageEmet) {
    this.messageEmet = messageEmet;
}

public List<Message> getMessageRecept() {
    return messageRecept;
}

public void setMessageRecept(List<Message> messageRecept) {
    this.messageRecept = messageRecept;
}

public List<Contact> getIdUser() {
    return idUser;
}

public void setIdUser(List<Contact> idUser) {
    this.idUser = idUser;
}

public List<Contact> getIdContact() {
    return idContact;
}

public void setIdContact(List<Contact> idContact) {
    this.idContact = idContact;
}

我真的需要帮助,我不能在没有表格的情况下制作我的页面

0 个答案:

没有答案