欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

hibernate annotation 继承关系映射

程序员文章站 2022-04-14 09:14:49
...
@Entity
@org.hibernate.annotations.Entity(polymorphism = PolymorphismType.IMPLICIT)
@Table(name = "article")
@Inheritance(strategy = InheritanceType.JOINED)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@NamedQueries( {
		@NamedQuery(name = "SelectAricle", query = "select new Aricle(id,title,subTitle,hits,addTime,tag) from Aricle"),
		@NamedQuery(name = "CountSelectAricle", query = "select count(*) from Aricle"),
		@NamedQuery(name = "findAriclebyId", query = "select new Aricle(id,title,subTitle,hits,addTime,tag) from Aricle where id=?"),
		@NamedQuery(name = "SelectAricleWithCategory", query = "select new Aricle(id,title,subTitle,hits,addTime,category) from Aricle aricle"),
		@NamedQuery(name = "SelectAricleWithCategoryId", query = "select new Aricle(id,title,subTitle,hits,addTime,category.id) from Aricle aricle")

})
public class Aricle extends IdEntity<Integer> {

	private static final long serialVersionUID = -8056490229900614401L;
	private String title;
	private String subTitle;
	private Date addTime;
	private Category category;
	private Set<BlogTags> blogTags = new LinkedHashSet<BlogTags>();
	private Integer hits;
	private String tag;//沉字段
	private Set<Comments> comments=new LinkedHashSet<Comments>();
	public String getTitle() {
		return title;
	}

	public Aricle() {
		super();
	}
	public Aricle(Integer id, String title, String subTitle){
		super.setId(id);
		this.title = title;
		this.subTitle = subTitle;
	}
	public Aricle(Integer id, String title, String subTitle, Integer hits, Date addTime,String tag) {
		super.setId(id);
		this.title = title;
		this.subTitle = subTitle;
		this.addTime = addTime;
		this.hits = hits;
		this.tag=tag;
	}

	public Aricle(Integer id, String title, String subTitle, Integer hits, Date addTime, Category category) {
		super.setId(id);
		this.title = title;
		this.subTitle = subTitle;
		this.addTime = addTime;
		this.category = category;
		this.hits = hits;
	}

	public Aricle(Integer id, String title, String subTitle, Integer hits, Date addTime, Integer categoryId) {
		super.setId(id);
		this.title = title;
		this.subTitle = subTitle;
		this.hits = hits;
		this.addTime = addTime;
		Category category = new Category();
		category.setId(categoryId);
		//category.setName(name);
		this.category = category;
	}

	public Aricle(Integer id, String title, String subTitle, Integer hits, Date addTime, Integer categoryId, String name) {
		super.setId(id);
		this.title = title;
		this.subTitle = subTitle;
		this.hits = hits;
		this.addTime = addTime;
		Category category = new Category();
		category.setId(categoryId);
		category.setName(name);
		this.category = category;
	}

	public Aricle(Integer id, String title, String subTitle, Integer hits, Date addTime, Integer categoryId,
			String name, Set<BlogTags> blogTags) {
		super.setId(id);
		this.title = title;
		this.subTitle = subTitle;
		this.hits = hits;
		this.addTime = addTime;
		Category category = new Category();
		category.setId(categoryId);
		category.setName(name);
		this.category = category;
		this.blogTags = blogTags;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@Column(nullable = true, columnDefinition = "varchar(145) default ''")
	public String getSubTitle() {
		return subTitle;
	}

	public void setSubTitle(String subTitle) {
		this.subTitle = subTitle;
	}

	@Temporal(TemporalType.TIMESTAMP)
	public Date getAddTime() {
		return addTime;
	}

	@Transient
	public String getAddTimeStr() {
		if (getAddTime() == null)
			return "";
		return DateFormatUtils.MIN_FORMAT.format(getAddTime());
	}

	@Transient
	public String getAddTimeYMD() {
		if (getAddTime() == null)
			return "";
		return DateFormatUtils.DATE_FORMAT.format(getAddTime());
	}

	@Transient
	public String getAddTimeMS() {
		if (getAddTime() == null)
			return "";
		return DateFormatUtils.format(getAddTime(), "HH:mm");
	}

	public void setAddTime(Date addTime) {
		this.addTime = addTime;
	}

	@ManyToOne(targetEntity = Category.class, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
	@JoinColumn(name = "category_id", referencedColumnName = "id")
	@Fetch(FetchMode.SELECT)
	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	@Transient
	public String getCategoryName() {
		if (getCategory() == null)
			return "";
		return getCategory().getName();
	}

	@Transient
	/**从数据库中获取ID*/
	public Integer getCategoryId() {
		if (getCategory() == null)
			return null;
		return getCategory().getId();
	}

	@Transient
	public Integer getCategorysId() {
		if (category == null)
			return null;
		return category.getId();
	}

	@Column(insertable = false, updatable = false)
	public Integer getHits() {
		return hits;
	}

	public void setHits(Integer hits) {
		this.hits = hits;
	}

	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "tagsr_rlation", joinColumns = { @JoinColumn(name = "ArticlesID", referencedColumnName = "id") }, inverseJoinColumns = { @JoinColumn(name = "TagsID", referencedColumnName = "id") })
	//Fecth策略定义
	//集合按id排序.
	@OrderBy("nums desc")
	//集合中对象id的缓存.
	@Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public Set<BlogTags> getBlogTags() {
		return blogTags;
	}

	public void setBlogTags(Set<BlogTags> blogTags) {
		this.blogTags = blogTags;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 0;
		result = prime * result + ((title == null) ? 0 : title.hashCode());
		result += prime * getId();
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		final Aricle other = (Aricle) obj;
		if (title == null) {
			if (other.title != null)
				return false;
		} else if (!title.equals(other.title))
			return false;
		if (getId() == null) {
			if (other.getId() != null)
				return false;
		} else if (!(getId() == other.getId()))
			return false;
		return true;
	}

	@Transient
	public String getTags() {
		StringBuffer sBuffer = new StringBuffer();
		for (Iterator<BlogTags> iterator = blogTags.iterator(); iterator.hasNext();) {
			sBuffer.append(iterator.next().getName()).append(",");
		}
		if (sBuffer.length() > 1)
			return sBuffer.substring(0, sBuffer.length() - 1);
		return null;
	}
	@Column(name="tags")
	public String getTag() {
		return tag;
	}

	public void setTag(String tag) {
		this.tag = tag;
	}
	@OneToMany(mappedBy = "aricle", fetch = FetchType.LAZY)
	@Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
	@OrderBy("id desc")
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public Set<Comments> getComments() {
		return comments;
	}

	public void setComments(Set<Comments> comments) {
		this.comments = comments;
	}
	
}

 子类:

@Entity  
@Table(name = "article_data")
@PrimaryKeyJoinColumn(name="article_id",referencedColumnName="id")
//@DiscriminatorValue("article_data")   
public class AricleDetail  extends Aricle{
	/**
	 * 
	 */
	private static final long serialVersionUID = 2467125353876220860L;
	private String content;
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}

}

 

相关标签: Hibernate Cache