Play - oracle 10g:PersistenceException:ERROR执行DML bindLog []错误[ORA-00001:唯一键约束违规(PLAY.PK_COMPANY)\ n

时间:2013-01-11 16:06:42

标签: java playframework oracle10g playframework-2.0 ebean

我正在尝试将oracle 10g和ebeans用于plesk的“计算机数据库”示例应用程序。但是,当我尝试在数据库中插入新计算机时,我收到此错误:

[PersistenceException: ERROR executing DML bindLog[] error[ORA-00001: Unique key constraint violation (PLAY.PK_COMPANY)\n ]]

在以下行:

{ 
 Form<Computer> computerForm = form(Computer.class).bindFromRequest();
        if(computerForm.hasErrors()) {
            return badRequest(createForm.render(computerForm));
        }
       computerForm.get().save(); //HERE
        flash("success", "Computer " + computerForm.get().name + " has been created");
       return GO_HOME;
}

这里有我的对象声明:

@Entity 
@Table(name="COMPUTER")
@SequenceGenerator(name = "computer_seq", sequenceName = "computer_seq")
public class Computer extends Model {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    public Long id;

    @Constraints.Required
    @Column(name="name")
    public String name;

    @Column(name="introduced")
    public String introduced;

    @Column(name="discontinued")
    public String discontinued;

    @ManyToOne(cascade = CascadeType.MERGE)
    public Company company;

    /**
     * Generic query helper for entity Computer with id Long
     */
    public static Finder<Long,Computer> find = new Finder<Long,Computer>(Long.class, Computer.class); 

    /**
     * Return a page of computer
     *
     * @param page Page to display
     * @param pageSize Number of computers per page
     * @param sortBy Computer property used for sorting
     * @param order Sort order (either or asc or desc)
     * @param filter Filter applied on the name column
     */
    public static Page<Computer> page(int page, int pageSize, String sortBy, String order, String filter) {
        return 
            find.where()
                .ilike("name", "%" + filter + "%")
                .orderBy(sortBy + " " + order)
                .fetch("company")
                .findPagingList(pageSize)
                .getPage(page);
    }

}

编辑1: 我尝试过不同的GeneratedValue strategy,例如AUTOIDENTITY,但没有任何效果......

编辑2: 我尝试使用Sequence策略,但它也不起作用:

@SequenceGenerator(name = "computer_seq", sequenceName = "computer_seq", initialValue=1, allocationSize=100)
[...]
    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="computer_seq")
    public Long id;

编辑3: 在这里你有公司实体:

@Entity 
@Table(name="COMPANY")
@SequenceGenerator(name = "company_seq", sequenceName = "company_seq", initialValue=1, allocationSize=100)
public class Company extends Model {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    public Long id;

    @Constraints.Required
    @Column(name="name")
    public String name;

    /**
     * Generic query helper for entity Company with id Long
     */
    public static Model.Finder<Long,Company> find = new Model.Finder<Long,Company>(Long.class, Company.class);

    public static Map<String,String> options() {
        LinkedHashMap<String,String> options = new LinkedHashMap<String,String>();
        for(Company c: Company.find.orderBy("name").findList()) {
            options.put(c.id.toString(), c.name);
        }
        return options;
    }

}

我尝试使用SEQUENCE strategyAUTO strategy并且它也不起作用。

1 个答案:

答案 0 :(得分:0)

当您在同一张桌子上保存重复ID时会显示此错误

保存前检查id值。 @see http://www.techonthenet.com/oracle/errors/ora00001.php

我的英语中的sory。 :d

编辑:

  

唯一键约束违规(PLAY.PK_COMPANY)也尝试检查COMPANY实体的autoGenerate序列。

编辑:

  

将公司生成更改为@GeneratedValue(strategy = GenerationType.AUTO,generator =“company_seq”)或更改为@GeneratedValue(strategy = GenerationType.SEQUENCE,generator =“company_seq”)