Jpa坚持主键限制违规

时间:2015-02-16 10:21:15

标签: java jpa eclipselink

我在传递持久性方面遇到了一些问题。现在我尝试以更详细的方式描述我的问题。我有两个实体Row和Field。

    @Entity
public class Row implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @SequenceGenerator(name="ROW_IMPORT_ID_GENERATOR", sequenceName="SEQ_RIGA_IMPORT")
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="ROW_IMPORT_ID_GENERATOR")
    private Long id;

    //bi-directional many-to-one association to CampoImport
    @OneToMany(mappedBy="row", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<Field> fields;

    //Getters and setters method

    public void addField(Field fieldToAdd){

        if(fields==null)
            fields = new ArrayList<Fields>();

        fields.add(campoToAdd);
        fieldToAdd.setRow(this);

    }
}


@Entity
public class Field implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @SequenceGenerator(name="FIELD_IMPORT_ID_GENERATOR", sequenceName="SEQ_CAMPO_IMPORT")
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="FIELD_IMPORT_ID_GENERATOR")
    private Long id;

    private String description;

    //bi-directional many-to-one association to RigaImport
    @ManyToOne
    @JoinColumn(name="ID_ROW")
    private Row row;

    //Getters and setters method
}

这是制作序列的sql语句:

CREATE SEQUENCE SEQ_RIGA_IMPORT INCREMENT BY 1 START WITH 1 MAXVALUE 999999999999999999 MINVALUE 1;

CREATE SEQUENCE SEQ_CAMPO_IMPORT INCREMENT BY 1 START WITH 1 MAXVALUE 999999999999999999 MINVALUE 1;

现在,当我尝试运行以下代码片段时:

Row rowToSave = new Row(some parameters);
Field fieldToSave = new Field( some parameters);
rowToSave.addField(fieldToSave);
rowRepository.save(rowToSave);

我收到此错误:

Internal Exception: java.sql.SQLException: ORA-00001: violated    restriction of uniqueness (BILL.FIELD_PK)

Error Code: 1
Call: INSERT INTO FIELD (ID, DESCRIPTION, ID_ROW) VALUES (?, ?, ?)
bind => [3 parameters bound]

我在哪里做错了?

1 个答案:

答案 0 :(得分:0)

您的数据库序列对象的分配大小为1,而JPA默认为50,这将导致此问题。设置您的分配大小以匹配。见https://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Entities/Ids/SequenceGenerator