Skip to content

  • Home
  • Blog
  • Projetos
  • Contato

Contornando o temido [org.hibernate.mapping.Column(authorities)]

maio 31, 2020 by jhonatan2760

Go to top

Algo muito comum quando estamos configurando o Spring Security em nossos projetos é poder recebera temida mensagem de: org.hibernate.MappingException: Could not determine type for: java.util.Collection,geralmente ela aparece na etapa em que começamos a manipular nossa classe de Usuário do domínio, quandoimplementados a interface UserDetails o método getAuthorities necesita ser mapeado com a cardinalidade da sua escolha, porém mapear diretamente no método get pode confundir a implementação da JPA, no nosso caso o Hibernate, sendo assim, a solução é mapear a classe de domínio do usuário e a de perfil nos atributos, segue o exemplo:

 

package com.model;

import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


@Entity
public class UserEntity implements UserDetails {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String cpf;
    @Column(length = 10)
    private String password;
    @ManyToOne(cascade = CascadeType.ALL)
    private UserPhotoEntity userPhotoEntity;
    @CreationTimestamp
    private LocalDateTime createdAt;
    @UpdateTimestamp
    private LocalDateTime updatedAt;
    @ManyToMany(fetch = FetchType.EAGER)
    private List profileList = new ArrayList<>();


    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getCpf() {
        return cpf;
    }

    public void setCpf(String cpf) {
        this.cpf = cpf;
    }



    public LocalDateTime getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }


    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }


    public UserPhotoEntity getUserPhotoEntity() {
        return userPhotoEntity;
    }

    public void setUserPhotoEntity(UserPhotoEntity userPhotoEntity) {
        this.userPhotoEntity = userPhotoEntity;
    }


    @Override
    public Collection getAuthorities() {
        return profileList;
    }

    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return null;
    }

    @Override
    public boolean isAccountNonExpired() {
        return false;
    }

    @Override
    public boolean isAccountNonLocked() {
        return false;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return false;
    }

    @Override
    public boolean isEnabled() {
        return false;
    }


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


}
Go to top
package com.model;

import org.springframework.security.core.GrantedAuthority;

import javax.persistence.*;

@Entity
public class Profile implements GrantedAuthority {


    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;

    public String getName() {
        return name;
    }

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


    public Long getId() {
        return id;
    }

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

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

Anotando apenas pelos atributos em ambos resolverá o problema, até a próxima!

 

Post Views: 1.534

Post navigation

Previous Post:

[Meetup] Ame Digital + SouJava

Next Post:

Programação Reativa com Spring Boot, WebFlux e DynamoDB

Posts recentes

  • Como explorar seu novo componente no Arduino
  • CORS em uma aplicação Angular 10 e Spring Security
  • Como gerar um certificado SSL grátis para sua aplicação em Spring
  • Arduino vs Dinossauro do Google Chrome
  • Comprei uma impressora 3D

Arquivos

  • abril 2022
  • novembro 2021
  • dezembro 2020
  • junho 2020
  • maio 2020
  • março 2020
  • dezembro 2019
  • novembro 2019
  • agosto 2019
  • junho 2019
  • março 2019
  • novembro 2018
  • agosto 2018
  • julho 2018
  • março 2018
  • fevereiro 2018
  • janeiro 2018
  • dezembro 2017
  • novembro 2017
  • outubro 2017
  • agosto 2017
  • abril 2017
  • novembro 2016
  • junho 2015
  • fevereiro 2015
  • janeiro 2015
  • novembro 2014
  • setembro 2014
  • agosto 2014
  • julho 2014
  • junho 2014
  • maio 2014
  • abril 2014
  • março 2014
  • fevereiro 2014
  • janeiro 2014
  • dezembro 2013
  • novembro 2013
  • outubro 2013

Categorias

  • 3D printter
  • ALREADY
  • Ame Digital
  • ANGULAR 4+ ROLE BASED
  • ANGULAR 5
  • ANGULAR ROLE BASED
  • Arduino
  • Consulta Operadora
  • Cors
  • Crawler
  • Data Science
  • DIRECTIVE ANGULAR
  • español
  • HTTPS
  • Impressora 3D
  • iOS
  • iOT
  • JAVA
  • JERSEY
  • Jsoup
  • Meetup
  • Pandas
  • Python
  • R
  • REQUEST JERSEY
  • scraping
  • SECURITY CONTEXT
  • Sem categoria
  • Spanish
  • Spring
  • Spring Security
  • Spring Webflux
  • Swift
  • Talkings
  • Technology
  • Uncategorized
  • Webflux
2021 - Jhonatan S. Souza