Hibernate常用注释收集8
Hibernate 独有的注解扩展
Hibernate 提供了与其自身特性想吻合的注解,org.hibernate.annotations package包含了这些
注解。
实体
org.hibernate.annotations.Entity 定义了 Hibernate 实体需要的信息。
? mutable: whether this entity is mutable or not 此实体是否可变
? dynamicInsert: allow dynamic SQL for inserts 用动态SQL新增
? dynamicUpdate: allow dynamic SQL for updates 用动态SQL更新
? selectBeforeUpdate: Specifies that Hibernate should never perform an SQL UPDATE unless it
is certain that an object is actually modified.指明Hibernate从不运行SQL Update,除非能确定
对象已经被修改
? polymorphism: whether the entity polymorphism is of PolymorphismType.IMPLICIT (default)
or PolymorphismType.EXPLICIT 指出实体多态是 PolymorphismType.IMPLICIT(默认)还是
PolymorphismType.EXPLICIT
? optimisticLock: optimistic locking strategy (OptimisticLockType.VERSION,
OptimisticLockType.NONE, OptimisticLockType.DIRTY or OptimisticLockType.ALL) 乐观锁
策略
标识符
@org.hibernate.annotations.GenericGenerator和@org.hibernate.annotations.GenericGenerators
允许你定义hibernate特有的标识符。
@Id @GeneratedValue(generator=”system-uuid”)
@GenericGenerator(name=”system-uuid”, strategy = “uuid”)
public String getId() {
@Id @GeneratedValue(generator=”hibseq”)
@GenericGenerator(name=”hibseq”, strategy = “seqhilo”,
parameters = {
@Parameter(name=”max_lo”, value = “5”),
@Parameter(name=”sequence”, value=”heybabyhey”)
}
)
public Integer getId() {
新例子
@GenericGenerators(
{
@GenericGenerator(
name=”hibseq”,
strategy = “seqhilo”,
parameters = {
@Parameter(name=”max_lo”, value = “5”),
@Parameter(name=”sequence”, value=”heybabyhey”)
}
),
@GenericGenerator(…)
}
)
自然ID
用 @NaturalId 注解标识
公式
让数据库而不是JVM进行计算。
@Formula(“obj_length * obj_height * obj_width”)
public long getObjectVolume()
索引
通过在列属性(property)上使用@Index注解,可以指定特定列的索引,columnNames属性
(attribute)将随之被忽略。
@Column(secondaryTable=”Cat1″)
@Index(name=”story1index”)
public String getStoryPart1() {
return storyPart1;
}
辨别符
@Entity
@DiscriminatorFormula(“case when forest_type is null then 0 else forest_type end”)
public class Forest { … }
过滤 查询 …
? 其中一个实体通过外键关联到另一个实体的主键。注:一对一,则外键必须为唯一约束。
@Entity
public class Customer implements Serializable {
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name=”passport_fk”)
public Passport getPassport() {
…
}
@Entity
public class Passport implements Serializable {
@OneToOne(mappedBy = “passport”)
public Customer getOwner() {
…
}
通过@JoinColumn注解定义一对一的关联关系。如果没有@JoinColumn注解,则系统自动
处理,在主表中将创建连接列,列名为:主题的关联属性名 + 下划线 + 被关联端的主键
列名。上例为 passport_id, 因为Customer 中关联属性为 passport, Passport 的主键为 id.
? 通过关联表来保存两个实体之间的关联关系。注:一对一,则关联表每个外键都必须是唯
一约束。
@Entity
public class Customer implements Serializable {
@OneToOne(cascade = CascadeType.ALL)
@JoinTable(name = “CustomerPassports”,
joinColumns = @JoinColumn(name=”customer_fk”),
inverseJoinColumns = @JoinColumn(name=”passport_fk”)
)
public Passport getPassport() {
…
}
@Entity public class Passport implements Serializable {
@OneToOne(mappedBy = “passport”)
public Customer getOwner() {
…
}
Customer 通过 CustomerPassports 关联表和 Passport 关联。该关联表通过 passport_fk 外
键指向 Passport 表,该信心定义为 inverseJoinColumns 的属性值。 通过 customer_fk 外
键指向 Customer 表,该信息定义为 joinColumns 属性值。
声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 嗅谱网
转载请注明:转自《Hibernate常用注释收集8》
本文地址:http://www.xiupu.net/archives-279.html
关注公众号:
微信赞赏
支付宝赞赏