我正在使用hibernate 4.3,oracle 11:
当我想插入一个员工时,(它与一个类别有一个关系(一个类别有很多员工)),首先我将一个类别插入到db,然后我尝试将一个员工插入到db中并获取例外,实体的代码是由hibernate生成的,所以我不知道什么是错的,问题似乎是Hibernate在我插入时没有插入ID_CAT员工,我做错了什么?我也尝试从hibernate生成表但是给了我同样的错误,感谢您的关注,我希望您能帮助我! (类别和员工都与实体相关"企业",但这部分运作良好)
例外:
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"员工":
@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" CATEGORY":
@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)
答案 0 :(得分:0)
你确定你已经填充了字段&#34; nombre&#34;对于所有员工?
您将获得ConstraintViolationException,并根据您的代码,&#34; nombre&#34;字段不应为null(nullable = false)。
答案 1 :(得分:0)
我解决了修改数据库的问题:我把所有外键列都作为引用表的主键的一部分,子列也是相关的,所以现在hibernate正确生成sql,也许不是最干净的方式但是有效。< / p>
答案 2 :(得分:0)
在我的情况下,解决方法是删除实体中的insertable =“ false”和updateble =“ false”。