0

我正在使用帮助程序类并尝试将一些数据插入到数据库表中。我有一个具有onetomany关系的自我加入课程。

插入事务的主类

          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("do@msn.com");
            checker.setName("Doris");
            checker.setPassword("890");
            checker.setUsername("dr89");

            // Create new staff
            Staff staff1 = new Staff();
            staff1.setStaffId("m6");
        staff1.setEmail("mr@msn.com");
            staff1.setName("Marius");
            staff1.setPassword("234");
            staff1.setUsername("mr23");


            Staff staff2 = new Staff();
            staff2.setStaffId("b8");
        staff2.setEmail("be@msn.com");
            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();
        } 

}

数据库

     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`));

在我运行代码后,它在密码列出现了约束冲突。

stacktrace中的查询和警告:

     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)

是的,我将表中的密码设置为不为空,并且添加了一些数据放在主类中,所以它不应该为空。所以我不确定为什么会出错。

4

1 回答 1

0

错字;

Staff staff2 = new Staff();
staff2.setStaffId("b8");
staff2.setEmail("be@msn.com");
staff2.setName("Betty");
staff1.setPassword("567");    << should be staff2
staff1.setUsername("be56");   << should be staff2

由于从未设置过密码staff2,因此插入(正确)失败。

于 2013-05-01T17:27:56.553 回答