Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ORA-01400: Cannot insert null into (TABLE.COLUMN) (Hibernate)

I'm using hibernate 4.3, oracle 11:

When I want to insert an employee, (it have a relationship one to many with category (one category have many employees)), first I Insert a category to db, then I try to insert an employee to the db and get an exception, the code of the entities was generated by hibernate, so I don't know what's wrong, The problem seems to be that Hibernate is not inserting ID_CAT when I'm inserting an employee, what im doing wrong? I also tried to generate tables from hibernate but gives me the same error, thanks for your attention and I hope you can help me! (category and employees are both related with entity "enterprise", but this part works well)

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 "EMPLOYEES":

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

}

I edited hibernate config to show sql and console showed this:

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)
like image 390
MikeOx Avatar asked Jul 15 '26 23:07

MikeOx


1 Answers

In my case, the solucion was removing insertable="false", updateble="false" in the entity.

like image 180
Carlos UCR Avatar answered Jul 18 '26 14:07

Carlos UCR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!