tags:

views:

89

answers:

1

Hi everyone,

I am trying to use an @JoinColumn as an @Id using JPA and I am getting SerializationExceptions, "Could not serialize."

UserRole.java:

@Entity
@Table(name = "authorities")
public class UserRole implements Serializable {

 @Column(name = "authority")
 private String role;

 @Id
 @ManyToOne
 @JoinColumn(name = "username")
 private User owner;

        ...
}

User.java:

@Entity
@Table(name = "users")
public class User implements Serializable {

 @Id
 @GeneratedValue
 protected Long id;

 @Column(name = "username")
 protected String email;

 @OneToMany(mappedBy = "owner", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
 protected Set<UserRole> roles = new HashSet<UserRole>();

    ....

}

"username" is set up as a unique index in my Users table but not as the primary key.

Is there any way to make "username" act as the ID for UserRole? I don't want to introduce a numeric key in UserRole. Have I totally lost the plot here?

I am using MySQL and Hibernate under the hood.

A: 

That mapping doesn't really make sense. ID has to be unique, but ManyToOne says 'lots of these have the same User.'

Affe
You're right, of course. And I'm an idiot!
Shivago