public static void main(String[] args) { 
// TODO Auto-generated method stub 
Integer primaryKey = createHoney(); 
System.out.println("primary key is " + primaryKey); 

在Main函数中打印出来的primaryKey是NUll,不知道为什么。我的表的主键用的是Number型的,开始的时候再Hbm.xml中我用的是sequence并且在数据库中也创建了sequence同样的结果。我后来用了uuid,native也是不可以。请高手帮忙解决。

解决方案 »

  1.   

    把你test类中的Integer 修改成Long就ok了
      

  2.   

    注意数据类型/** 
        * @param args 
        */ 
        public static void main(String[] args) { 
    //     TODO Auto-generated method stub 
        Long primaryKey = createHoney(); 
        System.out.println("primary key is " + primaryKey); 
        }     private static Long createHoney() { 
        Session session = null; 
        Transaction tx = null; 
        Logger log = Logger.getLogger("TestClient");     log.info("creating honey"); //     [laliluna] our returned primary key 
        Long id = null;     try { 
    //     [laliluna] get the session from the factory 
        session = HibernateUtil.currentSession(); //     [laliluna] always start a transaction before doing something 
    //     (even reading) from the database 
        tx = session.beginTransaction(); 
    //     [laliluna] create a new object 
        Userlist userList = new Userlist(); 
        userList.setUsername("123"); 
        userList.setUserpwd("123"); //     [laliluna] save it to the database, Hibernate returns your object 
    //     with the primary key field updated! 
        id = (Long) session.save(userList); //     [laliluna] commit your transaction or nothing is wrote to the db 
        tx.commit(); //     [laliluna] clean up (close the session) 
        session.close();     } catch (HibernateException e) { //     [laliluna] when an error occured, try to rollback your 
    //     transaction 
        if (tx != null) 
        try { 
        tx.rollback(); 
        } catch (HibernateException e1) { 
        log.warn("rollback not successful"); 
        } 
        /* 
        * [laliluna] close your session after an exception!! Your session 
        * is in an undefined and unstable situation so throw it away! 
        * 
        */ 
        if (session != null) 
        try { 
        session.close(); 
        } catch (HibernateException e2) { 
        log.warn("session close not successful"); 
        } 
        }     return id; 
        }
      

  3.   

    谢谢lovechineseboy(蓝太阳) 的解答,但是问题还是没有解决,Hbm.xml文件需要改动吗?可以具体点吗!!谢谢!!!
      

  4.   

    其它文件都不需要修改  当然包括hbm.xml文件!!