0

Trying to implement Spring Security and cannot map roles and users. So I have "users", "roles" and "user_role_connector" database tables:

CREATE TABLE users (
   user_pk INT NOT NULL AUTO_INCREMENT,
   ...
PRIMARY KEY (user_pk));

CREATE TABLE roles (
   role_pk INT NOT NULL AUTO_INCREMENT,  
   role VARCHAR(20) NOT NULL UNIQUE,  
PRIMARY KEY (role_pk)); 

CREATE TABLE user_role_connector (
   urc_pk INT NOT NULL AUTO_INCREMENT,
   user_fk INT NOT NULL,  
   role_fk INT NOT NULL,  
FOREIGN KEY (user_fk) REFERENCES users(user_pk),
FOREIGN KEY (role_fk) REFERENCES roles(role_pk),
PRIMARY KEY (urc_pk));

And I have Entity classes for Role and User. Both work until I add in mapping:

@Entity
@Table(name = "users")
public final class UserAccount {

    private int user_pk;
    ...

    @OneToOne(cascade=CascadeType.ALL)   
    @JoinTable(name = "user_role_connector",  
        joinColumns        = {@JoinColumn(name = "user_fk", referencedColumnName = "user_pk")},  
        inverseJoinColumns = {@JoinColumn(name = "role_fk", referencedColumnName = "role_pk")}  
    )  
    private Role role;

@Entity
@Table(name = "roles")
final public class Role {

    private int    role_pk;
    private String role;

    @OneToMany(cascade=CascadeType.ALL)  
    @JoinTable(name="user_role_connector",   
        joinColumns        = {@JoinColumn(name="role_fk", referencedColumnName="role_pk")},  
        inverseJoinColumns = {@JoinColumn(name="user_fk", referencedColumnName="user_pk")}  
    )  
    private Set<UserAccount> userRoles;

Why wont it map? Errors that come are bean creating errors from the alphabetically first controller so it must be bad mapping.

And is there any good Spring Security tutorial out there?

EDIT: Forgot to write that the classes work untill the "role" field to userAccount and userRoles to role is added. And only the code with error is displayd here. Sry for bad english.

2 Answers 2

1

I think, there is a contradiction between UserAccount and Role classes. UserAccount describes user_role_connector table as OneToOne, yet Role describes the same relationship as OneToMany.

Sign up to request clarification or add additional context in comments.

3 Comments

Yep, we wouldn't really expect to see an association "join table" for a one-to-one association.. "User.Roles" should really be a plurality. This may be the source of your error.
Well it's rare, but not uncommon, to do OneToOne using join table, see here for example. But that's not the case here, either UserAcount should be ManyToOne or Role OneToOne
Discovered the error cause to be bad annotation placement. This might also be broken, but will come out later on. Atm continual errors are on different topic. Thank you for your help!
1

Error was caused by annotations placement. Here I used it on field while else was used on get method. Must follow one rule! Smarter once again! :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.