2 qq 16151201 qq_16151201 于 2016.04.20 15:14 提问

hibernate简单的增加问题,对PL/sql不是很熟 1C
 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个回答

xiaoyao880609
xiaoyao880609   2016.04.20 16:18

insert
into
usertable
(username, telnum)
values
(?, ?)
很明显你sql拼接中就没有将user类中的uid insert进去,由于uid应该为主键所以报了无法将 NULL 插入 ("TEST"."USERTABLE"."U_ID")错误
请检查hibernate配置。

zaizaisheng
zaizaisheng   2016.04.20 16:17

uID你设置为自增长的主键了吗

xiaochaolovedan
xiaochaolovedan   2016.04.20 16:54

你set的时候是不是没有set全啊,里面是不是有NULL值啊,你看看你的映射对吗,检查一下。是不是你的telnum的值太小了,你的电话号,最好还是String类型吧

xiaochaolovedan
xiaochaolovedan   2016.04.20 16:56

你看你的主键和U_id 对应的对吗,

huaitianqi2016
huaitianqi2016   2016.04.20 18:05

insert
into
usertable
(username, telnum)
values
(?, ?)
where id = ? 要加上这个id吧

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!