@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 Annotation 中文文档》是Java开发者们的重要参考资料,它详细介绍了如何在Hibernate框架中使用注解进行对象关系映射(ORM)。Hibernate是一款强大的开源Java持久化框架,它简化了数据库与Java对象之间的...
在Java开发领域,Hibernate作为一种强大的对象关系映射(ORM)框架,极大地简化了数据库操作。本篇将深入探讨Hibernate 3版本中的注解使用,帮助开发者理解如何利用注解进行数据持久化,提高开发效率。 一、...
在Hibernate中,注解(Annotation)是一种声明式的方法,用于配置实体类、属性以及它们与数据库表之间的映射关系。本文将深入探讨“最全的Hibernate Annotation API文档”中的关键知识点。 一、实体类(Entity) 在...
**标题:“Hibernate继承映射(Annotation)详解”** 在Java持久化框架Hibernate中,继承映射是一种关键特性,它允许开发者将对象模型的继承结构映射到数据库的表结构。在传统的面向对象编程中,继承是实现代码复用和...
而Hibernate Annotation则是Hibernate提供的一种基于注解的实体映射方式,它极大地简化了传统XML配置文件的繁琐工作,使得开发过程更加简洁高效。本文将通过一个实际操作的实例,逐步介绍Hibernate Annotation的基础...
- 使用InheritanceType.JOINED或SINGLE_TABLE进行继承映射,根据需求选择合适的策略。 - 利用`@Temporal`处理日期和时间类型,确保与数据库的兼容性。 - 使用`@Cacheable`和`@Cache`注解提升查询效率,通过缓存...
2. **映射机制**: Hibernate支持XML映射文件(hbm.xml)和注解映射两种方式,让对象与数据库表之间建立映射关系。 3. **Session和Transaction**: Session是Hibernate的主要工作单元,负责保存、更新和检索对象。...
在Hibernate 3.x版本中,引入了Annotation注解,这是一种元数据的方式,可以替代XML配置文件来描述对象与数据库表之间的映射关系。 **Hibernate Annotation注解** 在Hibernate 3.x之前,对象到数据库的映射通常...
- **继承关系映射**:`@Inheritance`定义了不同的继承策略,如单表、每个类一张表或连接子类等。 - **关联关系映射**:`@OneToOne`、`@ManyToOne`、`@OneToMany`和`@ManyToMany`注解分别对应一对一、多对一、一对...
### Hibernate Annotation 帮助文档知识点总结 #### 1. 创建注解项目 - **系统需求**:在开始创建一个支持 Hibernate 注解的项目之前,需要确保满足以下系统需求: - Java 开发环境(例如 JDK 1.8 或更高版本)。...
5. 注解式继承:通过`@Inheritance`和`@DiscriminatorColumn`可以实现类的继承关系映射。 二、SLF4J介绍与使用 SLF4J(Simple Logging Facade for Java)是一个日志抽象层,为不同的日志实现(如log4j、logback)...
7. **继承映射**:Hibernate支持单表继承(SINGLE_TABLE)、联合继承(JOINED)和表-per-hierarchy(TABLE_PER_CLASS)。使用`@Inheritance`和`@DiscriminatorColumn`、`@DiscriminatorValue`注解可以定义继承策略。...
Hibernate是Java领域中广泛使用的对象关系映射(ORM)工具,它极大地简化了数据库操作。在3.4.0版本中,Hibernate引入了注解支持,以替代XML配置文件,使代码更加简洁和直观。 **1. Hibernate注解的基本概念** ...
- **简单属性映射**:使用 `@Column` 注解来指定 Java 属性和数据库列之间的映射关系。 - **标识符属性映射**:使用 `@Id` 注解来指定实体的唯一标识符。 - **继承映射**:支持多种继承映射策略,如单表继承、...
#### 2.4.4 继承映射 - **2.2.4.1 每个类一张表**:`@Inheritance(strategy = InheritanceType.SINGLE_TABLE)`。 - **2.2.4.2 单表继承**:`@DiscriminatorColumn`和`@DiscriminatorValue`用于区分子类实例。 - **...