2011-08-24 135 views
0

如果您知道更好的标题,请更改标题,因为我真的不知道如何表达问题。JPA坚持PK对象(ManyToMany)

我有三类:

@Entity 
public class User { 

@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
@Column(name = "id") 
private Integer id; 

@NotNull 
@Size(min = 1, max = 45) 
@Column(name = "name") 
private String name; 

@JoinTable(name = "usuer_has_contact", joinColumns = { 
    @JoinColumn(name = "user_id", referencedColumnName = "id")}, inverseJoinColumns = { 
    @JoinColumn(name = "contact_id", referencedColumnName = "id")}) 
@ManyToMany(cascade = CascadeType.ALL) 
private List<Contato> contactList; 

//Getters and Setters 

} 

DB Table: 
Table Name: User 
Columns: id (int pk), name (varchar(45) not null). 

@Entity 
private class Contact { 

@EmbeddedId 
protected UserHasContact userHasContact; 

@NotNull 
@Size(min = 1, max = 45) 
@Column(name = "value") 
private String value; 

@ManyToMany(mappedBy = "contactList") 
private List<User> userList; 

//Getters and Setters 

} 

DB Table: 
Table Name: Contact 
Columns: id (int pk), value (varchar(45) not null). 

@Embeddable 
private class UserHasContact { 

@NotNull 
@Column(name = "id") 
private Integer id; 

//Getters and Setters 

} 

DB Table: 
Table Name: UserHasContact 
Columns: userId (int pk), contactId (int pk). 

我想要做的是,当我坚持用户本身坚持的一切。例如:

User user = new User(); 
user.setContactList(new ArrayList<Contact>()); 

Contact contact = new Contact(); 
contact.setValue("555-5555"); 

user.getContactList().add(contact); 

// Here I'd call another class, passing User so it would only do a 
// entityManager.persist(user), and it would persist it all and 
// take care of all tables for me. What I don't want to do is to 
// fill up the values myself, I want let JPA do it for me. 

我希望这样做后保存,但它说的ContactID为null,它不能为空。 我能做什么?

回答

1

为什么你创建一个可嵌入UserHasContact类来存储单个Integer?你让事情变得更加困难。只需使用Integer ID作为联系人主键。但这不是你问题的原因。

您正试图将包含联系人的用户保留在其联系人列表中。您的联系人ID不是自动生成的,您没有为此联系人ID分配任何ID。 JPA如何将这个联系人保存在数据库中?而且,你没有坚持接触,所以它是暂时的。

您必须

  • 要么分配一个ID tothe联系,或标注其ID,以便它自动生成
  • 持续的接触以及用户

下面是代码联系实体:

@Entity 
private class Contact { 
@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) // this makes the ID auto-generated 
@Column(name = "id") 
private Integer id; 

@NotNull 
@Size(min = 1, max = 45) 
@Column(name = "value") 
private String value; 

@ManyToMany(mappedBy = "contactList") 
private List<User> userList; 

//Getters and Setters 
} 

而在用户和联系人的代码创建:是我的IDE产生

User user = new User(); 
user.setContactList(new ArrayList<Contact>()); 

entityManager.persist(user); 

Contact contact = new Contact(); 
contact.setValue("555-5555"); 

entityManager.persist(contact); 

user.getContactList().add(contact); 
// you should also make sure that the object graph is consistent, so 
// the following line should lso be added, (though not strictly necessary) 
contact.getUserList().add(user); 
+0

UserHasContact,这就是为什么我试图使用它像这样。还有另一个名为Place的对象,它访问相同的对象联系人,并具有对象PlaceHasContact – pringlesinn

+1

IDE随后生成糟糕的代码。它是哪个IDE? –

+0

NetBeans 7.0.1 ...那么应该如何处理注释或类呢?我是新的,有点还是输了 – pringlesinn