package com.fang.bbks.modules.sys.entity; import java.io.Serializable; import java.util.List; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.OrderBy; import javax.persistence.Table; import javax.persistence.Transient; import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; import org.hibernate.annotations.NotFound; import org.hibernate.annotations.NotFoundAction; import org.hibernate.annotations.Where; import com.fang.bbks.common.persistence.BaseEntity; import com.google.common.collect.Lists; /** * @Intro 书籍分类表 * @author Lee * @Date 2013-8-1 */ @SuppressWarnings("serial") @Entity @Table(name = "TB_CATEGORY") @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) public class Category extends BaseEntity{ @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Long id; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name="parent_id") @NotFound(action = NotFoundAction.IGNORE) @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) private Category parent;// 父级菜单 @OneToMany(cascade = {CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REMOVE},fetch=FetchType.LAZY,mappedBy="parent") @Where(clause="del_flag='"+DEL_FLAG_NORMAL+"'") @NotFound(action = NotFoundAction.IGNORE) @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) private List<Category> childList = Lists.newArrayList(); private String name; // 分类名称 private String intro; // 分类描述 private String delFlag; //删除标记(0:正常;1:删除) /** * */ public Category() { this.delFlag = DEL_FLAG_NORMAL; } public Category(Long id) { super(); this.id = id; } public Category(String name,String intro) { super(); this.name = name; this.intro = intro; } public String getDelFlag() { return delFlag; } public void setDelFlag(String delFlag) { this.delFlag = delFlag; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public Category getParent() { return parent; } public void setParent(Category parent) { this.parent = parent; } public List<Category> getChildList() { return childList; } public void setChildList(List<Category> childList) { this.childList = childList; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getIntro() { return intro; } public void setIntro(String intro) { this.intro = intro; } @Transient public static void sortList(List<Category> list, List<Category> sourcelist, Long parentId){ for (int i=0; i<sourcelist.size(); i++){ Category e = sourcelist.get(i); if (e.getParent()!=null && e.getParent().getId()!=null && e.getParent().getId().equals(parentId)){ list.add(e); // 判断是否还有子节点, 有则继续获取子节点 for (int j=0; j<sourcelist.size(); j++){ Category child = sourcelist.get(j); if (child.getParent()!=null && child.getParent().getId()!=null && child.getParent().getId().equals(e.getId())){ sortList(list, sourcelist, e.getId()); break; } } } } } @Transient public boolean isRoot(){ return isRoot(this.id); } @Transient public static boolean isRoot(Long id){ return id != null && id.equals(1L); } }