I need to Associate two entities in @Manytomany with JPA using Mysql, but it is not saved in the composite table (usuario_item)

Asked

Viewed 37 times

0

I need to make the connection between user and item for a login system

I’m stagnant in this error 2 days ago. I searched for forums and tutorials and could not get anything to help me.
If possible give me tips on how to do more efficient research on, I come here when I see no other option.

> WARN:

  • 2020-10-29 12:36:13.074 WARN 38324 --- [ main] Configservletwebserverapplicationcontext : Exception encountered During context initialization - cancelling refresh Attempt: org.springframework.Beans.factory.Beancreationexception: Error creating bean with name 'usuarioRepository' defined in com.fcc.ads.turmad.nunesdaniel.appweb.repositories.UsuarioRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Cannot resolve Reference to bean 'jpaMappingContext' while Setting bean Property 'mappingContext'; nested Exception is org.springframework.Beans.factory.Beancreationexception: Error Creating bean with name 'jpaMappingContext': Invocation of init method failed; nested Exception is javax.persistence.Persistenceexception: [Persistenceunit: default] Unable to build Hibernate Sessionfactory; nested Exception is org.hibernate.Mappingexception: Could not determine type for: java.util.Set, at table: usuario, for Columns: [org.hibernate.Mapping.Column(items)]
  • 2020-10-29 12:36:13.076 WARN 38324 --- [ main] o.s.b.f.support.DisposableBeanAdapter : Invocation of destroy method failed on bean with name 'entityManagerFactory': javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate Sessionfactory; nested Exception is org.hibernate.Mappingexception: Could not determine type for: java.util.Set, at table: usuario, for Columns: [org.hibernate.Mapping.Column(items)]



> LOGS:

  • org.springframework.Beans.factory.Beancreationexception: Error creating bean with name 'usuarioRepository' defined in com.fcc.ads.turmad.nunesdaniel.appweb.repositories.UsuarioRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Cannot resolve Reference to bean 'jpaMappingContext' while Setting bean Property 'mappingContext'; nested Exception is org.springframework.Beans.factory.Beancreationexception: Error Creating bean with name 'jpaMappingContext': Invocation of init method failed; nested Exception is javax.persistence.Persistenceexception: [Persistenceunit: default] Unable to build Hibernate Sessionfactory; nested Exception is org.hibernate.Mappingexception: Could not determine type for: java.util.Set, at table: usuario, for Columns: [org.hibernate.Mapping.Column(items)]
  • Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested Exception is org.hibernate.Mappingexception: Could not determine type for: java.util.Set, at table: usuario, for Columns: [org.hibernate.Mapping.Column(items)]
  • Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: usuario, for columns: [org.hibernate.mapping.Column(items)]
  • Caused by: org.hibernate.Mappingexception: Could not determine type for: java.util.Set, at table: usuario, for Columns: [org.hibernate.Mapping.Column(items)]



> SQL SCRIPT:

create database app_web_estoque;

use app_web_estoque;

create table item (
    id int not null auto_increment,
    nome varchar(45),
    marca varchar(45),
    quantidade int,
    preco double,
    primary key (id)
);

create table usuario(
    id int not null auto_increment,
    nome varchar(45),
    nomeacesso varchar(45),
    senha varchar(45),
    items int,
    primary key (id)
);

create table usuario_item (
    usuario_id int not null,
    item_id int not null,
    foreign key (usuario_id) references usuario(id),
    foreign key (item_id) references item(id)
);



> ENTITIES:

User

@Entity
@Table(name="usuario")
public class Usuario implements Serializable {
    private static final long serialVersionUID = 1L;

    // outros atributos

    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy="usuarios")
    private Set items = new HashSet();
    
    public Usuario() {
    }
    
    public Usuario(Long id, String nome, String nomeacesso, String senha) {
        this.id = id;
        this.nome = nome;
        this.nomeacesso = nomeacesso;
        this.senha = senha;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long getId() {
        return id;
    }

    // getters e setters

    public Set getItems() {
        return items;
    }
}

Item

@Entity
@Table(name="item")
public class Item implements Serializable {
    private static final long serialVersionUID = 1L;
    
    // outros atributos

    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name="usuario_item", 
        joinColumns= {@JoinColumn(name="item_id")}, 
        inverseJoinColumns= {@JoinColumn(name="usuario_id")})
    private Set usuarios = new HashSet();
    
    public Item() {
    }
    
    public Item(Long id, String nome, String marca, Integer quantidade, Double preco) {
        this.id = id;
        this.nome = nome;
        this.marca = marca;
        this.quantidade = quantidade;
        this.preco = preco;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long getId() {
        return id;
    }

    // getters e setters
    
    public Set getUsuarios() {
        return usuarios;
    }
}



> DEPENDENCIES:

org.springframework.boot
spring-boot-starter-data-jpa

org.springframework.boot spring-boot-starter-thymeleaf
org.springframework.boot spring-boot-starter-we
mysql mysql-connector-java runtime



> PROPERTIES:

spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=jdbc:mysql://localhost:3306/app_web_estoque?useTimezone=true&serverTimezone=UTC
spring.datasource.username=admin
spring.datasource.password=admin
spring.jpa.open-in-view=false
  • I took the @Id annotation from get and put in the variable, the application runs but still does not receive the data in the database.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.