Hibernate Annotations(四)
普通一对多(延迟加载方式)
@OneToMany(mappedBy = "troop", cascade = ...{CascadeType.ALL}, fetch = FetchType.LAZY)
@OrderBy(clause = "name desc")
public Set<Soldier> getSoldiers() ...{
return soldiers;
}
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "troop_fk")
public Troop getTroop() ...{
return troop;
}
DeleteOrphan模式
@OneToMany(mappedBy = "troop", cascade = ...{CascadeType.ALL}, fetch = FetchType.LAZY)
@OrderBy(clause = "name desc")
@org.hibernate.annotations.Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
public Set<Soldier> getSoldiers() ...{
return soldiers;
}
troop.getSoldiers().clear();
s = openSession();
tx = s.beginTransaction();
s.merge( troop );
tx.commit();
s.close();
OneToManySet
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "CUST_ID")
@Sort(type = SortType.COMPARATOR, comparator = TicketComparator.class)
public SortedSet<Ticket> getTickets() ...{
return tickets;
}
public class TicketComparator implements Comparator<Ticket> ...{
public int compare(Ticket ticket, Ticket ticket1) ...{
if ( ticket == null || ticket1 == null ) ...{
throw new IllegalStateException( "Ticket comparison only available through non null tickets" );
}
return ticket1.getNumber().length() - ticket.getNumber().length();
}
}
OneToManyCollection
@OneToMany(targetEntity = org.hibernate.test.annotations.Discount.class,
cascade = CascadeType.ALL, mappedBy = "owner")
@Cascade(...{ALL})
public Collection getDiscountTickets() ...{
return discountTickets;
}
JoinColumns
@OneToMany(cascade = CascadeType.ALL, mappedBy = "parent")
@BatchSize(size = 5)
@javax.persistence.OrderBy("favoriteSuperhero asc, favoriteSinger desc")
public Set<Child> children;
@ManyToOne()
@JoinColumns(...{
@JoinColumn(name = "parentCivility", referencedColumnName = "isMale"),
@JoinColumn(name = "parentLastName", referencedColumnName = "lastName"),
@JoinColumn(name = "parentFirstName", referencedColumnName = "firstName")
})
public Parent parent;
@Column(name = "fav_sup_hero")
public String favoriteSuperhero;
@Column(name = "fav_singer")
public String favoriteSinger;
注:cascade的值只能从CascadeType.PERSIST(级联新建)、CascadeType.REMOVE(级联删除)、CascadeType.REFRESH(级联刷新)、CascadeType.MERGE(级联更新)中选择一个或多个。还有一个选择是使用CascadeType.ALL,表示选择全部四项。
<
补充:Jsp教程,Java基础
- 更多JSP疑问解答:
- jsp新手求指导,不要笑!
- 如何让一个form提取的值传递给多个jsp?
- DW中,新建的html页面能否有jsp或php代码?
- jsp 如何限制表单,实现只能填写特定的数据。
- jsp 和javabean结合的程序有问题
- 从数据库里取出的数据如何传递到另外的jsp页面中
- 你好,ext嵌入那个jsp页面,是不是还需要加上一些插件啊,不太懂,麻烦你了。
- JSP不能处理所有问题吗?还要来一大堆的TLD,TAG,XML。为JSP 非要 Servlet 不可吗?
- 光标离开时全角转半角在jsp中怎么实现
- jsp 页面 打开 pdf 文件 控制大小 和 工具栏 能发份源码么 谢啦
- jsp页面点保存按钮,运行缓慢,弹出对话框提示
- jsp刷新页面如何不闪屏
- jsp 与html 的交互问题?
- jsp小数显示问题 例如 我在oracle 数据库中查询出来的是 0.01 但是在jsp页面上就显示成 .01 没有前面的0
- jsp中日历控件