1. 程式人生 > >前幾天用AnnotationSessionFactoryBean 註解方式代替*.hbm.xml時掃描對映實休檔案後報錯

前幾天用AnnotationSessionFactoryBean 註解方式代替*.hbm.xml時掃描對映實休檔案後報錯

吐操一下.Hibernate真心配置麻煩.不是VO. 是sessionFactory

hibernate3.5以前的版本使用註解使用hibernate-annotations.jar以及ejb-persistence.jar。其實這裡的ejb-persistence.jar和前面提到的hibernate-jpa-2.0-api-1.0.0.Final.jar功能一樣,實現持久化功能。但hibernate-jpa-2.0-api-1.0.0.Final.jar在ejb-persistence.jar基礎上新添了一些類,估計是hibernate官方對jpa的擴充套件

報錯如下:      org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginAction': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibLoginDAOImpl': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/hibernate/annotations/Entity

ejb3-persistence.jar hibernate-jpa-2.0-api-1.0.1.Final.jarp 這兩個是有衝突的.

給出兩方配置方式:

    	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource">
			<ref bean="myDataSource" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
			</props>
		</property>
		<property name="annotatedClasses">
			<list>
				<value>com.danlan.vo.Knowledge</value>
				<value>com.danlan.vo.MjhUserinfo</value>
			</list>
		</property>
	</bean>
	

當然.這裡也可以改為目錄掃描

<property name="annotatedClasses">
<list>
<value>com.danlan.vo.Knowledge</value>
<value>com.danlan.vo.MjhUserinfo</value>
</list>
</property>

二:

	
	<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="myDataSource" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
			</props>
		</property>
		<property name="mappingLocations">
		 	<value>classpath:/com/danlan/vo/*.hbm.xml </value>  
		</property>