Spring and hibernate integrate automatic scanning traps
The spring 2.5.6 automatic package scanning (Packagestoscan) feature is used when configuring Hibernate+spring.
This feature automatically scans the entity classes in the package to avoid the hassle of writing annotatedclasses. But when I use this feature, I've been encountering errors like this:
caused By:org.hibernate.MappingException:Unknown entity:com.mycompany.app.model.Users
The configuration in spring is:
Java code <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" > ${hibernate.dialect} </prop> <prop key= "HibernaTe.show_sql "> ${hibernate.show_sql} </prop> <prop key= "Hibernate.jdbc.fetch_size" > ${hibernate.jdbc.fetch_size} </prop> <prop key= "Hibernate.jdbc.batch_size" > ${ hibernate.jdbc.batch_size} </prop> <prop key= "Hibernate.c3p0.timeout" > ${hibernate.c3p0.timeout} </prop> </props> </property> <span style= " background-color: #ff0000; " ><property name= "Packagestoscan" value= "com.mycompany.app.model.*" /></span> </bean>
But my entity class is clearly under Com.mycompany.app.model, why can't I scan it?
After a closer look at the document discovery, Packagestoscan's value should be configured as com.mycompany.app.*, which is the upper layer of the entity's package. This is really an incredible trap.