外键值未插入数据库:无法在列中插入 Null
Foreign key value not inserting in database: Cannot insert Null in column
我正在将客户端应用程序从 Hibernate 迁移到 EclipseLink。但是在学生实体上调用 persist 方法时,它不会为外键插入值。数据库是oracle。地址和学生之间存在多对一关系。
同样在生成的 SQL 外键列不存在。
@Entity
@Table(name = "Student", schema = "Student_DB")
@SequenceGenerator(name = "studentSeq", sequenceName = "Student_SEQ", schema = "Student_DB", allocationSize = 1)
public class StudentEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "studentSeq")
@Column(name = "S_ID")
private BigDecimal studentID;
@OneToMany(mappedBy = "studentEntity", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "S_ID")
private List<AddressEntity> addresses;
}
addAddress(address){
address.setStudentEntity(this);
@Entity
@Table(name = "Address", schema = "Student_DB")
@SequenceGenerator(name = "AddressSeq", sequenceName = "Address_SEQ", schema = "Student_DB", allocationSize = 1)
public class AddressEntity implements Serializable {
@Id
@Column(name = "A_ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "AddressSeq")
private long addressID;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "S_ID", referencedColumnName = "S_ID", insertable = false, updatable = false, nullable = false)
private StudentEntity studentEntity;
}
*
> [EL Fine]: sql: 2019-06-25
> 18:39:00.895--ClientSession(1656550367)--Connection(-872205654)--INSERT
> INTO Student_DB.Student (S_ID, ...) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?,
> ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) bind => [24 parameters
> bound] [EL Fine]: sql: 2019-06-25
> 18:39:00.946--ClientSession(1656550367)--Connection(-872205654)--INSERT
> INTO Student_DB.Address (A_ID, CO...) VALUES (?, ?, ?, ?, ?) bind =>
> [5 parameters bound] [EL Fine]: sql: 2019-06-25
> 18:39:00.954--ClientSession(1656550367)--SELECT 1 FROM DUAL [EL
> Warning]: 2019-06-25 18:39:00.955--UnitOfWork(-922357549)--Exception
> [EclipseLink-4002] (Eclipse Persistence Services -
> 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DatabaseException Internal
> Exception: java.sql.SQLIntegrityConstraintViolationException:
> ORA-01400: cannot insert NULL into ("Student_DB"."Address"."S_ID")
*
正确的映射如下:
@Entity
@Table(name = "Student", schema = "Student_DB")
@SequenceGenerator(name = "studentSeq", sequenceName = "Student_SEQ", schema = "Student_DB", allocationSize = 1)
public class StudentEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "studentSeq")
@Column(name = "S_ID")
private BigDecimal studentID;
@OneToMany(mappedBy = "studentEntity", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<AddressEntity> addresses;
}
@Entity
@Table(name = "Address", schema = "Student_DB")
@SequenceGenerator(name = "AddressSeq", sequenceName = "Address_SEQ", schema = "Student_DB", allocationSize = 1)
public class AddressEntity implements Serializable {
@Id
@Column(name = "A_ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "AddressSeq")
private long addressID;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "S_ID", referencedColumnName = "S_ID", nullable = false)
private StudentEntity studentEntity;
}
我正在将客户端应用程序从 Hibernate 迁移到 EclipseLink。但是在学生实体上调用 persist 方法时,它不会为外键插入值。数据库是oracle。地址和学生之间存在多对一关系。
同样在生成的 SQL 外键列不存在。
@Entity
@Table(name = "Student", schema = "Student_DB")
@SequenceGenerator(name = "studentSeq", sequenceName = "Student_SEQ", schema = "Student_DB", allocationSize = 1)
public class StudentEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "studentSeq")
@Column(name = "S_ID")
private BigDecimal studentID;
@OneToMany(mappedBy = "studentEntity", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "S_ID")
private List<AddressEntity> addresses;
}
addAddress(address){
address.setStudentEntity(this);
@Entity
@Table(name = "Address", schema = "Student_DB")
@SequenceGenerator(name = "AddressSeq", sequenceName = "Address_SEQ", schema = "Student_DB", allocationSize = 1)
public class AddressEntity implements Serializable {
@Id
@Column(name = "A_ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "AddressSeq")
private long addressID;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "S_ID", referencedColumnName = "S_ID", insertable = false, updatable = false, nullable = false)
private StudentEntity studentEntity;
}
*
> [EL Fine]: sql: 2019-06-25
> 18:39:00.895--ClientSession(1656550367)--Connection(-872205654)--INSERT
> INTO Student_DB.Student (S_ID, ...) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?,
> ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) bind => [24 parameters
> bound] [EL Fine]: sql: 2019-06-25
> 18:39:00.946--ClientSession(1656550367)--Connection(-872205654)--INSERT
> INTO Student_DB.Address (A_ID, CO...) VALUES (?, ?, ?, ?, ?) bind =>
> [5 parameters bound] [EL Fine]: sql: 2019-06-25
> 18:39:00.954--ClientSession(1656550367)--SELECT 1 FROM DUAL [EL
> Warning]: 2019-06-25 18:39:00.955--UnitOfWork(-922357549)--Exception
> [EclipseLink-4002] (Eclipse Persistence Services -
> 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DatabaseException Internal
> Exception: java.sql.SQLIntegrityConstraintViolationException:
> ORA-01400: cannot insert NULL into ("Student_DB"."Address"."S_ID")
*
正确的映射如下:
@Entity
@Table(name = "Student", schema = "Student_DB")
@SequenceGenerator(name = "studentSeq", sequenceName = "Student_SEQ", schema = "Student_DB", allocationSize = 1)
public class StudentEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "studentSeq")
@Column(name = "S_ID")
private BigDecimal studentID;
@OneToMany(mappedBy = "studentEntity", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<AddressEntity> addresses;
}
@Entity
@Table(name = "Address", schema = "Student_DB")
@SequenceGenerator(name = "AddressSeq", sequenceName = "Address_SEQ", schema = "Student_DB", allocationSize = 1)
public class AddressEntity implements Serializable {
@Id
@Column(name = "A_ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "AddressSeq")
private long addressID;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "S_ID", referencedColumnName = "S_ID", nullable = false)
private StudentEntity studentEntity;
}