关于Hibernate4 中SessionFactory 空指针异常,求大神解惑

日月_年 2015-05-10 09:16:22
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">1991</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.min_size">1</property>
<property name="hibernate.c3p0.timeout">5000</property>
<property name="hibernate.c3p0.max_statements">100</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<property name="hibernate.c3p0.acquire_increment">2</property>
<property name="hibernate.c3p0.validate">true</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<mapping class="org.zmf.hibernate.entity.ReAddress"/>
<mapping class="org.zmf.hibernate.entity.Users"/>
<mapping class="org.zmf.hibernate.entity.News"/>
</session-factory>










Configuration conf = new Configuration().configure();
StandardServiceRegistryBuilder srb = new StandardServiceRegistryBuilder()
.applySettings(conf.getProperties());
ServiceRegistry sr = srb.build();
// 以Configuration实例创建SessionFactory实例
SessionFactory sf = conf.buildSessionFactory(sr);


每次执行到这就停了,实在是找不到原因了
...全文
411 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
另一花生 2015-08-17
  • 打赏
  • 举报
回复
也是同样问题
日月_年 2015-05-12
  • 打赏
  • 举报
回复
import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity(name="readdress") public class ReAddress{ @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Integer id; private String rename; public ReAddress() { } public ReAddress(String rename) { this.rename = rename; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getRename() { return rename; } public void setRename(String rename) { this.rename = rename; } } 好像是这个实体设计的有问题,
  • 打赏
  • 举报
回复
以下是hibernate4.3创建sessionFactory的代码:
package util.hibernate;  
      
import org.hibernate.HibernateException;  
import org.hibernate.Session;  
import org.hibernate.SessionFactory;  
import org.hibernate.boot.registry.StandardServiceRegistry;  
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;  
import org.hibernate.cfg.Configuration;  
      
public class HibernateUtil {  
private static SessionFactory factory;  
          
    static{  
        try {  
            Configuration configuration = new Configuration().configure();  
            StandardServiceRegistry  serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();  
             factory = configuration.configure().buildSessionFactory(serviceRegistry);  
      
        } catch (HibernateException e) {  
            e.printStackTrace();  
        }  
    }  
          
    public static SessionFactory getSessionFactory(){  
        return factory;  
    }  
          
    public static Session getOpenSession(){  
        return factory.openSession();  
    }  
          
    public static Session getCurrentSession(){  
        return factory.getCurrentSession();  
    }  
          
    public static void closeSession(Session session){  
        if(session!=null){  
            if(session.isOpen()){  
                session.close();  
            }  
        }  
    }  
}
如果使用注解的话,hibernate4.3直接使用Entity来指定表名,如:@Entity(name="user"),而不能用table,如:@Table(name = "user")。
爱上无名氏 2015-05-10
  • 打赏
  • 举报
回复
异常贴出来啊
日月_年 2015-05-10
  • 打赏
  • 举报
回复
求大神指点迷津,感激不尽
finemi 2015-05-10
  • 打赏
  • 举报
回复
查看异常信息
boybaozi 2015-05-10
  • 打赏
  • 举报
回复
你确认程序在别人的电脑上ok吗?是的话你就重装mysql

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧