我尝试持久化一个与另一个子实体连接的父实体,但问题是在持久化时没有为这个子节点生成id,所以我有这个错误:title: This is the title
author:
- Author Juan$^1$
- Author Tu$^2$
institute:
- $^1$Juans Casa
- $^2$Tus Place
date: "Thursday, April 09, 2015"
output:
beamer_presentation
有子实体:
[org.hibernate.engine.jdbc.spi.SqlExceptionHelper] ORA-01400: cannot insert NULL into ("L2S$OWNER"."SABRI"."TRANSITION_MATRIX_ID")
PK课程:
@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;}
}
有父实体:
@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;
}
在这里,我尝试保留父表,该表也应该持久保存子表,但不生成Id!
@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;}
答案 0 :(得分:0)
假设您使用的是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类。之前对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
答案 1 :(得分:0)
感谢Alan Hay,我发现了问题,我将我的IDclass的属性TRANSITION_MATRIX_ID更改为sabriEntity,我删除了这个类的所有注释!
子实体
@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;
PK等级
@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;
}
}