Hibernate configuration file (.cfg.xml) for matching multiple MySQL tables in a single database?

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

<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>
+4
source share
2 answers

cfg.xml. , XML. : cfg.xml, , , .

XML- , hbm.xml

<mapping class ="models.Category" />

-

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

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>

, .

+6

. , . .

+1

Source: https://habr.com/ru/post/1543619/


All Articles