如何将数据保存到多个hibernate数据库:H2和mysql(localhost)

时间:2014-04-29 22:51:22

标签: java hibernate

我正在尝试将我的应用程序保存到两个数据库:H2和mysql(XAMPP,localhost)。

这些是我的XML.config文件:

H2:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">org.h2.Driver</property>
        <property name="connection.url">jdbc:h2:file:C:/WAKILI/WAKILIdb</property>
        <property name="connection.username">sa</property>
        <property name="connection.password"></property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.H2Dialect</property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

    <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>

        <!-- Names the annotated entity class -->
        <mapping class="wakiliproject.Forms.AddNew.DB.NewBeautifulKiwi"/>
        <mapping class="wakiliproject.Forms.AddNew.DB.CalendarPOJO"/>

    </session-factory>

</hibernate-configuration>

XAMPP:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/sakila?zeroDateTimeBehavior=convertToNull</property>
        <property name="hibernate.connection.username">Revilo</property>
        <property name="hibernate.connection.password">eddyan</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.H2Dialect</property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>

        <!-- Names the annotated entity class -->
        <mapping class="wakiliproject.Forms.AddNew.DB.NewBeautifulKiwi"/>
        <mapping class="wakiliproject.Forms.AddNew.DB.CalendarPOJO"/>

    </session-factory>

</hibernate-configuration>

这就是我编写SessionFactory的方法:

public class HibernateUtil {

    private static final SessionFactory sessionFactoryH2;
    private static final SessionFactory sessionFactoryXampp;

    static {
        try {
            // Create the SessionFactory from standard (hibernate.cfg.xml) 
            // config file.
            sessionFactoryH2
                = new Configuration().configure("/HibernateXML/hibernate.cfg.xml").buildSessionFactory();
            sessionFactoryXampp
                = new Configuration().configure("/HibernateXML/hibernateServer.cfg.xml").buildSessionFactory();
        } catch (HibernateException ex) {
            // Log the exception. 
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactoryHs() {
        return sessionFactoryH2;
    }

    public static SessionFactory getSessionFactoryXampp() {
        return sessionFactoryXampp;
    }
}

然后我尝试将String保存到数据库中,如下所示:

public class ManageItems {

    Session session = HibernateUtil.getSessionFactoryH2().openSession();
    Session sessionServer = HibernateUtil.getSessionFactoryXampp().openSession();

    /* Method to CREATE an item in the database */
    public void addItems(Object o) {
        Transaction tx = null;
        Transaction txServer;

        try {
            tx = session.beginTransaction();
            txServer = sessionServer.beginTransaction();

            session.save(o);
            sessionServer.save(o);

            tx.commit();
            txServer.commit();

        } catch (HibernateException e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        } finally {
            session.close();
            sessionServer.close();
        }
    }
}

public class PersistNewBeautifulKiwi {

    public void doKiwi(String kiwi) {
        NewBeautifulKiwi newBeautifulKiwi = new NewBeautifulKiwi();
        newBeautifulKiwi.setKiwi(kiwi);

        new ManageItems().addItems(newBeautifulKiwi);
    }
}

我收到此错误:

Caused by: java.lang.NoSuchMethodError: wakiliproject.Persistence.ManageItems.addItems(Ljava/lang/Object;)Ljava/lang/Integer;
at wakiliproject.Forms.AddNew.DB.HibernatePersist.PersistNewBeautifulKiwi.doKiwi(PersistNewBeautifulKiwi.java:12)

有谁可以帮我确定我哪里出错了。提前谢谢大家。

还有更好的方法吗?我计划通过方法参数传递hibernate xml配置文件路径。

0 个答案:

没有答案