6

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>
1
  • I am also trying to do the same thing. Did you find the solution @TonyGW Commented Mar 28, 2015 at 22:25

2 Answers 2

6

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.

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> 

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.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks, but I don't use xml to map my class to database table. Instead I use annotations on my entities such as Book.
@TonyGW - Please refer below link to extensively understand the annotations based mapping with detailed examples. link
Well, if I am understanding this right, we have Hibernate annotations and JPA annotations, similar but different. You can always check this link to see more details: doc.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.