重复键值

Duplicate Key value

我为数据库创建了我的实体,在我尝试使用 SQL 查询插入元素后,我使用 talend 插入了它们,它告诉我该 ID 已经存在。

package o.absix.rest.entities;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Fnc {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(nullable = false,updatable = true)
    private int N_fnc;
    private String Etat;
    private String Cloture_par;
    private Date date_cloture;
    private String codeart;
    private String libelle_art;
    private String collection;
    private String Type;
    private String detect_par;
    private Date date_detection;
    private String lieu_detection;
    private String libelle_def;
    private String doc_ref;
    private String desc_non_confir;
    private String desc_principale;
    private Integer quantite_litigieuse;
    private Integer quantite_non_conf;
    private String defaut_secon;
    private String unite;
    private String valeur_piece;
    private Integer rebut_qte;
    private String rebut_pourcen;
    private String valeur_rebut;
    private Integer retouche_qte;
    private String retouche_pourcen;
    private Integer accp_en_etat_qte;
    private String accp_en_etat_pource;
    private Boolean valider;
    private String nature;
    private Integer n_phase;
    private Integer n_operation;
   /*Constructor*///
   public Fnc(){

   }


    /**
     * @return int return the N_fnc
     */
    public int getN_fnc() {
        return N_fnc;
    }

    /**
     * @param N_fnc the N_fnc to set
     */
    public void setN_fnc(int N_fnc) {
        this.N_fnc = N_fnc;
    }

    /**
     * @return String return the Etat
     */
    public String getEtat() {
        return Etat;
    }

    /**
     * @param Etat the Etat to set
     */
    public void setEtat(String Etat) {
        this.Etat = Etat;
    }

    /**
     * @return String return the Cloture_par
     */
    public String getCloture_par() {
        return Cloture_par;
    }

    /**
     * @param Cloture_par the Cloture_par to set
     */
    public void setCloture_par(String Cloture_par) {
        this.Cloture_par = Cloture_par;
    }

    /**
     * @return Date return the date_cloture
     */
    public Date getDate_cloture() {
        return date_cloture;
    }

    /**
     * @param date_cloture the date_cloture to set
     */
    public void setDate_cloture(Date date_cloture) {
        this.date_cloture = date_cloture;
    }

    /**
     * @return long return the codeart
     */
    public String getCodeart() {
        return codeart;
    }

    /**
     * @param codeart the codeart to set
     */
    public void setCodeart(String codeart) {
        this.codeart = codeart;
    }

    /**
     * @return String return the libelle_art
     */
    public String getLibelle_art() {
        return libelle_art;
    }

    /**
     * @param libelle_art the libelle_art to set
     */
    public void setLibelle_art(String libelle_art) {
        this.libelle_art = libelle_art;
    }

    /**
     * @return String return the collection
     */
    public String getCollection() {
        return collection;
    }

    /**
     * @param collection the collection to set
     */
    public void setCollection(String collection) {
        this.collection = collection;
    }

    /**
     * @return String return the Type
     */
    public String getType() {
        return Type;
    }

    /**
     * @param Type the Type to set
     */
    public void setType(String Type) {
        this.Type = Type;
    }

    /**
     * @return String return the detect_par
     */
    public String getDetect_par() {
        return detect_par;
    }

    /**
     * @param detect_par the detect_par to set
     */
    public void setDetect_par(String detect_par) {
        this.detect_par = detect_par;
    }

    /**
     * @return Date return the date_detection
     */
    public Date getDate_detection() {
        return date_detection;
    }

    /**
     * @param date_detection the date_detection to set
     */
    public void setDate_detection(Date date_detection) {
        this.date_detection = date_detection;
    }

    /**
     * @return String return the lieu_detection
     */
    public String getLieu_detection() {
        return lieu_detection;
    }

    /**
     * @param lieu_detection the lieu_detection to set
     */
    public void setLieu_detection(String lieu_detection) {
        this.lieu_detection = lieu_detection;
    }

    /**
     * @return String return the libelle_def
     */
    public String getLibelle_def() {
        return libelle_def;
    }

    /**
     * @param libelle_def the libelle_def to set
     */
    public void setLibelle_def(String libelle_def) {
        this.libelle_def = libelle_def;
    }

    /**
     * @return String return the doc_ref
     */
    public String getDoc_ref() {
        return doc_ref;
    }

    /**
     * @param doc_ref the doc_ref to set
     */
    public void setDoc_ref(String doc_ref) {
        this.doc_ref = doc_ref;
    }

    /**
     * @return String return the desc_non_confir
     */
    public String getDesc_non_confir() {
        return desc_non_confir;
    }

    /**
     * @param desc_non_confir the desc_non_confir to set
     */
    public void setDesc_non_confir(String desc_non_confir) {
        this.desc_non_confir = desc_non_confir;
    }

    /**
     * @return String return the desc_principale
     */
    public String getDesc_principale() {
        return desc_principale;
    }

    /**
     * @param desc_principale the desc_principale to set
     */
    public void setDesc_principale(String desc_principale) {
        this.desc_principale = desc_principale;
    }

    /**
     * @return Integer return the quantite_litigieuse
     */
    public Integer getQuantite_litigieuse() {
        return quantite_litigieuse;
    }

    /**
     * @param quantite_litigieuse the quantite_litigieuse to set
     */
    public void setQuantite_litigieuse(Integer quantite_litigieuse) {
        this.quantite_litigieuse = quantite_litigieuse;
    }

    /**
     * @return Integer return the quantite_non_conf
     */
    public Integer getQuantite_non_conf() {
        return quantite_non_conf;
    }

    /**
     * @param quantite_non_conf the quantite_non_conf to set
     */
    public void setQuantite_non_conf(Integer quantite_non_conf) {
        this.quantite_non_conf = quantite_non_conf;
    }

    /**
     * @return String return the defaut_secon
     */
    public String getDefaut_secon() {
        return defaut_secon;
    }

    /**
     * @param defaut_secon the defaut_secon to set
     */
    public void setDefaut_secon(String defaut_secon) {
        this.defaut_secon = defaut_secon;
    }

    /**
     * @return String return the unite
     */
    public String getUnite() {
        return unite;
    }

    /**
     * @param unite the unite to set
     */
    public void setUnite(String unite) {
        this.unite = unite;
    }

   

    /**
     * @return Boolean return the valider
     */
    public Boolean isValider() {
        return valider;
    }

    /**
     * @param valider the valider to set
     */
    public void setValider(Boolean valider) {
        this.valider = valider;
    }


    /**
     * @return String return the valeur_piece
     */
    public String getValeur_piece() {
        return valeur_piece;
    }

    /**
     * @param valeur_piece the valeur_piece to set
     */
    public void setValeur_piece(String valeur_piece) {
        this.valeur_piece = valeur_piece;
    }

    /**
     * @return Integer return the rebut_qte
     */
    public Integer getRebut_qte() {
        return rebut_qte;
    }

    /**
     * @param rebut_qte the rebut_qte to set
     */
    public void setRebut_qte(Integer rebut_qte) {
        this.rebut_qte = rebut_qte;
    }

    /**
     * @return String return the rebut_pourcen
     */
    public String getRebut_pourcen() {
        return rebut_pourcen;
    }

    /**
     * @param rebut_pourcen the rebut_pourcen to set
     */
    public void setRebut_pourcen(String rebut_pourcen) {
        this.rebut_pourcen = rebut_pourcen;
    }

    /**
     * @return String return the valeur_rebut
     */
    public String getValeur_rebut() {
        return valeur_rebut;
    }

    /**
     * @param valeur_rebut the valeur_rebut to set
     */
    public void setValeur_rebut(String valeur_rebut) {
        this.valeur_rebut = valeur_rebut;
    }

    /**
     * @return Integer return the retouche_qte
     */
    public Integer getRetouche_qte() {
        return retouche_qte;
    }

    /**
     * @param retouche_qte the retouche_qte to set
     */
    public void setRetouche_qte(Integer retouche_qte) {
        this.retouche_qte = retouche_qte;
    }

    /**
     * @return String return the retouche_pourcen
     */
    public String getRetouche_pourcen() {
        return retouche_pourcen;
    }

    /**
     * @param retouche_pourcen the retouche_pourcen to set
     */
    public void setRetouche_pourcen(String retouche_pourcen) {
        this.retouche_pourcen = retouche_pourcen;
    }

    /**
     * @return Integer return the accp_en_etat_qte
     */
    public Integer getAccp_en_etat_qte() {
        return accp_en_etat_qte;
    }

    /**
     * @param accp_en_etat_qte the accp_en_etat_qte to set
     */
    public void setAccp_en_etat_qte(Integer accp_en_etat_qte) {
        this.accp_en_etat_qte = accp_en_etat_qte;
    }

    /**
     * @return String return the accp_en_etat_pource
     */
    public String getAccp_en_etat_pource() {
        return accp_en_etat_pource;
    }

    /**
     * @param accp_en_etat_pource the accp_en_etat_pource to set
     */
    public void setAccp_en_etat_pource(String accp_en_etat_pource) {
        this.accp_en_etat_pource = accp_en_etat_pource;
    }

  


    /**
     * @return String return the nature
     */
    public String getNature() {
        return nature;
    }

    /**
     * @param nature the nature to set
     */
    public void setNature(String nature) {
        this.nature = nature;
    }

    /**
     * @return Integer return the n_phase
     */
    public Integer getN_phase() {
        return n_phase;
    }

    /**
     * @param n_phase the n_phase to set
     */
    public void setN_phase(Integer n_phase) {
        this.n_phase = n_phase;
    }

    /**
     * @return Integer return the n_operation
     */
    public Integer getN_operation() {
        return n_operation;
    }

    /**
     * @param n_operation the n_operation to set
     */
    public void setN_operation(Integer n_operation) {
        this.n_operation = n_operation;
    }

    public Fnc(int n_fnc, String etat, String cloture_par, Date date_cloture, String codeart, String libelle_art,
            String collection, String type, String detect_par, Date date_detection, String lieu_detection,
            String libelle_def, String doc_ref, String desc_non_confir, String desc_principale,
            Integer quantite_litigieuse, Integer quantite_non_conf, String defaut_secon, String unite,
            String valeur_piece, Integer rebut_qte, String rebut_pourcen, String valeur_rebut, Integer retouche_qte,
            String retouche_pourcen, Integer accp_en_etat_qte, String accp_en_etat_pource, Boolean valider,
            String nature, Integer n_phase, Integer n_operation) {
        N_fnc = n_fnc;
        Etat = etat;
        Cloture_par = cloture_par;
        this.date_cloture = date_cloture;
        this.codeart = codeart;
        this.libelle_art = libelle_art;
        this.collection = collection;
        Type = type;
        this.detect_par = detect_par;
        this.date_detection = date_detection;
        this.lieu_detection = lieu_detection;
        this.libelle_def = libelle_def;
        this.doc_ref = doc_ref;
        this.desc_non_confir = desc_non_confir;
        this.desc_principale = desc_principale;
        this.quantite_litigieuse = quantite_litigieuse;
        this.quantite_non_conf = quantite_non_conf;
        this.defaut_secon = defaut_secon;
        this.unite = unite;
        this.valeur_piece = valeur_piece;
        this.rebut_qte = rebut_qte;
        this.rebut_pourcen = rebut_pourcen;
        this.valeur_rebut = valeur_rebut;
        this.retouche_qte = retouche_qte;
        this.retouche_pourcen = retouche_pourcen;
        this.accp_en_etat_qte = accp_en_etat_qte;
        this.accp_en_etat_pource = accp_en_etat_pource;
        this.valider = valider;
        this.nature = nature;
        this.n_phase = n_phase;
        this.n_operation = n_operation;
    }

}

日志和错误消息:

Hibernate: select typeplan0_.id as id1_15_, typeplan0_.type as type2_15_ from type_plan typeplan0_
Hibernate: insert into fnc (cloture_par, etat, type, accp_en_etat_pource, accp_en_etat_qte, codeart, collection, date_cloture, date_detection, defaut_secon, desc_non_confir, desc_principale, detect_par, doc_ref, libelle_art, libelle_def, lieu_detection, n_operation, n_phase, nature, quantite_litigieuse, quantite_non_conf, rebut_pourcen, rebut_qte, retouche_pourcen, retouche_qte, unite, valeur_piece, valeur_rebut, 
valider) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2020-12-22 09:25:16.111  WARN 23320 --- [nio-8081-exec-9] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 23505
2020-12-22 09:25:16.111 ERROR 23320 --- [nio-8081-exec-9] o.h.engine.jdbc.spi.SqlExceptionHelper   : ERROR: duplicate key value violates unique constraint "fnc_pkey"
  Détail : Key (n_fnc)=(17) already exists.
2020-12-22 09:25:16.218 ERROR 23320 --- [nio-8081-exec-9] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [fnc_pkey]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause

org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "fnc_pkey"
  Détail : Key (n_fnc)=(17) already exists.
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2505) ~[postgresql-42.2.9.jar:42.2.9]      
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2241) ~[postgresql-42.2.9.jar:42.2.9]
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:310) ~[postgresql-42.2.9.jar:42.2.9]
at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:447) ~[postgresql-42.2.9.jar:42.2.9]
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:368) ~[postgresql-42.2.9.jar:42.2.9]
at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:158) ~[postgresql-42.2.9.jar:42.2.9]
at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:124) ~[postgresql-42.2.9.jar:42.2.9]
at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61) ~[HikariCP-3.4.2.jar:na]
at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate(HikariProxyPreparedStatement.java) ~[HikariCP-3.4.2.jar:na]   
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:197) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final]
at org.hibernate.dialect.identity.GetGeneratedKeysDelegate.executeAndExtract(GetGeneratedKeysDelegate.java:57) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final]
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:43) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final]
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3124) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final]
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3728) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final]
at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:84) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final]
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:645) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final]
at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:282) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final]
at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:263) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final]      
at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:317) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final]
at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:335) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final]
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:292) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final]
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:198) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final]
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:128) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final]
at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:192) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final]
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:135) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final]
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:62) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final]
at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:108) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final]
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:702) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final]
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:688) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:368) ~[spring-orm-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at com.sun.proxy.$Proxy90.persist(Unknown Source) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:314) ~[spring-orm-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at com.sun.proxy.$Proxy90.persist(Unknown Source) ~[na:na]
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:554) ~[spring-data-jpa-2.2.4.RELEASE.jar:2.2.4.RELEASE]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:371) ~[spring-data-commons-2.2.4.RELEASE.jar:2.2.4.RELEASE]
at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:204) ~[spring-data-commons-2.2.4.RELEASE.jar:2.2.4.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:657) ~[spring-data-commons-2.2.4.RELEASE.jar:2.2.4.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:621) ~[spring-data-commons-2.2.4.RELEASE.jar:2.2.4.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:605) ~[spring-data-commons-2.2.4.RELEASE.jar:2.2.4.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:366) 
~[spring-tx-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:99) ~[spring-tx-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) ~[spring-tx-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:178) ~[spring-data-jpa-2.2.4.RELEASE.jar:2.2.4.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:95) ~[spring-aop-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at com.sun.proxy.$Proxy114.save(Unknown Source) ~[na:na]
at o.absix.rest.services.FncService.addFnc(FncService.java:21) ~[classes/:na]
at o.absix.rest.controllers.FncController.addFnc(FncController.java:29) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106) ~[spring-webmvc-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:888) ~[spring-webmvc-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793) ~[spring-webmvc-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) ~[spring-webmvc-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660) ~[tomcat-embed-core-9.0.30.jar:9.0.30]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.30.jar:9.0.30]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.30.jar:9.0.30]

生成主键的选项

1. GenerationType.AUTO
   GenerationType.AUTO is the default generation type and lets the persistence provider choose the generation strategy
2. GenerationType.IDENTITY
    GenerationType.IDENTITY is the easiest to use but not the best one from a performance point of view
3. GenerationType.SEQUENCE
   The GenerationType.SEQUENCE is my preferred way to generate primary key values and uses a database sequence to generate unique values
4. GenerationType.TABLE
   It simulates a sequence by storing and updating its current value in a database table which requires the use of pessimistic locks which put all transactions into a sequential order

我的建议使用:GenerationType.SEQUENCE

谢谢大家的回复,我找到答案了。

我在数据库中创建了名为 Fnc_generator 的序列。

CREATE SEQUENCE Fnc_generator;

将序列添加到我的 table

ALTER TABLE fnc ALTER COLUMN n_fnc SET DEFAULT nextval('Fnc_generator');

在我的实体中,我在生成密钥的选项中添加了序列

@Id @SequenceGenerator(name="Fnc_generator", sequenceName = "Fnc_seq", allocationSize=50) @GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "Fnc_generator") @Column(nullable = false,updatable = true).

之后,在我的数据中设置了 fnc_generator 我的 id 的最大值。

SELECT setval('Fnc_generator', max(n_fnc)) from fnc;