0

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
}
Software Engineer
  • 15,457
  • 7
  • 74
  • 102
Niklas S.
  • 1,046
  • 10
  • 22
  • What is an activation? Does a user have one activation? Does an activation belong only to a single user? – Software Engineer Jul 15 '14 at 21:58
  • An activation is basically just a temporary record which hold the activation code for completing the registration process. When a user activates his account with this code, the record is deleted. So, one User would have one UserActivation, which can be deleted after some time. – Niklas S. Jul 15 '14 at 22:02

1 Answers1

0

You can use the ManyToOne mapping :

@Entity
@Table(name = "activations")
public class UserActivation {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long activationId;
    @Column(length = 64)
    private String activationCode;

    @ManyToOne(optional=false)
    @JoinColumn(name="USER_NAME")
    private User user; // since your User Id is username, the join column will be username
}

if you want the username to be unique, wich means one activation for a single user :

@Entity
@Table(name = "activations",uniqueConstraints=@UniqueConstraint(columnsnames="USER_NAME"))
Rafik BELDI
  • 4,140
  • 4
  • 24
  • 38
  • Hi, thanks for your quick reply. Doesn't ManyToOne mean, that I have many Activations for one User? To be honest, I currently don't really understand the ...To... relations. – Niklas S. Jul 15 '14 at 22:01
  • I updated my answer , if you want a single user add a unique constraint, I prefer `ManyToOne`+ unique constraint over `OneToOne` cause it is easier to manage in both Uniderectional and bidirectional Mapping – Rafik BELDI Jul 15 '14 at 22:06
  • Thank you very much for your help, I will try if that works for me and then message back! Have a nice evening! – Niklas S. Jul 15 '14 at 22:33
  • and take a look at this answer for more details about the difference between oneToOne and ManyToOne : http://stackoverflow.com/questions/2452987/hibernate-why-use-many-to-one-to-represent-a-one-to-one – Rafik BELDI Jul 15 '14 at 23:15