简体   繁体   English

Hibernate/SQL SERVER java.sql.SQLException:无法将 NULL 值插入具有默认值的列

[英]Hibernate/SQL SERVER java.sql.SQLException: Cannot insert NULL values into column that has default value

having Hibernate and SQL Server, I'm facing a problem when I'd like to update an entity called Client .拥有 Hibernate 和 SQL 服务器,当我想更新一个名为Client的实体时遇到问题。 The Client entity has auditing fields like the name of current user who create/update this latter and the date times when these operations occurs.客户端实体具有审计字段,例如创建/更新后者的当前用户的名称以及这些操作发生的日期时间。 The problem here is that when an update happens, the java.sql.SQLException is thrown, below the log for more details:这里的问题是,当更新发生时,会抛出java.sql.SQLException ,在日志下方以获取更多详细信息:

Caused by: java.sql.SQLException: Cannot insert the value NULL into column 'IS_SUPPRIMER', table 'IJSS_DB.dbo.TF_CODE_PAIE';引起:java.sql.SQLException:无法将值 NULL 插入列 'IS_SUPPRIMER',表 'IJSAIES._CODE;。 column does not allow nulls.列不允许空值。 UPDATE fails.更新失败。 at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:368) ~[jtds-1.2.4.jar:1.2.4] at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2820) ~[jtds-1.2.4.jar:1.2.4] at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2258) ~[jtds-1.2.4.jar:1.2.4] at net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:632) ~[jtds-1.2.4.jar:1.2.4] at net.sourceforge.jtds.jdbc.JtdsStatement.processResults(JtdsStatement.java:584) ~[jtds-1.2.4.jar:1.2.4] at net.sourceforge.jtds.Z84BEFFD3A0D49636A58CE6080CAA87C at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:368) ~[jtds-1.2.4.jar:1.2.4] at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java: 2820) ~[jtds-1.2.4.jar:1.2.4] at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2258) ~[jtds-1.2.4.jar:1.2.4] at net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:632) ~[jtds-1.2.4.jar:1.2.4] at net.sourceforge.jtds.jdbc.JtdsStatement.processResults(JtdsStatement.java:584 ) ~[jtds-1.2.4.jar:1.2.4] 在 net.sourceforge.jtds.Z84BEFFD3A0D49636A58CE6080CAA87C 7Z.JtdsStatement.executeSQL(JtdsStatement.java:546) ~[jtds-1.2.4.jar:1.2.4] at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.executeUpdate(JtdsPreparedStatement.java:506) ~[jtds-1.2.4.jar:1.2.4] 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.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3421) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.Z93F725A07423FE1C889F448B33D2 7Z.JtdsStatement.executeSQL(JtdsStatement.java:546) ~[jtds-1.2.4.jar:1.2.4] at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.executeUpdate(JtdsPreparedStatement.java:506) ~[jtds-1.2 .4.jar:1.2.4] 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.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3421) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.persister.entity.AbstractEntityPersister.更新或插入(AbstractEntityPersister.Z93F725A07423FE1C889F448B33D2 1F46Z:3283) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3695) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:149) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:604) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.engine.spi.ActionQueue.lambda$executeActions$1(ActionQueue.java:478) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at Z93F725A07423FE1C8 1F46Z:3283) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3695) ~[hibernate-core-5.4 .10.Final.jar:5.4.10.Final] at org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:149) ~[hibernate-core-5.4.10.Final.jar:5.4.10. Final] at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:604) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.engine.spi. ActionQueue.lambda$executeActions$1(ActionQueue.java:478) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] 在 Z93F725A074 89F448B33D21F46Z.util.LinkedHashMap.forEach(LinkedHashMap.java:684) ~[?:?] at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:475) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:348) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:40) ~[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 89F448B33D21F46Z.util.LinkedHashMap.forEach(LinkedHashMap.java:684) ~[?:?] at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:475) ~[hibernate-core-5.4.10.Final .jar:5.4.10.Final] at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:348) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org .hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:40) ~[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.doFlush(SessionImpl.java:1344) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:435) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3221) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2389) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.engine.Z84BEFFD3A0D49636A58CE6080CAA87 .10.Final.jar:5.4.10.Final] at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1344) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:435) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl. java:3221) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2389) ~[hibernate-core-5.4.10 .Final.jar:5.4.10.Final] 在 org.hibernate.engine.Z84BEFFD3A0D49636A58CE6080CAA87 C7Z.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:447) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:183) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$300(JdbcResourceLocalTransactionCoordinatorImpl.java:40) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcRe C7Z.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:447) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl .beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:183) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$300(JdbcResourceLocalTransactionCoordinatorImpl .java:40) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcRe sourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:281) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:101) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:534) ~[spring-orm-5.2.3.RELEASE.jar:5.2.3.RELEASE]... 98 more sourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:281) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java :101) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:534) ~[spring-orm-5.2. 3.RELEASE.jar:5.2.3.RELEASE]... 98 更多

Note that IS_SUPPRIMER column is among auditing fields that indicates if this column is deleted or not (IS_SUPPRIMER=true /deleted, IS_SUPPRIMER=false/ not deleted),as result we make a logical remove and not a physical remove.请注意, IS_SUPPRIMER列是指示该列是否被删除的审计字段(IS_SUPPRIMER=true /deleted,IS_SUPPRIMER=false/未删除),因此我们进行了逻辑删除而不是物理删除。

What's strange here is that the client entity which is mapped to TF_CLIENT table has a column named IS_SUPPRIMER with default value, below the script:这里奇怪的是,映射到TF_CLIENT表的客户端实体在脚本下方有一个名为IS_SUPPRIMER的默认值列:

CREATE TABLE [dbo].[TF_CLIENT] (
    [CLT_ID] [bigint] IDENTITY(1,1) NOT NULL,
    [CLT_LIBELLE] [nvarchar](50) NOT NULL,
    [CLT_DT_OUVERTURE] [date] NOT NULL,
    [CLT_DT_FERMETURE] [date],
    [CLT_B_ACTIF] [bit] CONSTRAINT DF_TF_CLIENT_B_ACTIF DEFAULT 1 NOT NULL,
    [DATE_CREATION] [datetime2](3),
    [DATE_MODIFICATION] [datetime2](3),
    [DATE_SUPRESSION] [datetime2](3),
    [AUTEUR_CREATION] [nvarchar](100),
    [AUTEUR_MODIFICATION] [nvarchar](100),
    [AUTEUR_SUPRESSION] [nvarchar](100),
    [IS_SUPPRIMER] [bit] CONSTRAINT DF_TF_UTILISATEUR_IS_SUPPRIMER DEFAULT 0 NOT NULL
    CONSTRAINT UQ_CLT_LIBELLE UNIQUE(CLT_LIBELLE),
    CONSTRAINT PK_TF_CLIENT PRIMARY KEY CLUSTERED
(
[CLT_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    END

so why updating a such Client entity will require a value of IS_SUPPRIMER which has already a default value?那么为什么更新这样的客户端实体需要一个已经有默认值的IS_SUPPRIMER值呢?

Below the code of Auditing class:在审核 class 的代码下方:

@Embeddable
@Getter
@Setter
@NoArgsConstructor
public class FieldAuditing implements Serializable {

    @Column(name = "DATE_CREATION")
    private Instant createdAt;

    @Column(name = "DATE_MODIFICATION")
    private Instant updatedAt;

    @Column(name = "DATE_SUPRESSION")
    private Instant deletedAt;

    @Column(name = "AUTEUR_CREATION", length = 100)
    private String createdBy;

    @Column(name = "AUTEUR_MODIFICATION", length = 100)
    private String updatedBy;

    @Column(name = "AUTEUR_SUPRESSION", length = 100)
    private String deletedBy;

    @Column(name = "IS_SUPPRIMER")
    private Boolean isDeleted = false;

    @PrePersist
    public void prePersist() {
        if (this.isDeleted == null)
            setIsDeleted(Boolean.FALSE);
        setCreatedAt(Instant.now());
        setCreatedBy(LoggedInUser.get());
    }

    @PreUpdate
    public void preUpdate() {
        if (this.isDeleted == null)
            setIsDeleted(Boolean.FALSE);
        setUpdatedAt(Instant.now());
        setUpdatedBy(LoggedInUser.get());
    }

}

Also the Client Entity class:还有客户端实体 class:

@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Table(name="TF_CLIENT", schema="dbo")
public class Client implements Serializable {

    private static final long serialVersionUID = 8832848102370267801L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator="native")
    @GenericGenerator(name = "native", strategy = "native")
    @Column(name = "CLT_ID", nullable = false)
    private Long id;

    @Column(name = "CLT_LIBELLE", nullable = false, length = 50, unique = true)
    private String libelle;

    @Temporal(TemporalType.DATE)
    @Column(name = "CLT_DT_OUVERTURE", nullable = false)
    private Date dateOuverture;

    @Temporal(TemporalType.DATE)
    @Column(name = "CLT_DT_FERMETURE")
    private Date dateFermeture;

    @Column(name = "CLT_B_ACTIF")
    private boolean isActif;

    @Embedded
    private FieldAuditing fieldAuditing = new FieldAuditing() ;

    @JsonIgnore
    @OneToMany
    @Builder.Default
    @JoinColumn(name="CLI_ID")
    Set<Etablissement> etablissements = Collections.emptySet();

    @JsonIgnore
    @OneToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
    @JoinColumn(name="CLI_ID")
    @Builder.Default
    @Setter(AccessLevel.NONE)
    Set<CodePaie> codePaies = new HashSet<>();

    @OneToMany(fetch = FetchType.EAGER, cascade = {CascadeType.ALL}, mappedBy = "client", orphanRemoval = true)
    @Builder.Default
    @Setter(AccessLevel.NONE)
    Set<ClientAction> clientActions = Sets.newHashSet();

    @OneToMany(fetch = FetchType.EAGER, cascade = {CascadeType.ALL}, mappedBy = "client", orphanRemoval = true)
    @Builder.Default
    @Setter(AccessLevel.NONE)
    Set<ClientEtat> clientEtats = Sets.newHashSet();

    public void addClientActions(Set<ClientAction> clientActions) {
        clientActions.forEach(clientAction -> clientAction.setClient(this));
        this.clientActions.addAll(clientActions);
    }

    public void addClientEtats(Set<ClientEtat> clientEtats) {
        clientEtats.forEach(etat -> etat.setClient(this));
        this.clientEtats.addAll(clientEtats);
    }

    public void addCodePaies(Set<CodePaie> codePaies) {
        codePaies.forEach(codePaie -> codePaie.setClient(this));
        this.codePaies.addAll(codePaies);
    }

    public void removeAllCodePaie() {
        codePaies.forEach(codePaie -> codePaie.setClient(null));
        this.codePaies.clear();
    }

}

Finally the code snippet of updating Client entity:最后是更新客户端实体的代码片段:

 private ClientDto save(ClientDto clientDto, Client client) {
        startDateShouldBeBeforeEndDate(clientDto);
        hasUniqueCodePaies(clientDto.getCodePaies());
        Client clientSaved = clientRepository.save(clientMapper.toEntity(clientDto, client));
        clientMapper.addOrRemoveClientActions(clientDto, clientSaved);
        clientMapper.addOrRemoveClientEtats(clientDto, clientSaved);
        clientRepository.save(clientSaved);
        clientDto.setId(clientSaved.getId());
        return clientDto;
    }

Kindly, could you take a look at my issue description and help me.请您看看我的问题描述并帮助我。

I'm thankful for any help.我很感激任何帮助。

The exception indicates that the problem lies in TF_CODE_PAIE not TF_CLIENT.异常表明问题出在 TF_CODE_PAIE 而不是 TF_CLIENT。 I would guess that TF_CODE_PAIE.IS_SUPPRIMER does not have a default value.我猜 TF_CODE_PAIE.IS_SUPPRIMER 没有默认值。

JPA execute query with all field of table. JPA 对表的所有字段执行查询。 So, when you don't set isDeleted field then it null and JPA execute query with null value for isDeleted field that's why you got the error.因此,当您不设置isDeleted字段时,它 null 和 JPA 使用 null 值执行查询isDeleted字段,这就是您收到错误的原因。

That's means you are trying to set null value in not-null field which is wrong.这意味着您正在尝试在错误的非空字段中设置 null 值。 Default means when you don't set the field in sql query it set by default value.默认表示当您未在 sql 查询中设置该字段时,将其设置为默认值。 Use this用这个

@Column(name = "IS_SUPPRIMER", columnDefinition = "bit default 0")
private Boolean isDeleted;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.sql.SQLException:无法将值NULL插入列 - java.sql.SQLException: Cannot insert the value NULL into column “ java.sql.SQLException表..中的列..不允许空值”,尽管列值不为空 - “java.sql.SQLException The column .. in table .. does not allow null values” although column value is not null java.sql.SQLException:没有此类值与我尝试插入的值 - java.sql.SQLException: no such column with the value I'm trying to insert java.sql.SQLException:没有这样的列 - java.sql.SQLException: no such column 获取java.sql.SQLException:无效的列名称与休眠 - Getting java.sql.SQLException: Invalid column name with hibernate java.sql.SQLException: 列 'n_id_proyecto_categoria_has_proyecto' 未找到,查询 hibernate - java.sql.SQLException: Column 'n_id_proyecto_categoria_has_proyecto' not found, query hibernate java.sql.SQLException:检测到未定义的列名。 (休眠) - java.sql.SQLException: An undefined column name was detected. (Hibernate) (Hibernate)java.sql.SQLException:字段&#39;xxxx&#39;没有默认值 - (Hibernate) java.sql.SQLException: Field 'xxxx' doesn't have a default value (休眠)由:java.sql.SQLException引起:字段&#39;UPDATE_TS&#39;没有默认值 - (Hibernate)Caused by: java.sql.SQLException: Field 'UPDATE_TS' doesn't have a default value Hibernate java.sql.SQLException:未为参数1指定值 - Hibernate java.sql.SQLException: No value specified for parameter 1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM