2011-08-24 274 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