持久化 2 table 具有相同生成的 id

Persisting 2 table with the same generated id

我尝试保留一个与另一个 child 实体连接的 parent 实体,但问题是在保留时不会为此 child 生成 ID,所以我有此错误:[org.hibernate.engine.jdbc.spi.SqlExceptionHelper] ORA-01400: cannot insert NULL into ("L2S$OWNER"."SABRI"."TRANSITION_MATRIX_ID")

存在 child 实体:

@Data
@Entity
@IdClass(MyLibrarySabriEntityPK.class)
@Table(name = "SABRI", schema = "L2S$OWNER", catalog = "")

public class MyLibrarySabriEntity extends ActionForm {
@Access(AccessType.FIELD)
@Id
@ManyToOne
@JoinColumn(name = "TRANSITION_MATRIX_ID", referencedColumnName = "ID_TRANSITION_MATRIX")
private MyLibraryTestEntity sabriEntity;

@Id
private String RATING_ID_ROW;

@Id
private String RATING_ID_COL;


@Basic
@Column(name = "TRANSITION_PROBABILITY", nullable = true, insertable = true, updatable = true, precision = 20)
private Double TRANSITION_PROBABILITY;}

PK class :

@Data
public class MyLibrarySabriEntityPK implements Serializable {
private String TRANSITION_MATRIX_ID;
private String RATING_ID_ROW;
private String RATING_ID_COL;


public MyLibrarySabriEntityPK(String TRANSITION_MATRIX_ID,String RATING_ID_COL,String RATING_ID_ROW ){
    this.TRANSITION_MATRIX_ID=TRANSITION_MATRIX_ID;
    this.RATING_ID_COL = RATING_ID_COL;
    this.RATING_ID_ROW= RATING_ID_ROW;
}

}

有 parent 实体:

@Data
@Entity
@Table(name = "TEST", schema = "L2S$OWNER", catalog = "")
public class MyLibraryTestEntity extends ActionForm {

    @Access(AccessType.FIELD)
    @OneToMany(mappedBy = "sabriEntity", cascade = CascadeType.PERSIST)
    private final List<MyLibrarySabriEntity> entities = new ArrayList<MyLibrarySabriEntity>(25);

    public void addEntitysabri(MyLibrarySabriEntity entity) {
        getEntities().add(entity);
        entity.setSabriEntity(this);
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "IdGenerated")
    @GenericGenerator(name = "IdGenerated", strategy = "dao.Identifier")
    @Column(name = "ID_TRANSITION_MATRIX", nullable = false, insertable = false, updatable = false, length = 10)
    private String ID_TRANSITION_MATRIX;


    @Basic
    @Column(name = "REFERENCE", nullable = true, insertable = true, updatable = true, precision = 0)
    private Integer reference;}

在这里我尝试保留 parent table 应该也保留 child table 但是没有生成 Id !

MyLibrarySabriEntity Entity = null;
MyLibraryTestEntity test = getMyLibraryTestEntity(matrixStartDate, matrixName);  // here I get the values of my entity test (parent)
    try {
        transaction.begin();
        for (int row = 0; row < 20; row++) {
            for (int col = 0; col < 20; col++) {
                double val = cells.get(row + FIRST_ROW, col + FIRST_COL).getDoubleValue();
                Entity = getMyLibrarySabriEntity(col, row, val); // this get the values of the Entity parameters (child)
                Entity.setSabriEntity(test);
                test.addEntitysabri(Entity);
                em.persist(test);
            }
        }


    } catch (Exception e) {
        if (transaction.isActive())
            transaction.rollback();
        LOGGER.warn(e.getMessage(), e);

    } finally {
        if (transaction.isActive())
            transaction.commit();
        em.close();

    }

假设您使用的是 JPA 2.0+

完全删除此映射:

@Id
@Column(name = "TRANSITION_MATRIX_ID", nullable = false, 
       insertable = true, updatable = true, length = 100)
private String TRANSITION_MATRIX_ID;

并将@Id直接放在ManyToOne上,删除可插入和可更新的属性。

@Access(AccessType.FIELD)
@Id
@ManyToOne
@JoinColumn(name = "TRANSITION_MATRIX_ID", referencedColumnName = "ID_TRANSITION_MATRIX")
private MyLibraryTestEntity sabriEntity;

相应地更新您的 ID class。任何先前对 TRANSITION_MATRIX_ID 的引用都应替换为对 sabriEntity 的引用。您还混淆了@EmbeddedId 和@IdClass:只有前者会包含列定义,而您使用的是后一种方法。

public class MyLibrarySabriEntityPK implements Serializable {

    private String sabriEntity;
    private String RATING_ID_ROW;
    private String RATING_ID_COL;
}

参见:

https://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#JPA_2.0

感谢 Alan Hay,我发现了问题,我将我的 IDclass 的 属性 TRANSITION_MATRIX_ID 更改为 sabriEntity 并删除了此 [=26= 的所有注释] !

子实体

@Data
@Entity
@IdClass(MyLibrarySabriEntityPK.class)
@Table(name = "SABRI", schema = "L2S$OWNER", catalog = "")
public class MyLibrarySabriEntity extends ActionForm {

@Access(AccessType.FIELD)
@ManyToOne
@Id
@JoinColumn(name = "TRANSITION_MATRIX_ID", referencedColumnName = "ID_TRANSITION_MATRIX")
private MyLibraryTestEntity sabriEntity;


@Id
private String RATING_ID_ROW;

@Id
private String RATING_ID_COL;


@Basic
@Column(name = "TRANSITION_PROBABILITY", nullable = true, insertable = true, updatable = true, precision = 20)
private Double TRANSITION_PROBABILITY;

父实体

@Data
@Entity
@Table(name = "TEST", schema = "L2S$OWNER", catalog = "")
public class MyLibraryTestEntity extends ActionForm {

@Access(AccessType.FIELD)
@OneToMany(mappedBy = "sabriEntity", cascade = CascadeType.PERSIST)
private final List<MyLibrarySabriEntity> entities = new ArrayList<MyLibrarySabriEntity>(25);

public void addEntitysabri(MyLibrarySabriEntity entity) {
    getEntities().add(entity);
    entity.setSabriEntity(this);
}

@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "IdGenerated")
@GenericGenerator(name = "IdGenerated", strategy = "dao.Identifier")
@Column(name = "ID_TRANSITION_MATRIX", nullable = false, insertable = false, updatable = false, length = 10)
private String ID_TRANSITION_MATRIX;


@Basic
@Column(name = "REFERENCE", nullable = true, insertable = true, updatable = true, precision = 0)
private Integer reference;

PKClass

@Data
public class MyLibrarySabriEntityPK implements Serializable {
private MyLibraryTestEntity sabriEntity;
private String RATING_ID_ROW;
private String RATING_ID_COL;

public MyLibrarySabriEntityPK() {
}

public MyLibrarySabriEntityPK(MyLibraryTestEntity sabriEntity,String RATING_ID_COL,String RATING_ID_ROW ){
this.sabriEntity=sabriEntity;
this.RATING_ID_COL = RATING_ID_COL;
this.RATING_ID_ROW= RATING_ID_ROW;

}

}