qq_16151201
程序猿如何进阶成攻城狮
2016-04-20 07:14

hibernate简单的增加问题,对PL/sql不是很熟

1
  • hibernate
 package test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

import bean.user;
public class test1{
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
         SessionFactory sessionFactory = null;       
         Configuration configuration = new Configuration().configure();
         ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
                .buildServiceRegistry();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
            //2. Sesson
            Session session = sessionFactory.openSession();
            //3. 开启事务
            Transaction transaction = session.beginTransaction();
            //4. 执行保存操作

         user u=new user();
         u.setTelnum(1314465655);
         u.setU_id(124);
         u.setUsername("张三");
            session.save(u);
            //5. 提交事物
            transaction.commit();
            //6. 关闭Session
            session.close();
            //7. 关闭SessionFectory
            sessionFactory.close();
    }
}

结果为:
[bfs-sdk-java][2016-04-20 15:06:11:885][INFO][org.hibernate.tool.hbm2ddl.SchemaUpdate]HHH000232: Schema update complete
Hibernate:
insert
into
usertable
(username, telnum)
values
(?, ?)
[bfs-sdk-java][2016-04-20 15:06:12:031][WARN][org.hibernate.engine.jdbc.spi.SqlExceptionHelper]SQL Error: 1400, SQLState: 23000
[bfs-sdk-java][2016-04-20 15:06:12:031][ERROR][org.hibernate.engine.jdbc.spi.SqlExceptionHelper]ORA-01400: 无法将 NULL 插入 ("TEST"."USERTABLE"."U_ID")

Exception in thread "main" org.hibernate.exception.ConstraintViolationException: ORA-01400: 无法将 NULL 插入 ("TEST"."USERTABLE"."U_ID")

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 com.sun.proxy.$Proxy3.executeUpdate(Unknown Source)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:96)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2763)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3274)
at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362)
at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:203)
at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:183)
at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:167)
at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:320)
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:287)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:193)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:126)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:204)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:189)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:724)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:716)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:712)
at test.test1.main(test1.java:31)

表结构为:
图片说明

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

5条回答

为你推荐

换一换