Jake
Jake

Reputation: 4660

Spring and Hibernate: Function of annotatedClasses and packagesToScan

I have read substantive tracts of Spring Documentation but deadlines have constrained my reading. My Spring Hibernate app is working. However I am not sure about some of the inner mechanisms.

Can someone explain how annotatedClasses functions, why we need it as well as the hibernate class map and also how its function differs from packages to scan?

<bean id="sfhbmSessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="annotatedClasses">
			<list>
				<value>com.prototype2.model.user.IdentityInformation</value>
				     <value>com.prototype2.model.user.IdentityInformationType</value>
				<value>com.prototype2.model.user.User</value>
				<value>com.prototype2.model.user.UserChild</value>
				<value>com.prototype2.model.user.UserMain</value>
		........  
			</list>
		</property>
		<property name="mappingResources">
			<list>
				<value>hibernatemap.cfg.xml</value>
			</list>
		</property>



		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<prop key="temp.use_jdbc_metadata_defaults">false</prop>
				<prop key="hibernate.archive.autodetection">class</prop>
				<prop key="show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.enable_lazy_load_no_trans">true</prop>
			</props>
		</property>


		<property name="packagesToScan">
			<array>
				<value>com.prototype2.model.assessment</value>
				<value>com.prototype2.model.user</value>
				<value>com.prototype2.model.scores</value>
				<value>com.prototype2.model.business</value>
				<value>com.prototype2.dao.factory</value>
				<value>com.prototype2.dao.user</value>
			</array>
		</property>
	</bean>

Upvotes: 3

Views: 1641

Answers (1)

Ali Dehghani
Ali Dehghani

Reputation: 48133

annotatedClass specifies annotated entity classes to register with this Hibernate SessionFactory. With this approach, you should register all the entities that are annotated with @Entity manually which is not a scalable solution. A better approach would be specifing the names of annotated packages (as opposed to individual classes) by packagesToScan property. Anytime you add a new entity to those packages, it'll be auotmatically registered.

Upvotes: 4

Related Questions