`
javne
  • 浏览: 66670 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

hibernate annotation 继承关系映射

阅读更多
@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;
	}

}

 

分享到:
评论
1 楼 qscchao 2011-07-28  
好复杂,请问博主这是那种继承方式?

相关推荐

    hibernate annotation 中文文档

    2.2.4. 映射继承关系 2.2.4.1. 每个类一张表 2.2.4.2. 每个类层次结构一张表 2.2.4.3. 连接的子类 2.2.4.4. 从父类继承的属性 2.2.5. 映射实体Bean的关联关系 2.2.5.1. 一对一(One-to-one) 2.2.5.2. 多对一(Many-to-...

    Hibernate继承映射(annotation)

    NULL 博文链接:https://cdxs2.iteye.com/blog/1934884

    hibernate annotation帮助文档

    2.2.4. 映射继承关系 2.2.4.1. 每个类一张表 2.2.4.2. 每个类层次结构一张表 2.2.4.3. 连接的子类 2.2.4.4. 从父类继承的属性 2.2.5. 映射实体Bean的关联关系 2.2.5.1. 一对一(One-to-one) 2.2.5.2. 多对一...

    精通Java Web整合开发(第2版)

    第12章 基于annotation注解技术的ssh 2整合开发 ...12.4.11 继承关系映射的annotation注解实现549 12.4.12 hibernate集合映射的annotation注解实现552 12.5 基于annotation的ssh 2整合开发554 12.6 小结563

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    9.1. 继承映射特性(Features of inheritance mappings) 16.1. 别名注射(alias injection names) 19.1. 缓存策略提供商(Cache Providers) 19.2. 各种缓存提供商对缓存并发策略的支持情况(Cache Concurrency ...

    Hibernate+中文文档

    9.1. 继承映射特性(Features of inheritance mappings) 16.1. 别名注射(alias injection names) 19.1. 缓存策略提供商(Cache Providers) 19.2. 各种缓存提供商对缓存并发策略的支持情况(Cache Concurrency ...

    hibernate3.2中文文档(chm格式)

    9.1. 继承映射特性(Features of inheritance mappings) 16.1. 别名注射(alias injection names) 19.1. 缓存策略提供商(Cache Providers) 19.2. 各种缓存提供商对缓存并发策略的支持情况(Cache Concurrency ...

    HibernateAPI中文版.chm

    9.1. 继承映射特性(Features of inheritance mappings) 16.1. 别名注射(alias injection names) 19.1. 缓存策略提供商(Cache Providers) 19.2. 各种缓存提供商对缓存并发策略的支持情况(Cache Concurrency ...

    Hibernate4_Inheritance_Annotation:该程序演示了如何使用 Annotations 在 Hibernate 中使用继承

    Hibernate4_Inheritance_Annotation 该程序演示了如何使用 Annotations 在 Hibernate 中使用继承。 Hibernate 支持 3 种类型的继承 每个具体类一个表 - 每个子类都有一个表,该表也具有超类的所有属性。 (TABLE_PER...

    Hibernate中文详细学习文档

    9.1.7. 隐式多态和其他继承映射混合使用 9.2. 限制 10. 与对象共事 10.1. Hibernate对象状态(object states) 10.2. 使对象持久化 10.3. 装载对象 10.4. 查询 10.4.1. 执行查询 10.4.2. 过滤集合 10.4.3. ...

    Hibernate注解.docx

    在Hibernate中使用注解,主要是为了替代映射文件,完成“类到表,属性到字段”的映射。 JPA提供了一套功能强大的注解。Hibernate直接使用了JPA的这套注解。当然,对于JPA中的一些不足,Hibernate又开发了一些自己的...

    Hibernate 中文 html 帮助文档

    9.1.7. 隐式多态和其他继承映射混合使用 9.2. 限制 10. 与对象共事 10.1. Hibernate对象状态(object states) 10.2. 使对象持久化 10.3. 装载对象 10.4. 查询 10.4.1. 执行查询 10.4.1.1. 迭代式获取结果(Iterating ...

    最全Hibernate 参考文档

    9.1.7. 隐式多态和其他继承映射混合使用 9.2. 限制 10. 与对象共事 10.1. Hibernate对象状态(object states) 10.2. 使对象持久化 10.3. 装载对象 10.4. 查询 10.4.1. 执行查询 10.4.1.1. 迭代式获取结果(Iterating ...

    hibernate 体系结构与配置 参考文档(html)

    9. 继承映射(Inheritance Mappings) 9.1. 三种策略 9.1.1. 每个类分层结构一张表(Table per class hierarchy) 9.1.2. 每个子类一张表(Table per subclass) 9.1.3. 每个子类一张表(Table per subclass),使用辨别...

    Hibernate教程

    10.1.7. 隐式多态和其他继承映射混合使用 10.2. 限制 11. 与对象共事 11.1. Hibernate对象状态(object states) 11.2. 使对象持久化 11.3. 装载对象 11.4. 查询 11.4.1. 执行查询 11.4.1.1. 迭代式获取结果...

    Hibernate注释大全收藏

    映射继承关系 EJB支持3种类型的继承。 • Table per Class Strategy: the &lt;union-class&gt; element in Hibernate 每个类一张表 • Single Table per Class Hierarchy Strategy: the &lt;subclass&gt; element in Hibernate...

    Hibernate3的帮助文档

    10.1.7. 隐式多态和其他继承映射混合使用 10.2. 限制 11. 与对象共事 11.1. Hibernate对象状态(object states) 11.2. 使对象持久化 11.3. 装载对象 11.4. 查询 11.4.1. 执行查询 11.4.1.1. 迭代式获取结果...

    hibernate3.04中文文档.chm

    10.1.7. 隐式多态和其他继承映射混合使用 10.2. 限制 11. 与对象共事 11.1. Hibernate对象状态(object states) 11.2. 使对象持久化 11.3. 装载对象 11.4. 查询 11.4.1. 执行查询 11.4.1.1. 迭代式获取结果...

Global site tag (gtag.js) - Google Analytics