zoukankan      html  css  js  c++  java
  • 出现一个错误

    package com.hanqi.test;
    import java.util.Date;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
    import org.hibernate.cfg.Configuration;
    import org.hibernate.service.ServiceRegistry;
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    
    import com.hanqi.entity.User;
    
    public class Test01 {
        private SessionFactory sf;
        private Session se;
        private Transaction ts;
        //在测试用例方法被执行之前自动执行方法
        //一般用来初始化公用的对象
        @Before
        public void  init()
        {
            //1获取配置文件
            Configuration cfg=new Configuration().configure();
                    
            //2注册配置
            ServiceRegistry sr=new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
                        
            //3获取SessionFactory(相当于JDBC)
            sf =cfg.buildSessionFactory(sr);        
            System.out.println(sf);
                    
            //4产生session  生成会话
            se=sf.openSession();
                    
            //5启动事物
            ts=se.beginTransaction();
        }
        //后知方法
        @After
        public void detory()
        {        
            //7提交事务
            ts.commit();
            
            //8释放资源
            se.close();        
            sf.close();
        }
        //测试Hibernate连接数据库
        
        @Test
        public void test() 
        {
                    
            //6操作数据库
            //(1)添加数据
            //实例化的新对象,处于临时状态
            User u1=new User();
            u1.setBirthday(new Date());
            u1.setMoney(2000);
            u1.setPassword("123456");
            u1.setUserName("恩恩");
            //(2)保存数据
            //通过save方法吧对象从临时状态转成持久化状态
            se.save(u1).toString();
        }
        @Test
        public void test1()
        {    
            //查询数据
            //提供两个参数
            //1需要返回哪一个持久化类的实例
            //2实例的标识(数据的主键值)
            //通过Session的get方法获得的对象处于持久化状态
            User u2=(User)se.get(User.class, 1);
            
            //修改
            u2.setUserName("庚衣");
            
            System.out.println(u2);
            
            //删除
            se.delete(u2);        //持久化对象进入删除状态
        }
        @Test
        public void test02()
        {
            User u1 =(User)se.get(User.class, 4);    //get立即加载
            System.out.println("get后");
            System.out.println(u1);
        }
        
        @Test
        public void test03()
        {
            User u1 =(User)se.load(User.class, 4);   //load延迟加载     懒加载
            System.out.println("load后");
            System.out.println(u1);                     //当使用是再加载
        }
        //测试游离状态
        @Test
        public void Test04()
        {
            //得到持久化状态
            User u1 =(User)se.load(User.class, 7);
            System.out.println(u1);
            //关闭Session
            se.close();
            //使u1进入游离状态
            u1.setUserName("李白");
            //把OID换成null
            u1.setUserID(null);
            System.out.println("重新设置Session事物");
            //从新创建session
            se=sf.openSession();
            //重新开启事物
            ts=se.beginTransaction();
            //
            se.saveOrUpdate(u1);
            //使用save
            //se.update(u1);
            System.out.println(u1);
        }
    }
    package com.hanqi.entity;
    //持久化类
    
    import java.util.Date;
    
    public class User 
    {
        private Integer userID;    
        private String userName;
        private Date birthday;
        private double money;
        private String password;
        
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        public Integer getUserID() {
            return userID;
        }
        public void setUserID(Integer userID) {
            this.userID = userID;
        }
        public String getUserName() {
            return userName;
        }
        public void setUserName(String userName) {
            this.userName = userName;
        }
        public Date getBirthday() {
            return birthday;
        }
        public void setBirthday(Date birthday) {
            this.birthday = birthday;
        }
        public double getMoney() {
            return money;
        }
        public void setMoney(double money) {
            this.money = money;
        }
        public User() {
            super();
        }
        public User(Integer userID, String userName, Date birthday, double money) {
            super();
            this.userID = userID;
            this.userName = userName;
            this.birthday = birthday;
            this.money = money;
        }
        @Override
        public String toString() {
            return "User [userID=" + userID + ", userName=" + userName + ", birthday=" + birthday + ", money=" + money
                    + ", password=" + password + "]";
        }    
    }
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                                             "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
     <session-factory >
      <!-- 数据库连接 -->
      <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
      <property name="hibernate.connection.password">123</property>
      <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
      <property name="hibernate.connection.username">test0816</property>
      <!-- 数据库方案 -->
      <property name="hibernate.default_schema">TEST0816</property>
      <!-- 数据库方言 -->
      <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
      <!-- 调试 -->
      <property name="hibernate.show_sql">true</property>
      <property name="hibernate.format_sql">true</property>
      <!-- 自动见表方法 -->
      <property name="hibernate.hbm2ddl.auto">update</property>
      <!-- 映射文件-->
      <mapping resource="com/hanqi/entity/User.hbm.xml"/>
     </session-factory>
    </hibernate-configuration>
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <!-- Generated 2016-11-7 14:41:21 by Hibernate Tools 3.4.0.CR1 -->
    <hibernate-mapping>
        <class name="com.hanqi.entity.User" table="T_USER">
            <id name="userID" type="java.lang.Integer">
                <column name="USERID" />
                <generator class="native" />
            </id>
            <property name="userName" type="java.lang.String">
                <column name="USERNAME" length="20" not-null="true" unique="true"/>
            </property>
            <property name="birthday" type="java.util.Date">
                <column name="BIRTHDAY" />
            </property>
            <property name="money" type="double">
                <column name="MONEY" length="8" scale="2" sql-type="NUMBER" default="0"/>
            </property>
            <property name="password" type="java.lang.String">
                    <column name="PASSWORD"  length="10"/>
               </property>
        </class>
    </hibernate-mapping>

    当在测试test04时出现错误了!!!!!!!!!!!!!!!!!1

    十一月 08, 2016 4:28:58 下午 org.hibernate.AssertionFailure <init>
    ERROR: HHH000099: an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session): org.hibernate.AssertionFailure: null identifier
    十一月 08, 2016 4:28:58 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
    INFO: HHH000030: Cleaning up connection pool [jdbc:oracle:thin:@localhost:1521:orcl]

  • 相关阅读:
    数据结构与算法习题总结——树结构
    SQL入门题集及学习笔记
    nlp入门系列笔记——阿里天池新闻文本新手赛
    linux一步一脚印--- ls -l 命令执行显示结果的每一列含义
    Python tuple元组---学习总结
    Python——列表深浅拷贝
    Python list列表---学习总结
    linux一步一脚印---mv命令
    linux一步一脚印---rm命令
    linux一步一脚印---cp命令
  • 原文地址:https://www.cnblogs.com/Levi1995/p/6043547.html
Copyright © 2011-2022 走看看