首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

Hibernate入门事例-高分求解

2011-11-21 
Hibernate入门例子-高分求解packagedev.hibernateimportjava.io.Serializableimportnet.sf.hibernate.Se

Hibernate入门例子-高分求解
package   dev.hibernate;
import   java.io.Serializable;
import   net.sf.hibernate.SessionFactory;
import   net.sf.hibernate.Transaction;
import   net.sf.hibernate.cfg.*;
import   net.sf.hibernate.Session;
import   org.apache.commons.logging.Log;
import   org.apache.commons.logging.LogFactory;
public   class   HibernateUtil{

  public   static   void   main(String[]   args)   {
    try{
SessionFactory   sf   =   new   Configuration().configure().buildSessionFactory();
Session   session   =   sf.openSession();
Transaction   tx   =   session.beginTransaction();
   
    User   p     =   new   User();
    p.setUid(new   Integer(1));
    p.setUsername( "LDQ ");
    p.setPassword( "12345 ");
    session.save(p);
    tx.commit();
    session.close();
    }catch(Exception   ex){    
      throw   new   ExceptionInInitializerError(ex);
    }    
  }
}
编译没有问题,只要运行到【new   Configuration()】就发生错误啦~
搞了2天了就是不明白怎么才能在Eclipse中运行这个程序
---------------------------------
Exception   in   thread   "main "   java.lang.ExceptionInInitializerError
at   dev.hibernate.HibernateUtil.main(HibernateUtil.java:20)
Caused   by:   org.apache.commons.logging.LogConfigurationException:   org.apache.commons.logging.LogConfigurationException:   java.lang.NullPointerException   (Caused   by   java.lang.NullPointerException)   (Caused   by   org.apache.commons.logging.LogConfigurationException:   java.lang.NullPointerException   (Caused   by   java.lang.NullPointerException))
at   org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:543)
at   org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:235)
at   org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:209)
at   org.apache.commons.logging.LogFactory.getLog(LogFactory.java:351)
at   net.sf.hibernate.cfg.Configuration. <clinit> (Configuration.java:95)
...   1   more
Caused   by:   org.apache.commons.logging.LogConfigurationException:   java.lang.NullPointerException   (Caused   by   java.lang.NullPointerException)
at   org.apache.commons.logging.impl.LogFactoryImpl.getLogConstructor(LogFactoryImpl.java:397)
at   org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:529)
...   5   more
Caused   by:   java.lang.NullPointerException
at   org.apache.commons.logging.impl.LogFactoryImpl.getLogConstructor(LogFactoryImpl.java:374)
...   6   more


[解决办法]
你的配置文件呢

[解决办法]
需要入门例子,请留下邮件地址或发
yzh963@vip.sina.com跟我要,免费提供

[解决办法]
要把你的hibernate.cfg.xml放在src目录下,还有可能你少导了一些jar包!
[解决办法]
<id name= "uid " type= "integer ">
<column name= "uid " />
<generator class= "native " />


</id>

native 改成ass什么的

mysql好象不支持native
[解决办法]
你别这么写啊,把SessionFactory和Session对象都写到try外面,你要是写到try内,就没法用finally来结束session了~
[解决办法]
要不把你的代码 大包发给我 我帮你看看

QQ 15755898
[解决办法]
new Configuration()报错可能是没找到你的配置文件,你指定路径再试一下。
[解决办法]
to :zqpsswh(似水无痕) mysql好象不支持native

这与数据库无关,generator的值可以指定与具体数据库想匹配的,如oracle可以指定为 sequence,也可以指定为 "native " ,而native是最通用的,如果是native ,则hibernate会自动去匹配数据库的规则.
[解决办法]
我用的

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;


/**
* @author Administrator
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class HibernateUtil {

private static Log log=LogFactory.getLog(HibernateUtil.class);

private static final org.hibernate.SessionFactory sessionFactory;

static {
try{
sessionFactory=new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
log.error( "Initial SessionFactory creation failed. ",ex);
ex.printStackTrace();
AnyCareLog.anyCarelog.error(ex.getMessage());
throw new ExceptionInInitializerError(ex);
}
}

public static final ThreadLocal session=new ThreadLocal();

public static Session currentSession() throws HibernateException {
Session s=(Session)session.get();
//Open a new Session, if this Thread has none yet
if (s==null) {
s=sessionFactory.openSession();
session.set(s);
}
return s;
}

public static void closeSession() throws HibernateException {
Session s=(Session) session.get();
session.set(null);
if (s !=null)
s.close();
}

}





import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html}.
*/
public class HibernateSessionFactory {

/**
* Location of hibernate.cfg.xml file.
* NOTICE: Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file. That
* is place the config file in a Java package - the default location
* is the default Java package. <br> <br>
* Examples: <br>
* <code> CONFIG_FILE_LOCATION = "/hibernate.conf.xml ".
* CONFIG_FILE_LOCATION = "/com/foo/bar/myhiberstuff.conf.xml ". </code>
*/
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml ";

/** Holds a single instance of Session */
private static final ThreadLocal threadLocal = new ThreadLocal();

/** The single instance of hibernate configuration */
private static final Configuration cfg = new Configuration();

/** The single instance of hibernate SessionFactory */


private static org.hibernate.SessionFactory sessionFactory;

/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code> SessionFactory </code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocal.get();

if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory = cfg.buildSessionFactory();
} catch (Exception e) {
System.err
.println( "%%%% Error Creating SessionFactory %%%% ");
e.printStackTrace();
}
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}

return session;
}

/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);

if (session != null) {
session.close();
}
}

/**
* Default constructor.
*/
private HibernateSessionFactory() {
}

}

热点排行