hello hibernate

来源:互联网 发布:阿里云 直播 编辑:程序博客网 时间:2024/06/03 15:43

这几天闲着无事 就看了看hibernate

看的视频是北京尚学堂马士兵的 版本是3.2 我自己下的是4.3.5 中间出了一堆问题 现记录下来

项目组织图如下

首先就是要


遇到的几个问题

问题一:

如果我们通过sessionfactory.getCurrentSession来获得session,那么在配置文件里,我们就得加上
 <property name="current_session_context_class">thread</property>
如果单纯的使用sessionfactory来获得session,那么上面的代码,加与不加都OK

问题二:

package com.bjsxt.hibernate;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.AnnotationConfiguration;import util.HibernateUtil;public class StuTest {    public static void main(String[] args) {        Stu s = new Stu();        s.setId(48236);        s.setName("zhangsan");         s.setAge(8);            //    SessionFactory sessionFactory =     //            new AnnotationConfiguration().configure().buildSessionFactory();    //    Session session = sessionFactory.getCurrentSession();        Session session= HibernateUtil.getSessionFactory().getCurrentSession();            session.beginTransaction();        session.save(s);        session.getTransaction().commit();    }}
首先就是在4.3中buildSessionFactory被废除了

相应的部分应该改成这样

Configuration cfg = new Configuration().configure();StandardServiceRegistryBuilder srb =new StandardServiceRegistryBuilder().applySettings(cfg.getProperties());StandardServiceRegistry sr = srb.build();SessionFactory sf = cfg.buildSessionFactory(sr);
见参考资料1

其实更方便的就是设置util类如下

package util;import org.hibernate.SessionFactory;import org.hibernate.boot.registry.StandardServiceRegistry;import org.hibernate.boot.registry.StandardServiceRegistryBuilder;import org.hibernate.cfg.Configuration;public class HibernateUtil4 {    private static final SessionFactory sessionFactory = buildSessionFactory();    private static SessionFactory buildSessionFactory() {        try {        Configuration cfg = new Configuration().configure();    StandardServiceRegistryBuilder srb =    new StandardServiceRegistryBuilder()    .applySettings(cfg.getProperties());    StandardServiceRegistry sr = srb.build();    return cfg.buildSessionFactory(sr);        }        catch (Throwable ex) {            // Make sure you log the exception, as it might be swallowed            System.err.println("Initial SessionFactory creation failed." + ex);            throw new ExceptionInInitializerError(ex);        }    }    public static SessionFactory getSessionFactory() {        return sessionFactory;    }}
参考4.3的说明文档 我就想不通了

 try {            // Create the SessionFactory from hibernate.cfg.xml            new Configuration().configure().buildSessionFactory(    new StandardServiceRegistryBuilder().build() );        }
为什么不return?   难道我想错了?


问题二:

<property name="hbm2ddl.auto">creat</property>
加上这句话就能让hibernate自动建表

可我的console上就是没有droptable creat teble

最后想是不是日志文件有问题 倒腾了半天

hbm2ddl.auto 里面是create 不是creat!!!

另外,如果hibernate的配置信息在spring中,那么像之前的hbm2ddl.auto前面都得加上hibernate

例如

<pre name="code" class="html"><bean id="sessionFactory"class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"><property name="dataSource"><ref bean="dataSource" /></property><property name="packagesToScan"><list><value>cdm.core.model</value></list></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop><prop key="hibernate.hbm2ddl.auto">update</prop><prop key="hibernate.show_sql">true</prop><prop key="hibernate.format_sql">true</prop><prop key="current_session_context_class">thread</prop></props></property></bean>



问题三:

session关闭问题


查阅资料 才发现获得session有两者方法

sf.opensession 和sf.getCurrentSession

第二种方式不用自己手动关闭session 第一种需要手动关闭

见参考资料2






参考资料

1  http://www.yihaomen.com/article/java/453.htm

2  http://blog.csdn.net/yongjian1092/article/details/8350313

0 0
原创粉丝点击