John Smith
John Smith

Reputation: 1793

configure hibernate to map entities automaticly

I use hibernate ORM in my project. Now I map entities like this :

<mapping class="entities.User"/>

but I have to do this for each entity I create - is there anything I can put in the hibernate configuration to make it scan itself for annotated entities in certain package?

thank you

Upvotes: 0

Views: 70

Answers (2)

Zeus
Zeus

Reputation: 6566

Using spring can help you get the package scanned. See the config below

<bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="packagesToScan">
            <list>
                <value>com.tds.hibernate.entities</value>
            </list>
        </property>
        <property name="dataSource" ref="dataSource" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
                <prop key="hibernate.current_session_context_class">thread</prop>

            </props>
        </property>

    </bean>

Upvotes: 1

Learner
Learner

Reputation: 21405

You can place all your java entities in a JAR file and then provide the path of the JAR file in hibernate configuration file like this:

<mapping jar="path_to_your_jar_file"/> 

Update:

This is helpful only if you have hbm.xml files for mapping instead of having annotations on your classes. These mapping files should be part of the JAR file.

Look at this link for addJar method of Configuration class.

Upvotes: 1

Related Questions