在JPA API中,javax.persistence.CascadeType类中定义了一些常量,分别表示特定的级联操做:java
当经过注解来映射持久化类时,若是但愿使用底层Hibernate的一些级联特性,那么还能够使用org.hibernate.annotations. CascadeType类的一些常量,例如:数据库
例如如下@OneToMany注解的cascade属性的取值为“org.hibernate.annotations.CascadeType.SAVE_UPDATE”:缓存
@OneToMany(mappedBy="parentCategory", targetEntity=Category.class) @org.hibernate.annotations.Cascade( org.hibernate.annotations.CascadeType.SAVE_UPDATE) private Set<Category> childCategories = new HashSet<Category>(0);
Category类是具备自身双向关联的类,它的childCategories属性以及parentCategory属性,进行了以下映射:markdown
@OneToMany(mappedBy="parentCategory", targetEntity=Category.class, cascade=CascadeType.ALL, fetch=FetchType.EAGER) private Set<Category> childCategories = new HashSet<Category>(0); //子商品类别 @ManyToOne(targetEntity =Category.class, cascade=CascadeType.ALL, fetch=FetchType.EAGER) @JoinColumn(name="CATEGORY_ID") private Category parentCategory; //父商品类别
对于以上两个属性,它们的级联操做都是CascadeType.ALL,这意味着对当前的Category对象进行特定操做时,会对所关联的父类别Category对象,以及所关联的全部子类别Category对象进行一样的级联操做。
另外,为了保证从数据库中加载一个Category对象时,会当即加载所关联的父类别和子类别Category对象,采用了当即检索策略:FetchType.EAGER。app