Tag: hibernate.cfg.xml

Hibernate ConfigurationException:找不到cfg.xml资源 IntelliJ

我在IntelliJ IDE中遇到了hibernate.cfg.xml的问题。 这是我的hibernate配置文件: org.postgresql.Driver jdbc:postgresql://localhost/HIndex index_user password 1 org.hibernate.dialect.PostgreSQL82Dialect true create 这是我的应用程序类: package HIndexSaar.HIndex; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class HibernateManager { private static SessionFactory factory; public HibernateManager(){ // //* Setup the configuration. // Configuration config = new Configuration().configure(“hibernate.cfg.xml”).addAnnotatedClass(Person.class) .addAnnotatedClass(University.class).addAnnotatedClass(Publication.class); factory = config.buildSessionFactory(); } 我通过运行以下代码得到一个错误: package HIndexSaar.HIndex; public class […]

Hibernate 4注释配置

我正在尝试仅使用带有注释的Hibernate 4和一个hibernate.cfg.xml文件。 我已经制作了自己的注释,并使用reflection将其添加到配置中。 我能够以这种方式使用Hibernate 4,但我的配置是使用不推荐的方法构建的。 final Configuration configuration = new Configuration(); final Reflections reflections = new Reflections(Item.class.getPackage().getName()); final Set<Class> classes = reflections.getTypesAnnotatedWith(Entity.class); for (final Class clazz : classes) { configuration.addAnnotatedClass(clazz); } return configuration.configure().buildSessionFactory(); (不推荐的代码: buildSessionFactory(); )。 即使是hibernate 4文档也显示以这种方式构建配置。 如果我尝试使用新方法( buildSessionFactory(ServiceRegistry) ,我得不到相同的结果,并且看起来很多不必要的代码完全按照已弃用的方法执行。但是,我不想继续使用这种风格,因为我不喜欢使用已弃用的代码。 我的问题是:如何以上述方式从配置文件中正确配置Hibernate 4? 我似乎只是造成错误并面临不必要的困难。

Hibernate – ServiceRegistryBuilder

我只是想学习Hibernate(最终版本4)但是在尝试创建会话工厂时我遇到了问题。 以下是与此问题相关的一些代码: hibernate.cfg.xml中: jdbc:mysql://localhost/fitterblog root com.mysql.jdbc.Driver 1 org.hibernate.dialect.MySQL5InnoDBDialect org.hibernate.transaction.JDBCTransactionFactory thread true <!– –> HibernateUtil.java: public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() { try { // Create the SessionFactory from hibernate.cfg.xml ServiceRegistryBuilder srb = new ServiceRegistryBuilder(); //NOTE: THIS IS WHERE MY PROGRAM DIES!! srb = srb.configure(); ServiceRegistry sr […]