【问题标题】:Playframework PersistenceException: ERROR executing DML bindLog[] error[Unique index or primary key violationPlayframework PersistenceException: ERROR execution DML bindLog[] error[唯一索引或主键违规
【发布时间】:2014-01-05 01:02:08
【问题描述】:

我正在使用 playframework 制作一个小“网站”。我已经制作了一种方法来创建一个 ploeg en 来显示它们。但是当我尝试删除它们时(制作了一个按钮),ik 给了我这个错误:

    [PersistenceException: ERROR executing DML bindLog[] error[Unique index or primary key
    violation: "PRIMARY_KEY_4 ON PUBLIC.PLOEG(ID)"; SQL statement:\n insert into ploeg 
    (id, naam, punten) values (?,?,?) [23505-172]]]

这是我的 Ploeg.java 中错误所在的方法:

    public static void maak(Ploeg ploeg) {
        ploeg.save();
    }

这是我的 Ploeg.java:

package models;

import play.data.validation.Constraints;
import play.db.ebean.Model;

import play.data.validation.Constraints.*;
import javax.persistence.*;
import java.util.*;
/**
 * Created by Bram on 3/01/14.
 */

@Entity
public class Ploeg extends Model {

@Id
public Long id;

//@Required
public String naam;
public Integer punten;

public static Finder<Long, Ploeg> find = new Finder(
        Long.class, Ploeg.class
);

public static List<Ploeg> all() {
    return find.all();
}

public static Ploeg maak(Ploeg ploeg) {
    ploeg.save();
    return ploeg;
}

public static void delete(Long id) {
    find.ref(id).delete();
}

public static void geefPunt(Long id) {
    Ploeg ploegje = find.byId(id);
    ploegje.punten = ploegje.punten + 3;
}

}

现在从我的 Applicaion.java 调用该方法,如下所示:

    public static Result deletePloeg(Long id) {
        Ploeg.delete(id);
        return redirect(routes.Application.ploegen());
    }

同样,该方法是通过单击删除按钮从 index.scala.html 触发的:

    @form(routes.Application.deletePloeg(ploeg.id)) {
        <input type="submit" value="Delete">
    }

我希望有人可以帮助我解释这个错误的含义以及如何解决它。我已经尝试这样做了 6 个多小时了。

【问题讨论】:

  • 检查为什么插入语句在删除时执行
  • 我这样做了,但我找不到它调用插入语句的位置。您可以看到它执行了哪些步骤,并且没有任何迹象表明它触发了插入函数或任何其他函数。
  • 错误只是说在 db 中有一行具有相同的主键,所以主键应该是唯一的。另外发布你的 Ploeg
  • routes.Application.ploegen() 这个方法在做什么。我认为这是生成一个新的 Plogen 并生成插入语句。还要让 id 自动生成 @GeneratedValue(strategy=GenerationType.AUTO)
  • 那 routes.Application.ploegen() 是重新路由到显示所有 ploegen 的页面。这就是它的作用:return ok(views.html.index.render(Ploeg.all(), ploegForm));

标签: java html scala playframework


【解决方案1】:

在我的路线文件中,我有这一行:

POST    /ploegen                    controllers.Application.deletePloeg(id: Long)

我把它改成了:

POST    /                           controllers.Application.deletePloeg(id: Long)

我不知道为什么,但它有效!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-28
    • 2014-12-27
    • 2019-09-28
    • 2019-12-24
    • 2021-05-19
    • 2019-02-09
    • 1970-01-01
    • 2021-08-16
    相关资源
    最近更新 更多