Hibernate配置文件(.cfg.xml)用于映射同一个数据库中的多个MySQL表? [英] Hibernate configuration file (.cfg.xml) for mapping multiple MySQL tables in the same database?

查看:500
本文介绍了Hibernate配置文件(.cfg.xml)用于映射同一个数据库中的多个MySQL表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

<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>


推荐答案

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

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.

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

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

<mapping class ="models.Category" />

类似于

with something like

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

和hbm.xml文件包含如下所需的映射:
例如:
$ b $

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

   <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.

这篇关于Hibernate配置文件(.cfg.xml)用于映射同一个数据库中的多个MySQL表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆