简体   繁体   English

未插入数据库的外键值:无法在列中插入Null

[英]Foreign key value not inserting in database: Cannot insert Null in column

I am migrating client application from Hibernate to EclipseLink. 我正在将客户端应用程序从Hibernate迁移到EclipseLink。 But it is not inserting value for foreign key when calling persist method on student entity. 但是当在学生实体上调用persist方法时,它不会为外键插入值。 Database is oracle. 数据库是oracle。 There is Many to one relation between Address and Student. 地址与学生之间存在多对一的关系。

Also in generated SQL the foreign key column is not present. 同样在生成的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")

* *

The correct mapping would look like: 正确的映射看起来像:

@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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Doctrine - 不保存外键(值为空) - Doctrine - not saving foreign key (value is null) EF - 对于已创建的实体:不能将值NULL插入列&#39;Id&#39;,表&#39;Languages&#39;; 列不允许空值 - EF - For an already created entity: Cannot insert the value NULL into column 'Id', table 'Languages'; column does not allow nulls FuelPHP在尝试插入相关模型的外键上获得“非空违反” - FuelPHP Get “Not null violation” on foreign key trying to insert into related model 为什么Hibernate在没有插入子行的情况下插入带有外键的父行? - Why does Hibernate insert a parent row with a foreign key without inserting the child row? JPA插入失败,因为我的外键值为零 - JPA Insert failed because of a zero value in my foreign key 如何在SQLAlchemy中指定外键列值? - How do you specify the foreign key column value in SQLAlchemy? Hibernate未在插入时设置外键 - Hibernate not setting foreign key on insert 插入具有外键约束的冲突 - Insert conflict with foreign key constraint 复合主键,使用@IdClass - 列“id”不能是 null - Composite Primary key, using @IdClass - Column 'id' cannot be null 一个映射实体中的外键为空 - Foreign key is null in one mapping entity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM