首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

用诠注配置Hibernate

2012-09-10 
用注解配置Hibernate用注解配置的方式配置hibernate 的映射关系时要导入三个包包 hibernate-annotations.j

用注解配置Hibernate

用注解配置的方式配置hibernate 的映射关系时要导入三个包包 hibernate-annotations.jar , ejb3-persistence.jar ,hibernate-commons-annotations.jar @Entity 用于注解一个类是一个数据库中的实体 @Id  用于注解现在的是一个主键,一般后面要跟上@GenerateValue(strategy=GenerationType.Auto) 这个就行了, 不过我们还得在hibernate.cfg.xml中添加一个<mapping class="com.xx.xx.bean.xx">在初始化Configuration接口时我们用AnnotationConfiguration();其实注解还有好多 @Column声明属性到映射 @Transient 声明属性不会跟数据库关联 如果这样用@Transient private Date birthday 这样数据库中就没有此字段@Formula 用查询语句动态生成一类的属性也是虚拟的列 通过查询语句计算出来 例:@Formula ("(select count(*) from t_user u where u.id>id )")private int countUser  要点:sql语句必需位于括号中       表最好用别名        可以引用当前属性的值,可以直接使用属性,如u.id>id 

热点排行