0

I have a table employee relationship many to one with table department

CREATE TABLE `tbl_employee` (
  `employees_id` int(11) NOT NULL AUTO_INCREMENT,
  `employees_name` varchar(20) NOT NULL,
  `tbl_department_` int(11) DEFAULT NULL,
  PRIMARY KEY (`persion_id`)
)

When adding new data to the employee table, if I enter the value of tbl_department_id is null,it will appear error

I have a table employee relationship many to one with table department

CREATE TABLE `tbl_employee` (
  `employees_id` int(11) NOT NULL AUTO_INCREMENT,
  `employees_name` varchar(20) NOT NULL,
  `tbl_department_` int(11) DEFAULT NULL,
  PRIMARY KEY (`persion_id`)
)

When adding new data to the employee table, if I enter the value of tbl_department_id is null,it will appear error

public class NewClass {
    public static void main(String[] args) {
        SessionFactory sessionFactory = NewHibernateUtil.getSessionFactory();
        Session currentSession = sessionFactory.getCurrentSession();
        currentSession.getTransaction().begin();

        TblDepartment tblDepartment = new TblDepartment();
        tblDepartment.setDepartmentId(null);

        TblEmployee tblEmployee = new TblEmployee();
        tblEmployee.setEmployeeName("");
        tblEmployee.setTblDepartment(tblDepartment);

        currentSession.save(tblEmployee);
        currentSession.getTransaction().commit();
    }
}

Exception in thread "main" org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: sf.enforcement.mavenproject1.TblDepartment
    at org.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:294)
    at org.hibernate.type.EntityType.getIdentifier(EntityType.java:510)
    at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:174)
    at org.hibernate.engine.query.spi.NativeSQLQueryPlan.bindNamedParameters(NativeSQLQueryPlan.java:162)
    at org.hibernate.engine.query.spi.NativeSQLQueryPlan.performExecuteUpdate(NativeSQLQueryPlan.java:219)
    at org.hibernate.internal.SessionImpl.executeNativeUpdate(SessionImpl.java:1306)
    at org.hibernate.internal.SQLQueryImpl.executeUpdate(SQLQueryImpl.java:389)
    at sf.enforcement.mavenproject1.NewClass2.main(NewClass2.java:30)

I see a similar question handled by use CascadeType.ALL,CascadeType.SAVE_UPDATE

object references an unsaved transient instance - save the transient instance before flushing

I tried to follow, but it failed. CascadeType.SAVE_UPDATE does not exist in hibernate version 4.3.1

@ManyToOne(fetch=FetchType.LAZY,cascade = CascadeType.ALL)
    @JoinColumn(name="tbl_department")
    public TblDepartment getTblDepartment() {
        return this.tblDepartment;
    }

My POM

<dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.3.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>1.0.0.Final</version>
        </dependency>

How should I save null values in a table with foreign keys? Please help me

Touya Akira
  • 301
  • 4
  • 14

0 Answers0