Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
60 views
in Technique[技术] by (71.8m points)

java - Hibernate insert not working (Request processing failed)

I'm trying use Hibernate to insert in a table but it isn't working and i don't know why. I was running the sql insert script directly in the postgresql and that's running.

That's the error Message:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request
processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: 
Executing an update/delete query; nested exception is javax.persistence.TransactionRequiredException: 
Executing an update/delete query] with root cause

The insert:

import java.util.List;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;

public interface QueryGrupoDeEstudosUsuario extends CrudRepository<EntidadeGrupoEstudoUsuario, Integer>{

    @Modifying
    @Query(value = "insert into grupo_estudo_usuario (id_grupo_estudo, id_usuario, administrador, pedido_pendente) values (3 , 1, false, true)", nativeQuery = true)
    void insereNaTabela(); 
    
}

The entity class:

@Getter
@Setter
@Entity
@Table(name = "grupo_estudo_usuario")
@ToString
public class EntidadeGrupoEstudoUsuario {

    @Id
    @Column(name = "id_grupo_estudo_usuario")
    private Integer idGrupoEstudoUsuario;

    @Getter(AccessLevel.NONE)
    @ManyToOne
    @JoinColumn(name = "id_grupo_estudo")
    private EntidadeGrupoDeEstudos idGrupoDeEstudos;
    
    @ManyToOne
    @JoinColumn(name = "id_usuario")
    private EntidadeUsuario usuario;
    
    @Column(name = "administrador")
    private Boolean administrador;

        
    @Column(name = "pedido_pendente")
    private Boolean pedidoPendente;

    @Column(name = "dt_atz")
    private Date data;


}

The SQL script:

CREATE SEQUENCE public.grupo_estudo_usuario_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    MAXVALUE 99999999
    CACHE 1;

ALTER TABLE public.grupo_estudo_usuario_id_seq OWNER TO postgres;

CREATE TABLE public.grupo_estudo_usuario (
    id_grupo_estudo bigint NOT NULL,
    id_usuario bigint NOT NULL,
    administrador boolean NOT NULL ,
    pedido_pendente boolean DEFAULT true NOT NULL,
    dt_atz timestamp(0) without time zone,
    id_grupo_estudo_usuario bigint DEFAULT nextval('public.grupo_estudo_usuario_id_seq'::regclass) NOT NULL
);

SELECT pg_catalog.setval('public.grupo_estudo_usuario_id_seq', 14, true);

ALTER TABLE ONLY public.grupo_estudo_usuario
    ADD CONSTRAINT grupo_estudo_usuario_pk PRIMARY KEY (id_grupo_estudo_usuario);

ALTER TABLE ONLY public.grupo_estudo_usuario
    ADD CONSTRAINT grupo_estudo_usuario_fk_1 FOREIGN KEY (id_grupo_estudo) REFERENCES public.grupo_estudo(id_grupo_estudo);

ALTER TABLE ONLY public.grupo_estudo_usuario
    ADD CONSTRAINT grupo_estudo_usuario_fk FOREIGN KEY (id_usuario) REFERENCES public.usuario(id_usuario);

CREATE TRIGGER grupo_estudo_usuario_dt_atz BEFORE INSERT OR UPDATE ON public.grupo_estudo_usuario FOR EACH ROW EXECUTE FUNCTION public.dt_atz();

How i'm using QueryGrupoDeEstudosUsuario:

@Repository
Public class insereTest {
     @Autowired
     QueryGrupoDeEstudosUsuario query;

     public void insere() {
          query.insereNaTabela();
     }
}
question from:https://stackoverflow.com/questions/65838867/hibernate-insert-not-working-request-processing-failed

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Make the repository method @Transactional

import java.util.List;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

public interface QueryGrupoDeEstudosUsuario extends CrudRepository<EntidadeGrupoEstudoUsuario, Integer>{
    
    @Transactional
    @Modifying
    @Query(value = "insert into grupo_estudo_usuario (id_grupo_estudo, id_usuario, administrador, pedido_pendente) values (3 , 1, false, true)", nativeQuery = true)
    void insereNaTabela(); 
    
}

The transaction management have to be activated with the @EnableTransactionManagement annotation in the application config


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...