0

I've been struggling with this for quite some time, and didn't get any concrete answers on google. I have many to many relationship between albums and users, for which the join table has an extra field "role" defined. The concrete implementation :

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;
}
}

Whenever I try to insert/read/... a user, I get the following error :

org.hibernate.PropertyNotFoundException: Could not find a setter for property empty in class java.util.Set

I guess this has something to do with the set of AlbumUsers,I'm keeping in the User class... I tried to make this field transient, but to no avail...

Has anybody come across similar errors?

Thanks...

wanderer_13
  • 1
  • 1
  • 1

1 Answers1

1

Try to use Set without generics.

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

getter also

Or problem can be in @Transient getters.

Ilya
  • 29,135
  • 19
  • 110
  • 158
  • Thank you for the suggestions, but neither worked. I tried to follow this solution : http://stackoverflow.com/questions/5127129/mapping-many-to-many-association-table-with-extra-columns which seemed plausible to me. The only thing that comes to mind is, that I have the wrong association. Most examples would create an albumUser before saving the actual user, which I do not want. Users should be able to register/login without having an album, hence no albumUser references... – wanderer_13 Mar 21 '12 at 21:36
  • Actually, this was the tutorial I used : http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/ – wanderer_13 Mar 21 '12 at 21:47