注解加载实体类文件代码如下:
package?com.bird.user.entity;?
import?javax.persistence.Entity;
import?javax.persistence.Table;
import?javax.persistence.Id;
@Entity
@Table(name?=?"user")
public?class?User?{?
???private?int??id???????????;????
???@Id?
???public?int?getId()?{
????????return?id;
????}?
????public?void?setId(int?id)?{
????????this.id?=?id;
????}?
}
如果不用注解?,需要在spring.xml中增加如下代码。
?<property?name="mappingResources">
??????<list>
???????<value>com/bird/user/entity/User.hbm.xml</value>
??????</list>
????</property>
去掉
<bean id="Gameentity" class="baby.entity.Gameentity" />
配置文件修改如下:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties" ref="hibernateProperties" />
<property name="packagesToScan" value="baby/entity/"></property>//关键是这句
</bean>
看的我头痛眼花,不过我可以指点你下,你用的注解是吧?那么请问你的配置文件里面有没有引进注解需要的各种头文件:
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
<context:annotation-config/>
还有你的命名,你的springconfg里面配置的bean名称要和你的action里面的名称一样,假如你配置的dao或者是service、那么就要实现借口。在action里面应是写借口,因为spring是面试借口编程的。
hibernate实体类注解怎么创建sessionfactory
spring 2.5.6新特性之packagestoscan
如果你使用早前版本的spring,又恰好采用了annotation注解方式(而非传统xml方式)配置hibernate对象关系映射,那么在通过org.springframework.orm.hibernate3.annotation.annotationsessionfactorybean配置sessionfactory时,你一定对annotatedclasses、annotatedpackages有一种说不出的胸闷的感觉,如此以高配置性见长的spring,怎么在这一个小小的环节上就不能做得再灵活些呢,一定要一个个手写class路径么
sessionFactoty这个bean配置有错误? 数据源的配置能不能贴出来看看
用户登录
还没有账号?立即注册
用户注册
投稿取消
文章分类: |
|
还能输入300字
上传中....