当前位置: 首页 > 知识库问答 >
问题:

org.hibernate.映射异常

薄涵衍
2023-03-14

我正在努力使用Java Spring Hibernate,我正在尝试实现Oauth2,在通过@ManyToMany将表用户连接到角色时,我不断遇到错误。我已经阅读了所有关于我的问题的答案,无论我尝试什么,我仍然得到了一个组织。冬眠映射异常。

以下是我正在努力做的事情的全部细节。

数据库结构

CREATE TABLE IF NOT EXISTS `role` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;

INSERT INTO `role` (`id`, `name`) VALUES
(1, 'ROLE_USER'),
(2, 'ROLE_ADMIN'),
(3, 'ROLE_GUEST');

-- --------------------------------------------------------

CREATE TABLE IF NOT EXISTS `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `email` varchar(255) NOT NULL,
  `nickname` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `user_email_uindex` (`email`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;


INSERT INTO `user` (`id`, `email`, `nickname`, `password`) VALUES
(1, 'test@test.com', 'Admin', 'test');

-- --------------------------------------------------------

--
-- Table structure for table `user_role`
--

DROP TABLE IF EXISTS `user_role`;
CREATE TABLE IF NOT EXISTS `user_role` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `email` varchar(255) NOT NULL,
  `role_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `email_fk` (`email`),
  KEY `role_fk` (`role_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;

--
-- Dumping data for table `user_role`
--

INSERT INTO `user_role` (`id`, `email`, `role_id`) VALUES
(1, 'test@test.com', 1),
(2, 'test@test.com', 2);

========================================

角色。JAVA

@Entity
public class Role implements GrantedAuthority {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @NotBlank
    private String name;

    @JsonIgnore
    @ManyToMany(mappedBy = "roles")
    private Set<User> users = new HashSet<>();

    @Override
    public String getAuthority() {
        return name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Set<User> getUsers() {
        return users;
    }

    public void setUsers(Set<User> users) {
        this.users = users;
    }
}

使用者JAVA

@Entity
@Table(name = "user")
public class User {
    private Integer id;
    private String email;
    private String password;
    private String nickname;

    @JsonIgnore
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "user_role",
            joinColumns = { @JoinColumn(name = "email") },
            inverseJoinColumns = { @JoinColumn(name = "role_id") })
    private Set<Role> roles = new HashSet<>();

    public User(User user) {
        super();
        this.id = user.getId();
        this.email = user.getEmail();
        this.password = user.getPassword();
        this.nickname = user.getNickname();
        this.roles = user.getRoles();
    }

    public User() {
    }

    @Id
    @Column(name = "id", nullable = false)
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Basic
    @Column(name = "email", nullable = false, length = 255)
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Basic
    @Column(name = "password", nullable = false, length = 255)
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Basic
    @Column(name = "nickname", nullable = false, length = 255)
    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public Set<Role> getRoles() {
        return roles;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User that = (User) o;
        return Objects.equals(id, that.id) &&
                Objects.equals(email, that.email) &&
                Objects.equals(password, that.password) &&
                Objects.equals(nickname, that.nickname);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, email, password, nickname);
    }
}

依赖性

**   <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jersey</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web-services</artifactId>
    </dependency>
    <dependency>
        <groupId>de.codecentric</groupId>
        <artifactId>spring-boot-admin-starter-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security.oauth</groupId>
        <artifactId>spring-security-oauth2</artifactId>
        <version>2.3.3.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.restdocs</groupId>
        <artifactId>spring-restdocs-mockmvc</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.2.3.Final</version>
    </dependency>
    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>persistence-api</artifactId>
        <version>1.0.2</version>
    </dependency>
</dependencies>**

问题:

org.springframework.beans.factory.BeanCreation异常:创建名为entityManagerFactory的bean时出错,该bean在类路径资源[org/spring框架/引导/autoconfiure/orm/jpa/HibernateJpaConfiguration.class]中定义:初始化方法调用失败;嵌套异常javax.persistence.持久性异常:[PeristenceUnit:默认]无法构建HiberNate SessionFactory;嵌套异常org.hibernate.MappingExctive:无法确定类型:java.util.设置,在表:用户,列:[org.hibernate.mapping.列(角色)]

共有2个答案

米夕
2023-03-14

尝试在@ElementCollection注释下面添加。

@ElementCollection(targetClass=Role.class)
@JsonIgnore
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "user_role",
            joinColumns = { @JoinColumn(name = "email") },
            inverseJoinColumns = { @JoinColumn(name = "role_id") })
    private Set<Role> roles = new HashSet<>();

注意:(targetClass=Role.class)是可选的。

孙项禹
2023-03-14

此问题与访问策略有关,在User Class中,访问策略由@Id注释(放在getter方法getId()上)确定。

因此,你应该把你的注释:

@JsonIgnore
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "user_role",
        joinColumns = { @JoinColumn(name = "email") },
        inverseJoinColumns = { @JoinColumn(name = "role_id") })

就在getter方法getRoles()上

 类似资料:
  • 为了处理运行Kotlin/SpringBoot应用程序时出现的以下错误(完整堆栈跟踪),我一直在咨询一些方法/帖子/堆栈溢出问题: 问题在于用Hibernate映射PostgreSQL的JSONB数据类型。 我广泛尝试和调试的两种方法如下: 实现自定义Hibernate映射并为JSONB创建自定义用户类型。参考文献:这里,这里,这里和这里 使用Hibernate类型。参考文献在这里,这里和这里 我

  • 我在启动Web服务器时遇到上述错误。我用 enum 映射和 enity 类。类似... 是什么问题,我在配置中遗漏了什么吗?谢谢!!

  • 首先,我对这个问题太长表示最诚挚的歉意,但老实说,我不知道如何缩短它,因为每个部分都是一个特例。诚然,我可能对此视而不见,因为我已经把头撞到墙上好几天了,我开始绝望了。 我向所有通读这本书的人表示最大的尊重和感谢。 我希望能够通过使用Jersey ExceptionMapers将Shiro的AuthenticationException及其子类映射到JAX-RS响应,Jersey例外映射器是使用G

  • 是否强制将我的外键实体从ClassA映射到ClassB中的主实体?

  • 我有一个映射到字符串值的特定键的映射列表。 类似于<代码>列表 目标:浏览此地图列表,并收集所有地图中单个键的值。 我是怎么做到的- 问题是:如果没有这样的密钥,我会因为a.get(key)而出现异常!因为求平均值会得到一个空值。如何检查或使lambda忽略任何此类地图并继续前进。 我知道我可以在

  • 我认为错误并不在注释中,因为我更改了几次注释,仍然得到了相同的异常。