2013-05-01 148 views
0

我正在使用助手類,並試圖將一些數據插入數據庫表。我有一個自我加入類與onetomany關係。SQL休眠錯誤

的插入事務的主類

  public static void main(String[] args) { 
    // TODO Auto-generated method stub 


    Session session = HibernateUtil.getSessionFactory().openSession(); 
     Transaction transaction = null; 
     try { 
      transaction = session.beginTransaction(); 

      // Create new checker 
      Staff checker = new Staff(); 
      checker.setStaffId("d8"); 
      checker.setEmail("[email protected]"); 
      checker.setName("Doris"); 
      checker.setPassword("890"); 
      checker.setUsername("dr89"); 

      // Create new staff 
      Staff staff1 = new Staff(); 
      staff1.setStaffId("m6"); 
     staff1.setEmail("[email protected]"); 
      staff1.setName("Marius"); 
      staff1.setPassword("234"); 
      staff1.setUsername("mr23"); 


      Staff staff2 = new Staff(); 
      staff2.setStaffId("b8"); 
     staff2.setEmail("[email protected]"); 
      staff2.setName("Betty"); 
      staff1.setPassword("567"); 
      staff1.setUsername("be56"); 

      Set<Staff> staff = new HashSet<Staff>(); 
      staff.add(staff1); 
      staff.add(staff2); 

      staff1.setChecker(checker); 
      staff2.setChecker(checker); 

       checker.setSetters(staff); 

      session.save(checker); 

      session.save(staff1); 
      session.save(staff2); 
      transaction.commit(); 

     }catch (HibernateException e) { 
      transaction.rollback(); 
      e.printStackTrace(); 
     } finally { 
      session.close(); 
     } 

} 

的DB

 CREATE TABLE `staff` (
`staff_id` VARCHAR(255) NOT NULL, 
`name` VARCHAR(255) NULL DEFAULT NULL, 
`username` VARCHAR(255) NOT NULL, 
`password` VARCHAR(255) NOT NULL, 
    `email` VARCHAR(255) NULL DEFAULT NULL, 
    `checker_id` VARCHAR(255) NULL DEFAULT NULL, 
    PRIMARY KEY (`staff_id`), 
    FOREIGN KEY (`checker_id`) REFERENCES `staff` (`staff_id`)); 

後,我跑的代碼,它想出了一個約束衝突在列密碼。

在堆棧跟蹤查詢和警告:

 Hibernate: select staff_.staff_id, staff_.checker_id as checker6_2_, staff_.email 

    as email2_, staff_.name as name2_, staff_.password as password2_, staff_.username as 

    username2_ from staff staff_ where staff_.staff_id=? 


    Hibernate: select staff_.staff_id, staff_.checker_id as checker6_2_, staff_.email as 

    email2_, staff_.name as name2_, staff_.password as password2_, staff_.username as 

    username2_ from staff staff_ where staff_.staff_id=? 


    Hibernate: insert into staff (checker_id, email, name, password, username, staff_id) 

    values (?, ?, ?, ?, ?, ?) 

    Hibernate: insert into staff (checker_id, email, name, password, username, staff_id) 

values (?, ?, ?, ?, ?, ?) 
    Hibernate: insert into staff (checker_id, email, name, password, username, 

    staff_id)  
    values (?, ?, ?, ?, ?, ?) 
    WARN : org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 1048, SQLState: 

    23000 
    ERROR: org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Column 'password' cannot 

    be null 

完整的錯誤

 org.hibernate.exception.ConstraintViolationException: Column 'password' cannot be null 
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:74) 
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49) 
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125) 
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110) 
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129) 
at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81) 
at $Proxy16.executeUpdate(Unknown Source) 
at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:56) 
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3028) 
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3469) 
at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:88) 
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362) 
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:354) 
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:275) 
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:326) 
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52) 
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1213) 
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:402) 
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101) 
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175) 
at com.project.professional.StaffRelationshipTest.main(StaffRelationshipTest.java:75) 

是的,我在表中設定的密碼不爲空,我也加入了一些數據放它在主類中,所以它不應該爲null。所以我不知道爲什麼錯誤。

回答

0

錯字;

Staff staff2 = new Staff(); 
staff2.setStaffId("b8"); 
staff2.setEmail("[email protected]"); 
staff2.setName("Betty"); 
staff1.setPassword("567"); << should be staff2 
staff1.setUsername("be56"); << should be staff2 

由於密碼是從來沒有設置staff2,刀片(這是正確的)失敗。

+0

我這樣的疏忽和愚蠢的錯誤。謝謝。 – user2259555 2013-05-01 17:40:46