Java spring 3.1 hibernate 4不带persistence.xml-多个数据源(无web应用程序)

Java spring 3.1 hibernate 4不带persistence.xml-多个数据源(无web应用程序),java,spring,hibernate,orm,sessionfactory,Java,Spring,Hibernate,Orm,Sessionfactory,我试着这样做: Properties hibernateProperties = new Properties(); hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect"); hibernateProperties.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver"); hib

我试着这样做:

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());
<?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>
它只是不起作用:)

在我的最后一行之后,我的应用程序就退出了函数,什么也不做:)

如果我进行调试,我可以看到spring捕获到一个异常,它说:

org.hibernate.HibernateException: /hibernate.cfg.xml not found
你能告诉我解决这个问题的正确方法吗


我想扫描包中的实体(注释为javax…),而不是使用一些
hibernate.cfg.xml
,我不想使用多个
数据源
持久化单元
。。我只想通过编程实现,因为我有动态的
数据源

我终于回答了我的问题:)

我调试了很多次,没有抛出错误(我不知道为什么)

我必须创建一个如下所示的
hibernate.cfg.xml

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());
<?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>

1.
线
org.hibernate.cache.internal.NoCacheProvider
真的
更新
然后在我使用的代码中

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();
}
公共DBReaderImpl(数据源) { this.source=源; Properties hibernateProperties=新属性(); setProperties(“hibernate.dialogue”、“org.hibernate.dialogue.mysqldialogue”); setProperties(“hibernate.connection.driver_class”,“com.mysql.jdbc.driver”); hibernateProperties.setProperty(“hibernate.connection.url”,“jdbc:mysql://”+source.getHost()+“//”+source.getDataBase()); setProperties(“hibernate.connection.username”,source.getUsername()); setProperties(“hibernate.connection.password”,source.getPassword()); setProperties(“hibernate.hbm2ddl.auto”、“validate”); 配置=新配置(); setProperties(hibernateProperties); configuration.addAnnotatedClass(Route.class); SessionFactory SessionFactory=configuration.configure().buildSessionFactory( 新建ServiceRegistryBuilder(); Session Session=sessionFactory.openSession(); session.beginTransaction(); List routes=session.createQuery(“从路由r中选择r”).List(); 用于(路线:路线) { 系统输出打印LN(路线); } session.close(); } 也许我将使用反射来读取我的所有
@Entity
类,以将所有带注释的类添加到我的配置中