10

is there any way to define objects in hibernate.cfg.xml by scope and not one by one?

For example, in Spring you can define all controllers by such annotation:

<context:component-scan base-package="crm.controller" />

Can I define hibernate classes in the same way? Or it must be defined one by one?

Thank you

nKognito
  • 6,297
  • 17
  • 77
  • 138
  • Related questions: http://stackoverflow.com/q/1413190/211197 http://stackoverflow.com/q/2419802/211197 – maksimov Apr 06 '12 at 14:20

5 Answers5

1

If you are using Spring MVC, you can configure it when setting up your sessionFactory. If you are using hbm files:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="mappingResources">
        <list>
            <value>file1.hbm.xml</value>
            <value>file2.hbm.xml</value>
        </list>
    </property>
    <property name="hibernateProperties" ref="hibernateProperties"/>
</bean>

If you are using annotated classes:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.me.domain">
    <property name="hibernateProperties" ref="hibernateProperties"/>
</bean>
stephen.hanson
  • 9,014
  • 2
  • 47
  • 53
0

Why dont you try this . I have this configured in springDAOcontext.xml

<bean id="csiSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="configLocation">
       <value>classpath:hibernate/hibernate.cfg.xml</value>
    </property>
    <property name="dataSource" ref="DataSource"/>

    <property name="mappingResources">
        <list>
            <value>hibernate/*</value>
                     </list>
                     <property>
Goutham
  • 80
  • 12
0

As far as I can remember, Hibernate as it is out-of-the box, does not support "package-sanning" to discover your domain classes.

But given that you're integrating Hibernate with Spring MVC, you could annotate your classes (standard JPA + Hibernate specific annotations) and within your Spring context configuration file, specify the package that contains the Hibernate entity classes. Here's how I configured it in a previous project :

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="org.springmvc.domain" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="show_sql">true</prop>
            <prop key="log_level">DEBUG</prop>
        </props>
    </property>
</bean>

The "packagesToScan" property define the packages that Spring will have Hibernate scan to discover the entity classes.

kyiu
  • 1,926
  • 1
  • 24
  • 30
0

im use jpa annotation @Entity,the code of applicationContext.xml is :

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.yourcompany.module" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
        </props>
    </property>
</bean>

The package path "com.yourcompany.module" is your java entity package,for example:

package com.yourcompany.module;
@Entity
class MyEntity{
    private Long id;
    set get...
}
snageyang
  • 59
  • 1
  • 6
0

Try using:

<mapping package="com.mycompany.model" />

instead of a set of elements:

<mapping class="com.mycompany.model.EntytyOne" />
<mapping class="com.mycompany.model.EntytyTwo" />
ŁukaszBachman
  • 33,595
  • 11
  • 64
  • 74