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在那个包里面的 展开
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在那个包里面的 展开
2个回答
展开全部
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();
}
}
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());
}*/
}
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());
}*/
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询