JPA - Can an @JoinColumn be an @Id as well? SerializationException occurs.

Posted by Shivago on Stack Overflow See other posts from Stack Overflow or by Shivago
Published on 2010-05-13T14:13:32Z Indexed on 2010/05/13 14:14 UTC
Read the original article Hit count: 169

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about java

Related posts about jpa