我试着这样做:
Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
hibernateProperties.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
hibernateProperties.setProperty("hibernate.connection.url", "jdbc:mysql://" + source.getHost() + "/" + source.getDataBase());
hibernateProperties.setProperty("hibernate.connection.username", source.getUsername());
hibernateProperties.setProperty("hibernate.connection.password", source.getPassword());
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", "validate");
Configuration configuration = new Configuration();
configuration.setProperties(hibernateProperties);
configuration.setProperty("packagesToScan", "com.company.comparer.entity");
SessionFactory sessionFactory = configuration.configure().buildSessionFactory(
new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry());它就是不起作用:)
4176 2012-11-28 17:48:52,583 - [main] INFO org.hibernate.Version - HHH000412: Hibernate Core {4.1.1}
4178 2012-11-28 17:48:52,585 - [main] INFO org.hibernate.cfg.Environment - HHH000206: hibernate.properties not found
4179 2012-11-28 17:48:52,586 - [main] INFO org.hibernate.cfg.Environment - HHH000021: Bytecode provider name : javassist
4195 2012-11-28 17:48:52,602 - [main] INFO org.hibernate.cfg.Configuration - HHH000043: Configuring from resource: /hibernate.cfg.xml
4195 2012-11-28 17:48:52,602 - [main] INFO org.hibernate.cfg.Configuration - HHH000040: Configuration resource: /hibernate.cfg.xml在我的最后一行之后,我的应用程序只是退出函数,什么也不做:)
如果我进行调试,我可以看到spring捕获了一个异常,它说:
org.hibernate.HibernateException: /hibernate.cfg.xml not found你能告诉我解决这个问题的正确方法是什么吗?
我想扫描包中的实体(注释为javax...)不使用某些hibernate.cfg.xml,也不想使用多个datasources或persistence units。我只想通过编程来完成这项工作,因为我有动态datasources
发布于 2012-11-29 16:58:32
我终于回答了我的问题:))
我调试了很多,错误没有抛出(我不知道为什么)……
我必须创建一个如下所示的hibernate.cfg.xml:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration >
<session-factory>
<!-- JDBC connection pool (use the built-in) -->
<property name="hibernate.connection.pool_size">1</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="hibernate.cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="hibernate.show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hibernate.hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>然后在我使用的代码中,如下所示
public DBReaderImpl(DataSource source)
{
this.source = source;
Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
hibernateProperties.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
hibernateProperties.setProperty("hibernate.connection.url", "jdbc:mysql://" + source.getHost() + "/" + source.getDataBase());
hibernateProperties.setProperty("hibernate.connection.username", source.getUsername());
hibernateProperties.setProperty("hibernate.connection.password", source.getPassword());
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", "validate");
Configuration configuration = new Configuration();
configuration.setProperties(hibernateProperties);
configuration.addAnnotatedClass(Route.class);
SessionFactory sessionFactory = configuration.configure().buildSessionFactory(
new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry());
Session session = sessionFactory.openSession();
session.beginTransaction();
List<Route> routes = session.createQuery("SELECT r FROM Route r").list();
for (Route route : routes)
{
System.out.println(route);
}
session.close();
}也许我会使用反射来读取所有的@Entity类,以便将所有带注释的类添加到我的配置中
https://stackoverflow.com/questions/13609111
复制相似问题