java - 无法执行语句; SQL [不适用];约束[空];

标签 java sql hibernate spring-boot

从特定模式中删除行时出现此错误:

Hibernate: select journal0_.id as id1_2_0_, journal0_.content as content2_2_0_, journal0_.filename as filename3_2_0_, journal0_.subject as subject4_2_0_, journal0_.tags as tags5_2_0_, journal0_.user_id as user_id6_2_0_, journal0_.version as version7_2_0_ from journal journal0_ where journal0_.id=?
Hibernate: select nextval ('hibernate_sequence')
Hibernate: insert into subscription (journal_id, user_id, version, id) values (?, ?, ?, ?)
2016-09-03 01:08:01.581  WARN 13462 --- [nio-8080-exec-9] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 23505, SQLState: 23505
2016-09-03 01:08:01.581 ERROR 13462 --- [nio-8080-exec-9] o.h.engine.jdbc.spi.SqlExceptionHelper   : Unique index or primary key violation: "UK_9XFQUT5UKXNSBX8NL2LR23TC5_INDEX_9 ON PUBLIC.SUBSCRIPTION(USER_ID, JOURNAL_ID) VALUES (9, 2, 10)"; SQL statement:
insert into subscription (journal_id, user_id, version, id) values (?, ?, ?, ?) [23505-191]
2016-09-03 01:08:01.598  INFO 13462 --- [nio-8080-exec-9] o.h.e.j.b.internal.AbstractBatchImpl     : HHH000010: On release of batch it still contained JDBC statements
2016-09-03 01:08:01.632 ERROR 13462 --- [nio-8080-exec-9] c.j.exceptions.ErrorController           : Exception during execution of SpringSecurity application

org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement

即使调用了带有正确参数的 delete 方法,它仍然在运行导致数据违规问题的插入查询。

这是模型:

@Entity
@Table(uniqueConstraints={@UniqueConstraint(columnNames={"userId", "journalId"})})
public class Subscription {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @Version
    private Integer version;

    private Integer userId;
    private Integer journalId;

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

    public Integer getId() {
        return this.id;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public Integer getVersion() {
        return this.version;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public Integer getUserId() {
        return this.userId;
    }

    public void setJournalId(Integer journalId) {
        this.journalId = journalId;
    }

    public Integer getJournalId() {
        return this.journalId;
    }
}

这里是删除方法被调用的地方:

@Override
public void unsubscribeJournalForSubscriber(Journal journal, Account subscriber) {

    Subscription subscription = new Subscription();
    subscription.setJournalId(journal.getId());
    subscription.setUserId(subscriber.getId());


    this.subscriptionRepository.delete(subscription);
}

最佳答案

如果您想在不使用主键的情况下删除记录,请为此创建一个查询。在名为“SubscriptionRepository”的方法中创建一个方法

@修改中 @Query("从 Subscription 中删除 s.role.userId = ?1 and s.journalId = ?2") deleteByUserIdJournalId(Integer userId,Integer journalId) 然后使用该方法删除您的记录。 按照这个https://docs.spring.io/spring-data/jpa/docs/current/reference/html/

关于java - 无法执行语句; SQL [不适用];约束[空];,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39299698/

相关文章:

java - Typemap javaimports不适用于内部C++类

java - BAD 请求 400 使用多部分/表单数据泽西客户端响应 java

sql - ORDER BY 仅适用于列别名

hibernate - 如何使用 JUnit 测试验证实体类 - Hibernate @Column 注释

java - 我收到“com.ibm.db2.jcc.am.SqlIntegrityConstraintViolationException : DB2 SQL Error: SQLCODE=-803, SQLSTATE=23505

java - 制作一个二维的 10x2 数组,每个元素设置为字符串 "x"

java - 为什么编译器给出 .NullPointerException 错误

mysql - 如何根据其他表中的列检索行

mysql - 获取特定条目以防重复条目

仅使用主键的 Hibernate 代理加载