0 Replies Latest reply on Aug 17, 2011 7:53 AM by Joseph Hwang

    Field 'record_EMP_ID' doesn't have a default value

    Joseph Hwang Novice

      I solved org.hibernate.TransientObjectException by adding "@OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL)"

      But this time values can't be inserted to DB.

       

      OS : Windows 7 

      JDK : JDK 1.6
      JBoss : JBoss 5.1.0.GA
      DB : MySQL 5.5

       

       

       

      Error is "Field 'record_EMP_ID' doesn't have a default value"

       


       

      - Members.java

       

       

      @Entity
      @Table(name = "family")
      public class Members implements Serializable {
        @Id
        @Column(name = "EMP_ID")
        private String id ;

       

        @Column(name = "EMP_Passwd")
        private String passwd ;

       

        @Column(name = "EMP_Name")
        private String name ;

        @OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL)
        @JoinTable(name="info", joinColumns = {@JoinColumn(name = "EMP_Name")})
        private Collection<Records> record = new ArrayList();

         ....getter and setter

      }

       

       

      - Records.java

       

      @Entity
      @Table(name = "info")
      public class Records implements Serializable {

        @Id
        @GeneratedValue(strategy=GenerationType.AUTO)
        @Column(name = "EMP_ID")
        private int i;

        @Column(name = "EMP_Name")
        private String name;

       

        @Column(name = "EMP_Bank_Account")
        private String account;

       

        @Column(name = "EMP_Hobby")
        private String hobby;

       

        @Column(name = "EMP_Phone")
        private int phone;

        ...getter and setter method

      }

       

       

      in jsp file I invoke EntityManager.persist

       

       

       

      Context ctx = new InitialContext();
      IMappingTestPort port = (IMappingTestPort) ctx.lookup("MappingTestBean/remote");

      Records r = new Records();
      r.setName("aaa");
      r.setAccount("111-2222");
      r.setHobby("bbb");
      r.setPhone(021112222);

      Members m = new Members();
      m.setId("aupres");
      m.setPasswd("aaa");
      m.setName("ccc");
      m.getRecord().add(r);

      port.setFamilyMember(m); // This EntityManager.persist method

       

       

      But it failed.


      In console

      20:21:29,233 INFO [STDOUT] Hibernate: insert into family (EMP_Name, EMP_Passwd, EMP_ID) values (?, ?, ?)
      20:21:29,268 INFO [STDOUT] Hibernate: insert into info (EMP_Bank_Account, EMP_Hobby, EMP_Name, EMP_Phone) values (?, ?, ?, ?)
      20:21:29,288 WARN [JDBCExceptionReporter] SQL Error: 1364, SQLState: HY000
      20:21:29,288 ERROR [JDBCExceptionReporter] Field 'record_EMP_ID' doesn't have a default value

       

      record_EMP_ID is automatically generated by hibernate

       

      Pls help!