gpt4 book ai didi

hibernate - ORA-01400 : Cannot insert null into (TABLE. 列)( hibernate )

转载 作者:行者123 更新时间:2023-12-02 03:16:15 27 4
gpt4 key购买 nike

我正在使用 hibernate 4.3,oracle 11:

当我想插入一个员工时,(它与类别有一对多的关系(一个类别有很多员工)),首先我将一个类别插入到数据库中,然后我尝试将一个员工插入到数据库中并得到一个异常(exception),实体的代码是由hibernate生成的,所以我不知道出了什么问题,问题似乎是当我插入员工时Hibernate没有插入ID_CAT,我做错了什么? 我也试过从 hibernate 生成表,但给了我同样的错误,感谢您的关注,希望您能帮助我!(类别和员工都与实体“企业”相关,但这部分效果很好)

异常(exception):

    abr 27, 2016 11:11:58 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1400, SQLState: 23000
abr 27, 2016 11:11:58 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: ORA-01400: no se puede realizar una inserción NULL en ("WSPUSER"."TM_EMPLEADOS"."ID_CAT")

abr 27, 2016 11:11:58 AM org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release
INFO: HHH000010: On release of batch it still contained JDBC statements
Error org.hibernate.exception.ConstraintViolationException: could not execute statement
Exception in thread "main" org.hibernate.TransactionException: Transaction not successfully started
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:172)
at tws.hibernate.dao.GenericDAO.endTransaction(GenericDAO.java:59)
at tws.hibernate.dao.GenericDAO.insert(GenericDAO.java:88)
at TwsTestRunner.insertarEmpleado(TwsTestRunner.java:289)
at TwsTestRunner.main(TwsTestRunner.java:27)

实体“员工”:

@Entity
@Table(name = "TM_EMPLEADOS")
public class TmEmpleados implements java.io.Serializable {

private TmEmpleadosId id;
private TmEmpresas tmEmpresas;
private TmCategoria tmCategoria;
private String nombre;

public TmEmpleados() {
}

public TmEmpleados(TmEmpleadosId id,TmEmpresas tmEmpresas, TmCategoria tmCategoria,String nombre) {
this.id = id;
this.tmEmpresas = tmEmpresas;
this.tmCategoria = tmCategoria;
this.nombre = nombre;
}

@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name = "idEmple", column = @Column(name = "ID_EMPLE", nullable = false, length = 25)),
@AttributeOverride(name = "mand", column = @Column(name = "MAND", nullable = false, length = 3)),
@AttributeOverride(name = "idEmp", column = @Column(name = "ID_EMP", nullable = false, length = 6)) })
public TmEmpleadosId getId() {
return this.id;
}

public void setId(TmEmpleadosId id) {
this.id = id;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumns({
@JoinColumn(name = "ID_CAT", referencedColumnName = "ID_CAT", nullable = false, insertable = false, updatable = false),
@JoinColumn(name = "ID_EMP", referencedColumnName = "ID_EMP", nullable = false, insertable = false, updatable = false),
@JoinColumn(name = "MAND", referencedColumnName = "MAND", nullable = false, insertable = false, updatable = false) })
public TmCategoria getTmCategoria() {
return this.tmCategoria;
}

public void setTmCategoria(TmCategoria tmCategoria) {
this.tmCategoria = tmCategoria;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumns({
@JoinColumn(name = "ID_EMP", referencedColumnName = "ID_EMP", nullable = false, insertable = false, updatable = false),
@JoinColumn(name = "MAND", referencedColumnName = "MAND", nullable = false, insertable = false, updatable = false) })
public TmEmpresas getTmEmpresas() {
return this.tmEmpresas;
}

@Column(name = "NOMBRE", nullable = false, length = 50)
public String getNombre() {
return this.nombre;
}

public void setNombre(String nombre) {
this.nombre = nombre;
}
}

实体“类别”:

@Entity
@Table(name = "TM_CATEGORIA")
public class TmCategoria implements java.io.Serializable {

private TmCategoriaId id;
private String descripcion;
private Set<TmEmpleados> tmEmpleadoses = new HashSet<TmEmpleados>(0);

public TmCategoria() {
}

public TmCategoria(TmCategoriaId id, String descripcion) {
this.id = id;
this.descripcion = descripcion;
}

public TmCategoria(TmCategoriaId id, String descripcion,Set<TmEmpleados> tmEmpleadoses) {
this.id = id;
this.descripcion = descripcion;
this.tmEmpleadoses = tmEmpleadoses;
}

@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name = "idCat", column = @Column(name = "ID_CAT", nullable = false, length = 3)),
@AttributeOverride(name = "idEmp", column = @Column(name = "ID_EMP", nullable = false, length = 6)),
@AttributeOverride(name = "mand", column = @Column(name = "MAND", nullable = false, length = 3)) })
public TmCategoriaId getId() {
return this.id;
}

public void setId(TmCategoriaId id) {
this.id = id;
}

@Column(name = "DESCRIPCION", nullable = false, length = 50)
public String getDescripcion() {
return this.descripcion;
}

public void setDescripcion(String descripcion) {
this.descripcion = descripcion;
}

@OneToMany(fetch = FetchType.LAZY, targetEntity = TmEmpleados.class, mappedBy = "tmCategoria")
public Set<TmEmpleados> getTmEmpleadoses() {
return this.tmEmpleadoses;
}

public void setTmEmpleadoses(Set<TmEmpleados> tmEmpleadoses) {
this.tmEmpleadoses = tmEmpleadoses;
}

}

我编辑了 hibernate 配置以显示 sql 并且控制台显示了这个:

abr 28, 2016 10:55:39 AM org.hibernate.validator.internal.util.Version <clinit>
INFO: HV000001: Hibernate Validator 5.1.3.Final
Hibernate: select tmcategori_.ID_CAT, tmcategori_.ID_EMP, tmcategori_.MAND, tmcategori_.DESCRIPCION as DESCRIPCION4_6_ from WSPUSER.TM_CATEGORIA tmcategori_ where tmcategori_.ID_CAT=? and tmcategori_.ID_EMP=? and tmcategori_.MAND=?
Hibernate: select tmempresas_.ID_EMP, tmempresas_.MAND, tmempresas_.DESCRIPCION as DESCRIPCION5_25_ from WSPUSER.TM_EMPRESAS tmempresas_ where tmempresas_.ID_EMP=? and tmempresas_.MAND=?
Hibernate: insert into WSPUSER.TM_EMPLEADOS ( NOMBRE, ID_EMP, ID_EMPLE, MAND) values (?, ?, ?, ?)
abr 28, 2016 10:55:40 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1400, SQLState: 23000
abr 28, 2016 10:55:40 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: ORA-01400: no se puede realizar una inserción NULL en ("WSPUSER"."TM_EMPLEADOS"."ID_CAT")

abr 28, 2016 10:55:40 AM org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release
INFO: HHH000010: On release of batch it still contained JDBC statements
Exception in thread "main" org.hibernate.TransactionException: Transaction not successfully started
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:172)
at tws.hibernate.dao.GenericDAO.endTransaction(GenericDAO.java:59)
at tws.hibernate.dao.GenericDAO.insert(GenericDAO.java:88)

最佳答案

在我的例子中,解决方案是删除实体中的 insertable="false", updatable="false"。

关于hibernate - ORA-01400 : Cannot insert null into (TABLE. 列)( hibernate ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36889728/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com