hibernate annotations:在类java.util.Set中找不到属性为空的setter

时间:2012-03-21 20:26:47

标签: hibernate

我已经在这方面苦苦挣扎了一段时间,并没有在谷歌得到任何具体的答案。我在专辑和用户之间有很多关系,为此连接表定义了一个额外的字段“角色”。具体实施:

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", joinColumns=@JoinColumn(name="album_id")),
@AssociationOverride(name = "pk.user", joinColumns=@JoinColumn(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:找不到类java.util.Set中属性为空的setter

我想这与AlbumUsers的集合有关,我保留在User类中......我试图使这个字段瞬态,但无济于事......

有人遇到过类似的错误吗?

...谢谢

1 个答案:

答案 0 :(得分:1)

尝试使用没有泛型的Set。

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

getter

或问题可能出在@Transient getters中。