简体   繁体   English

休眠映射不起作用

[英]Hibernate mapping not working

Trying to implement Spring Security and cannot map roles and users. 尝试实现Spring Security,并且无法映射角色和用户。 So I have "users", "roles" and "user_role_connector" database tables: 所以我有“用户”,“角色”和“ user_role_connector”数据库表:

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. 出现的错误是bean从按字母顺序的第一个控制器创建错误,因此它必须是错误的映射。

And is there any good Spring Security tutorial out there? 那里有什么好的Spring Security教程吗?

EDIT: Forgot to write that the classes work untill the "role" field to userAccount and userRoles to role is added. 编辑:忘了写这些类工作,直到添加到userAccount的“角色”字段和添加到角色的userRoles为止。 And only the code with error is displayd here. 并且仅在此处显示有错误的代码。 Sry for bad english. 不好意思英语。

I think, there is a contradiction between UserAccount and Role classes. 我认为UserAccount和Role类之间存在矛盾。 UserAccount describes user_role_connector table as OneToOne, yet Role describes the same relationship as OneToMany. UserAccount将user_role_connector表描述为OneToOne,而Role描述与OneToMany相同的关系。

Error was caused by annotations placement. 错误是由注释放置引起的。 Here I used it on field while else was used on get method. 在这里,我在现场使用了它,而其他则在get方法上使用了。 Must follow one rule! 必须遵循一条规则! Smarter once again! 再聪明一点! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM