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

Orm 未在数据库中插入外键值:无法在列中插入Null,orm,eclipselink,websphere-liberty,bidirectional-relation,Orm,Eclipselink,Websphere Liberty,Bidirectional Relation,我正在将客户端应用程序从Hibernate迁移到EclipseLink。但在对学生实体调用persist方法时,它并没有为外键插入值。数据库是oracle。地址和学生之间存在多对一的关系 在生成的SQL中,外键列也不存在 * *正确的映射如下所示: @Entity @Table(name = "Student", schema = "Student_DB") @SequenceGenerator(name = "studentSeq", sequenceName = "Student_SEQ",

我正在将客户端应用程序从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)
    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;
}

它不是精确的代码,但类似于你有私人列表地址;使用OneToManymappedBy=studentEntity映射,并指定@JoinColumnname=S_ID注释。MappedBy用于指示另一方控制外键(定义和字段),而JoinColumn与此冲突,用于单向映射。不能两者都是,因此您在部署期间忽略了一些警告,并且由于AddressEntity.studentEntity引用是只读的,因此未设置该字段。嗨,Chris,谢谢您的输入,我尝试了两个选项,第一个是删除@join column,第二个是从student类中删除mappedby,但结果是相同的错误代码:1400 Call:INSERT INTO student_DB.Address S_ID,DGFH,SGFFD,GHJG,GHJGH VALUES?bind=>[5个参数绑定]在insert语句中,它还应该包括S_ID,但我不知道为什么这里缺少它:如果您保留insertable=false、updatable=false,但删除了join列,则表示从未从该映射中设置外键。如果保留joinColumn注释,Eclipselink将在初始插入后在单独的语句中更新外键。您不能使用联接列单向映射,并且对EclipseLink具有NOTNULL约束。嗨,Simon,谢谢!!!谢谢你的努力。建议的更改是有效的,但只是一个问题,因为它是客户端的现有代码,只是想知道地址实体中insertable=false、updateable=false的目的是什么。正如我所知,它们应该在那里,这样我的地址实体就不会创建或更新student,或者我对这个概念的理解有误吗?它们表明此列是只读的。出了什么问题。我很乐意帮忙。如果你能接受我的回答,我将不胜感激
@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;
}