标签:style blog class code java ext
参考:
http://docs.jboss.org/hibernate/annotations/3.4/reference/zh_cn/html_single/
1、系统配置:
可以通过使用 mapping的 resource,于class 属性混合配置
<mapping resource="com/rhythmk/model/product.hbm.xml" />
<mapping class="com.rhythmk.model.User"></mapping>
在创建SessionFactory 也可以通过代码完成映射关联:
|
1
2
3
4
5 |
sessionFactory = new
AnnotationConfiguration() .addPackage("test.animals") //the fully qualified package name .addAnnotatedClass(Flight.class) .addAnnotatedClass(Sky.class) .buildSessionFactory(); |
2、实体:
》每一个持久化POJO类都是一个实体bean,这可以通过在类的定义中使用@Entity
》hibernate的访问类型分别为 field或property. EJ3规范要求在需要访问的元素上进行注解声明,例如,如果访问类型为 property就要在getter方法上进行注解声明, 如果访问类型为 field就要在字段上进行注解声明.应该尽量避免混合使用这两种访问类型
》实体bean中所有的非static非transient的属性都可以被持久化, 除非你将其注解为@Transient.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 |
@Entity@Table(name = "t_user")public class User { @Id @GeneratedValue public
Integer getUserID() { return
userID; } @Column(name = "create_time") public
Date getCreateTime() { return
createTime; } public
void setUserID(Integer userID) { this.userID = userID; }.......<br><br>} |
|
1
2
3
4
5
6
7
8
9
10
11 |
@Column( name="columnName"; (1) 可选,列名(默认值是属性名) boolean
unique() default
false; (2) 可选,是否在该列上设置唯一约束(默认值false) boolean
nullable() default
true; (3) 可选,是否设置该列的值可以为空(默认值false) boolean
insertable() default
true; (4) 可选,该列是否作为生成的insert语句中的一个列(默认值true) boolean
updatable() default
true; (5) 可选,该列是否作为生成的update语句中的一个列(默认值true) String columnDefinition() default
""; (6) 可选: 为这个特定列覆盖SQL DDL片段 (这可能导致无法在不同数据库间移植) String table() default
""; (7) 可选,定义对应的表(默认为主表) int
length() default
255; (8) 可选,列长度(默认值255) int
precision() default
0; // decimal precision (9) 可选,列十进制精度(decimal precision)(默认值0) int
scale() default
0; // decimal scale (10) 可选,如果列十进制数值范围(decimal scale)可用,在此设置(默认值0) |
3、映射继承关系:
|
1
2
3
4
5
6
7
8
9
10
11
12 |
@Entity@Inheritance(strategy=InheritanceType.SINGLE_TABLE)@DiscriminatorColumn( name="planetype", discriminatorType=DiscriminatorType.STRING)@DiscriminatorValue("Plane")public
class Plane { ... }@Entity@DiscriminatorValue("A320")public
class A320 extends
Plane { ... } |
|
1
2
3
4
5
6
7
8
9
10 |
@Entity@Inheritance(strategy=InheritanceType.JOINED)public class Boat implements
Serializable { ... }@Entitypublic class Ferry extends
Boat { ... }@Entity@PrimaryKeyJoinColumn(name="BOAT_ID")public
class AmericaCupClass extends
Boat { ... } |
Rhythmk 学习 Hibernate 07 - Hibernate annotation 实体注解,布布扣,bubuko.com
Rhythmk 学习 Hibernate 07 - Hibernate annotation 实体注解
标签:style blog class code java ext
原文地址:http://www.cnblogs.com/rhythmK/p/3705182.html