Java tutorial
/* * Copyright 2005-2013 shopxx.net. All rights reserved. * Support: http://www.shopxx.net * License: http://www.shopxx.net/license */ package com.lingxiang2014.entity; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.OrderBy; import javax.persistence.SequenceGenerator; import javax.persistence.Table; import javax.persistence.Transient; import org.apache.commons.lang.StringUtils; import org.hibernate.validator.constraints.Length; import org.hibernate.validator.constraints.NotEmpty; @Entity @Table(name = "lx_article_category") @SequenceGenerator(name = "sequenceGenerator", sequenceName = "lx_article_category_sequence") public class ArticleCategory extends OrderEntity { private static final long serialVersionUID = -5132652107151648662L; public static final String TREE_PATH_SEPARATOR = ","; private static final String PATH_PREFIX = "/article/list"; private static final String PATH_SUFFIX = ".jhtml"; private String name; private String seoTitle; private String seoKeywords; private String seoDescription; private String treePath; private Integer grade; private ArticleCategory parent; private Set<ArticleCategory> children = new HashSet<ArticleCategory>(); private Set<Article> articles = new HashSet<Article>(); @NotEmpty @Length(max = 200) @Column(nullable = false) public String getName() { return name; } public void setName(String name) { this.name = name; } @Length(max = 200) public String getSeoTitle() { return seoTitle; } public void setSeoTitle(String seoTitle) { this.seoTitle = seoTitle; } @Length(max = 200) public String getSeoKeywords() { return seoKeywords; } public void setSeoKeywords(String seoKeywords) { this.seoKeywords = seoKeywords; } @Length(max = 200) public String getSeoDescription() { return seoDescription; } public void setSeoDescription(String seoDescription) { this.seoDescription = seoDescription; } @Column(nullable = false) public String getTreePath() { return treePath; } public void setTreePath(String treePath) { this.treePath = treePath; } @Column(nullable = false) public Integer getGrade() { return grade; } public void setGrade(Integer grade) { this.grade = grade; } @ManyToOne(fetch = FetchType.LAZY) public ArticleCategory getParent() { return parent; } public void setParent(ArticleCategory parent) { this.parent = parent; } @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY) @OrderBy("order asc") public Set<ArticleCategory> getChildren() { return children; } public void setChildren(Set<ArticleCategory> children) { this.children = children; } @OneToMany(mappedBy = "articleCategory", fetch = FetchType.LAZY) public Set<Article> getArticles() { return articles; } public void setArticles(Set<Article> articles) { this.articles = articles; } @Transient public List<Long> getTreePaths() { List<Long> treePaths = new ArrayList<Long>(); String[] ids = StringUtils.split(getTreePath(), TREE_PATH_SEPARATOR); if (ids != null) { for (String id : ids) { treePaths.add(Long.valueOf(id)); } } return treePaths; } @Transient public String getPath() { if (getId() != null) { return PATH_PREFIX + "/" + getId() + PATH_SUFFIX; } return null; } }