2012-03-21 56 views
0

我一直在爲此奮鬥了很長一段時間,並沒有在google上得到任何具體答案。我有許多專輯和用戶之間的關係,爲此,連接表有一個額外的字段「角色」定義。具體實現:hibernate annotations:無法在類java.util.Set中找到屬性爲空的setter

User class : 

@Entity 
@Table(name="USERS") 

public class User { 

private long id; 
private String userName; 
private String password; 
private String mailAddress; 
private Set<AlbumUser> albumUsers; 

// Getters 
/** 
* @return the id 
*/ 
@Id 
@GeneratedValue(strategy=GenerationType.AUTO) 
public long getId() { 
    return id; 
} 

/** 
* @return the userName 
*/ 
public String getUserName() { 
    return userName; 
} 


/** 
* @return the password 
*/ 
@Type(type="encryptedString") 
public String getPassword() { 
    return password; 
} 


/** 
* @return the mailAddress 
*/ 
public String getMailAddress() { 
    return mailAddress; 
} 

// Setters 

/** 
* @param id the id to set 
*/ 
public void setId(long id) { 
    this.id = id; 
} 

/** 
* @param userName the userName to set 
*/ 
public void setUserName(String userName) { 
    this.userName = userName; 
} 

/** 
* @param password the password to set 
*/ 
public void setPassword(String password) { 
    this.password = password; 
} 

/** 
* @param mailAddress the mailAddress to set 
*/ 
public void setMailAddress(String mailAddress) { 
    this.mailAddress = mailAddress; 
} 

public String toString() 
{ 
    return ("User : " + getUserName()); 
} 

/** 
* @return the albumUsers 
*/ 
//----bidirectional association 
@Transient 
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.user") 
public Set<AlbumUser> getAlbumUsers() { 
    return albumUsers; 
} 

/** 
* @param albumUsers the albumUsers to set 
*/ 
public void setAlbumUsers(Set<AlbumUser> albumUsers) { 
    this.albumUsers = albumUsers; 
} 
} 

AlbumUser class : 

@Entity 
@Table(name = "ALBUMS_USERS") 
@AssociationOverrides({ 
@AssociationOverride(name = "pk.album", [email protected](name="album_id")), 
@AssociationOverride(name = "pk.user", [email protected](name="user_id"))}) 

public class AlbumUser { 

private AlbumUserPK pk = new AlbumUserPK(); 
private Role role; 

@EmbeddedId 
private AlbumUserPK getPk() 
{ 
    return pk; 
} 

private void setPk(AlbumUserPK pk) 
{ 
    this.pk = pk; 
} 

public void setAlbum(Album album) { 
    getPk().setAlbum(album); 
} 

@Transient 
public Album getAlbum() { 
    return getPk().getAlbum(); 
} 

public void setUser(User user) { 
    getPk().setUser(user); 
} 

@Transient 
public User getUser() { 
    return getPk().getUser(); 
} 

/** 
* @return the role 
*/ 
@Enumerated(EnumType.STRING) 
public Role getRole() { 
    return role; 
} 

/** 
* @param role 
*   the role to set 
*/ 
public void setRole(Role role) { 
    this.role = role; 
} 
} 

@Embeddable 
class AlbumUserPK implements Serializable { 

private Album album; 


private User user; 

/** 
* @return the album 
*/ 
@ManyToOne 
public Album getAlbum() { 
    return album; 
} 

/** 
* @param album 
*   the album to set 
*/ 
public void setAlbum(Album album) { 
    this.album = album; 
} 

/** 
* @return the user 
*/ 
@ManyToOne 
public User getUser() { 
    return user; 
} 

/** 
* @param user 
*   the user to set 
*/ 
public void setUser(User user) { 
    this.user = user; 
} 
} 

每當我嘗試插入/讀/ ......一個用戶,我得到以下錯誤:

org.hibernate.PropertyNotFoundException:找不到財產空二傳手class java.util.Set

我想這與一組AlbumUsers有關,我保存在User類中......我試圖讓這個字段成爲瞬態的,但無濟於事......

有沒有人遇到類似的錯誤?

謝謝...

回答

1

嘗試使用設置沒有泛型。

public void setAlbumUsers(Set albumUsers) { 
    this.albumUsers = albumUsers; 
} 

吸氣也

或者問題可在@Transient干將。

+0

謝謝你的建議,但都沒有奏效。我試圖遵循這個解決方案:http://stackoverflow.com/questions/5127129/mapping-many-to-many-association-table-with-extra-columns這似乎對我來說似乎合理。唯一想到的是,我有錯誤的關聯。大多數例子都會在保存實際用戶之前創建一個albumUser,這是我不想要的。用戶應該可以在沒有專輯的情況下注冊/登錄,因此沒有專輯用戶參考... – 2012-03-21 21:36:57

+0

其實,這是我使用的教程:http://www.mkyong.com/hibernate/hibernate-many-to-many-例如聯接表,柱外的註釋/ – 2012-03-21 21:47:19

相關問題