Java 用于在同一数据库中映射多个 MySQL 表的 Hibernate 配置文件 (.cfg.xml)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24089645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 10:06:06  来源:igfitidea点击:

Hibernate configuration file (.cfg.xml) for mapping multiple MySQL tables in the same database?

javamysqlhibernatemapping

提问by TonyGW

I am experimenting with Hibernate for my Java web app. The following is part of my hibernate.cfg.xml, and I wondering how to map multiple database tables in the same configuration file. I use annotations to map my models to mysql database table, and I have multiple model classes (for example: models.Book), how to map the models in hibernate.cfg.xml?

我正在为我的 Java Web 应用程序试验 Hibernate。以下是我的hibernate.cfg.xml的一部分,我想知道如何在同一个配置文件中映射多个数据库表。我使用注解将我的模型映射到mysql数据库表,并且我有多个模型类(例如:models.Book),如何映射hibernate.cfg.xml中的模型?

<hibernate-configuration>
    <session-factory>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/test_db</property>
        <property name="connection.username">root</property>
        <property name="connection.password">xxx</property>

        <property name="connection.pool_size">1</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="current_session_context_class">thread</property>
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">validate</property>

        <mapping class ="models.Category" />

    </session-factory>
</hibernate-configuration>

回答by dvk317960

We should not specify mappings in cfg.xml file. It has to be done by either annotations or XML. For Annotations: The cfg.xml file that is provided by you looks ok, if we are using the annotations to indicate database mappings with entity classes.

我们不应在 cfg.xml 文件中指定映射。它必须通过注释或 XML 来完成。对于注释:如果我们使用注释来指示与实体类的数据库映射,那么您提供的 cfg.xml 文件看起来没问题。

To use XML way of mapping between Entities and Tables, an hbm.xml file needs to be created and in that case, Replace

要使用实体和表之间的 XML 映射方式,需要创建一个 hbm.xml 文件,在这种情况下,替换

<mapping class ="models.Category" />

with something like

<mapping resource="models/Book.hbm.xml></mapping> 

and hbm.xml file contains the necessary mapping as follows. for example:

和 hbm.xml 文件包含必要的映射如下。例如:

   <hibernate-mapping>
    <class name="models.Book" table="Book" catalog="your database name">
        <id name="bookId" type="java.lang.Integer">
            <column name="BOOKID" />
            <generator class="identity" />
        </id>
        <property name="authorName" type="string">
            <column name="AUTHOR_NAME" length="10" not-null="true" unique="true" />
        </property>
    </class>//all the database mappings
</hibernate-mapping>

Sorry, if I understand your question wrongly.

对不起,如果我理解错了你的问题。

回答by pratap reddy

WE can't configure the multiple databases in single configuration file.if we use multiple databases we have to use multiple configuration file. in respective tables we can configure in respective configuration file.

我们不能在一个配置文件中配置多个数据库。如果我们使用多个数据库,我们必须使用多个配置文件。在相应的表中,我们可以在相应的配置文件中进行配置。