hibernate4.2问题 5

packageorg.hibernate.tutorial.util;importorg.hibernate.SessionFactory;importorg.hiber... package org.hibernate.tutorial.util;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

private static final SessionFactory sessionFactory = buildSessionFactory();

private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
new Configuration().configure().buildSessionFactory(
new StandardServiceRegistryBuilder().build() );
}
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;
}

}

这是hibernate4.2api中的

但是我发现我的程序找不到这个import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
我想请问一下这个StandardServiceRegistryBuilder在那个包里面的
展开
 我来答
534756893
2014-04-12 · TA获得超过103个赞
知道小有建树答主
回答量:82
采纳率:0%
帮助的人:41.6万
展开全部
hibernate-core-4.3.4.Final.jar\org\hibernate\boot\registry。
4.2的不是这个类吧?好像4.2是楼下的方法,我的是4.3的:
导入required包就能运行了,找不到说明4.2以前的没有更新到这个类。
祝你好运

package one.annotation;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

@Entity
public class Sample1 {
private int id;
private String name;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static void main(String[]args){
Configuration cfg = new Configuration().configure();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
cfg.addAnnotatedClass(Sample1.class);
SessionFactory sessionFactory = cfg.buildSessionFactory(serviceRegistry);
Session session=sessionFactory.openSession();
Sample1 s=new Sample1();
s.setName("test");
session.beginTransaction();
session.save(s);
session.getTransaction().commit();

}
}
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
推荐于2018-05-07
展开全部
换一个用法试试

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public final class HibernateUtil {
private static SessionFactory factory = null;

public static SessionFactory getSessionFactory() {
if (factory == null) {
Configuration conf = new Configuration().configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(conf.getProperties()).buildServiceRegistry();
factory = conf.buildSessionFactory(serviceRegistry);
}

return factory;
}

/*public static void main(String[] args) {
SessionFactory sf = HibernateUtil.getSessionFactory();
System.out.println(sf.toString());
}*/
}
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式