in my applicationContext.xml, this is how I map xml to POJO. how to map directory to class file without required to create xml?
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="mappingResources">
<list>
<value>com/custompackage/custom/spi/hibernate3/HibernateCurrentStep.hbm.xml</value>
<value>com/custompackage/custom/spi/hibernate3/HibernateHistoryStep.hbm.xml</value>
<value>com/custompackage/custom/spi/hibernate3/HibernatecustomEntry.hbm.xml</value>
<value>user/custom/hibernate3/PropertySetItemImpl.hbm.xml</value>
<value>com/custompackage/user/provider/hibernate3/user/impl/HibernateGroupImpl.hbm.xml</value>
<value>com/custompackage/user/provider/hibernate3/user/impl/HibernateUserImpl.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
.....
</property>
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
And you can further simplify things by converting
<property name="annotatedClasses">
<list>
<value>com.mycompany.sample.domain.Order</value>
<value>com.mycompany.sample.domain.LineItem</value>
...
</list>
</property>
to
<property name="packagesToScan" value="com.mycompany.sample.domain" />
in your AnnotationSessionFactoryBean
so now all classes annotated with @Entity
in the com.mycompany.sample.domain
package will be automatically picked up.
Instead of using XML mapping files, you can use the Hibernate Annotations library which is based on Java 5 annotations.
As usual, you'll need to declare your persistence classes in the Hibernate configuration file (typically hibernate.cfg.xml
), though you use the <mapping>
element to declare your persistent classes:
<hibernate-configuration>
<session-factory>
<mapping class="com.mycompany.sample.domain.Order"/>
<mapping class="com.mycompany.sample.domain.LineItem"/>
</session-factory>
</hibernate-configuration>
If you are using the Spring framework, you can set up an annotation-based Hibernate session factory using the AnnotationSessionFactoryBean
class, as shown here:
<!-- Hibernate session factory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.DerbyDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
...
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.mycompany.sample.domain.Order</value>
<value>com.mycompany.sample.domain.LineItem</value>
...
</list>
</property>
</bean>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With