I'm currently taking a look into Hibernate (with Spring, if important) and I tried to make a small web application in which someone can register and log in. There is a secured area which is protected by Spring Security and I already got my UserDetailsService
working, so Spring Security is using the Hibernate stuff to do the login.
Now I am working on the registration for new users. I thought about how to do it and I came to having a separate table for the activations which would basically have 3 columns: activationId
, username
and activation_code
. The activation code would be some kind of hash but I guess its not relevant for this question.
My question basically is, how to do the relationship between my users
table and the activations
. I need to link the username
from the activations
to the username
from the users
table.
Do I have to use a One To One Mapping? Or how can I link exactly one column from another table into the activation table?
Here's my User
Entity:
@Entity
@Table(name = "users")
public class User {
@Id
@Column(name = "username", unique = true, nullable = false, length = 45)
private String username;
@Column(name = "password", nullable = false, length = 64)
private String password;
@Column(name = "enabled", nullable = false)
private boolean enabled;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
private Set<UserRole> userRole = new HashSet<UserRole>(0);
@Column(name = "name")
private String name;
@Column(name = "lastname")
private String lastname;
public User() {
}
public User(String username, String password, boolean enabled) {
this.username = username;
this.password = password;
this.enabled = enabled;
}
public User(String username, String password, boolean enabled, Set<UserRole> userRole) {
this.username = username;
this.password = password;
this.enabled = enabled;
this.userRole = userRole;
}
// Getters and setters for all attributes omitted
}
If you need the UserRole
Entity too, just tell me and I will add it but I think it's not needed. Here is also the current version of my UserActivation
Entity, but of course it is not finished yet:
import javax.persistence.*;
@Entity
@Table(name = "activations")
public class UserActivation {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long activationId;
@Column(length = 64)
private String activationCode;
@Column
private String userName; // here we need some magic relationship
}