Hibernate常用注释收集6


用 cascading 实现传播持久化(Transitive persistence)

cascade 属性接受值为 CascadeType 数组,其类型如下:

? CascadeType.PERSIST: cascades the persist (create) operation to associated entities persist() is
called or if the entity is managed 如果一个实体是受管状态,或者当 persist() 函数被调用时,
触发级联创建(create)操作。

? CascadeType.MERGE: cascades the merge operation to associated entities if merge() is called
or if the entity is managed 如果一个实体是受管状态,或者当 merge() 函数被调用时,触发
级联合并(merge)操作。

? CascadeType.REMOVE: cascades the remove operation to associated entities if delete() is
called 当 delete() 函数被调用时,触发级联删除(remove)操作。

? CascadeType.REFRESH: cascades the refresh operation to associated entities if refresh() is
called 当 refresh() 函数被调用时,出发级联更新(refresh)操作。

? CascadeType.ALL: all of the above 以上全部

映射二级列表

使用类一级的 @SecondaryTable 和 @SecondaryTables 注解可以实现单个实体到多个表的
映射。使用 @Column 或者 @JoinColumn 注解中的 table 参数可以指定某个列所属的特定
表。

@Entity
@Table(name=”MainCat”)
@SecondaryTables({
@SecondaryTable(name=”Cat1″, pkJoinColumns={
@PrimaryKeyJoinColumn(name=”cat_id”, referencedColumnName=”id”)}),
@SecondaryTable(name=”Cat2″, uniqueConstraints={
@UniqueConstraint(columnNames={“storyPart2”})})
})
public class Cat implements Serializable {
private Integer id;
private String name;

private String storyPart1;
private String storyPart2;
@Id @GeneratedValue
public Integer getId() {
return id;
}
public String getName() {
return name;
}
@Column(table=”Cat1″)
public String getStoryPart1() {
return storyPart1;
}
@Column(table=”Cat2″)
public String getStoryPart2() {
return storyPart2;
}

上述例子中, name 保存在 MainCat 表中,storyPart1保存在 Cat1 表中,storyPart2 保存
在 Cat2 表中。 Cat1 表通过外键 cat_id 和 MainCat 表关联, Cat2 表通过 id 列和
MainCat 表关联。对storyPart2 列还定义了唯一约束。

映射查询

使用注解可以映射 EJBQL/HQL 查询,@NamedQuery 和 @NamedQueries 是可以使用在类
级别或者JPA的XML文件中的注解。



select p from Plane p




@Entity
@NamedQuery(name=”night.moreRecentThan”, query=”select n from Night n where
n.date >= :date”)
public class Night {

}
public class MyDao {
doStuff() {
Query q = s.getNamedQuery(“night.moreRecentThan”);
q.setDate( “date”, aMonthAgo );
List results = q.list();

}

}

可以通过定义 QueryHint 数组的 hints 属性为查询提供一些 hint 信息。下图是一些
Hibernate hints:

声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 智乐兔
转载请注明:转自《Hibernate常用注释收集6
本文地址:https://www.zhiletu.com/archives-277.html
关注公众号:智乐兔

赞赏

wechat pay微信赞赏alipay pay支付宝赞赏

上一篇
下一篇

相关文章

在线留言

你必须 登录后 才能留言!