Java 无法执行语句;SQL [不适用];约束 [null];

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39299698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:02:13  来源:igfitidea点击:

Could not execute statement; SQL [n/a]; constraint [null];

javasqlhibernatespring-boot

提问by Rajeev Massey

I am getting this error while delete a row from a particular schema:

从特定架构中删除一行时出现此错误:

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

Even though the delete method with proper arguments is called still it's running the insert query which is causing data violation issue.

即使调用了具有适当参数的删除方法,它仍在运行导致数据违规问题的插入查询。

Here is the model:

这是模型:

@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;
    }
}

This is where the delete method is called:

这是调用 delete 方法的地方:

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

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


    this.subscriptionRepository.delete(subscription);
}

采纳答案by Keaz

If you want to delete records without using primary key then create a query for that. create a method in the "SubscriptionRepository" called

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

@Modifying @Query("delete from Subscription s where s.role. userId = ?1 and s. journalId = ?2") deleteByUserIdJournalId(Integer userId,Integer journalId)then use that method to delete your records. follow this https://docs.spring.io/spring-data/jpa/docs/current/reference/html/

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

回答by Md Zahid Raza

For deleting Subscription entity, you need to set only Primary key of the entity, which is idfield of Subscription Entity. However you are setting journalIdand userIdfield and trying to delete. You should only set the idfield of Subscription entity and it will be deleted.

删除订阅实体,只需要设置实体的主键,即id订阅实体的字段。但是,您正在设置journalIduserId字段并尝试删除。您只需设置id订阅实体的字段,它将被删除。

Update:

更新:

Your repository code should be

您的存储库代码应该是

public interface SubscriptionRepository extends CrudRepository<Subscription, Integer> {

    /**
     * Returns array of Subscribed journals by specific user's id
     * 
     * @param userId
     * @return
     */
    ArrayList<Subscription> findByUserId(Integer userId);
    List<Subscription> findByUserIdAndJournalId(Integer userId, Integer journalId);

}

Now your business loginc code will be:

现在您的业务登录代码将是:

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

    List<Subscription> list = subscriptionRepository.findByUserIdAndJournalId(subscriber.getId(), journal.getId());

    for(Subscription subscription : list){
        subscriptionRepository.delete(subscription);
    }

}