@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;
}
}
分享到:
相关推荐
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-...
NULL 博文链接:https://cdxs2.iteye.com/blog/1934884
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. 多对一...
第12章 基于annotation注解技术的ssh 2整合开发 ...12.4.11 继承关系映射的annotation注解实现549 12.4.12 hibernate集合映射的annotation注解实现552 12.5 基于annotation的ssh 2整合开发554 12.6 小结563
9.1. 继承映射特性(Features of inheritance mappings) 16.1. 别名注射(alias injection names) 19.1. 缓存策略提供商(Cache Providers) 19.2. 各种缓存提供商对缓存并发策略的支持情况(Cache Concurrency ...
9.1. 继承映射特性(Features of inheritance mappings) 16.1. 别名注射(alias injection names) 19.1. 缓存策略提供商(Cache Providers) 19.2. 各种缓存提供商对缓存并发策略的支持情况(Cache Concurrency ...
9.1. 继承映射特性(Features of inheritance mappings) 16.1. 别名注射(alias injection names) 19.1. 缓存策略提供商(Cache Providers) 19.2. 各种缓存提供商对缓存并发策略的支持情况(Cache Concurrency ...
9.1. 继承映射特性(Features of inheritance mappings) 16.1. 别名注射(alias injection names) 19.1. 缓存策略提供商(Cache Providers) 19.2. 各种缓存提供商对缓存并发策略的支持情况(Cache Concurrency ...
Hibernate4_Inheritance_Annotation 该程序演示了如何使用 Annotations 在 Hibernate 中使用继承。 Hibernate 支持 3 种类型的继承 每个具体类一个表 - 每个子类都有一个表,该表也具有超类的所有属性。 (TABLE_PER...
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中使用注解,主要是为了替代映射文件,完成“类到表,属性到字段”的映射。 JPA提供了一套功能强大的注解。Hibernate直接使用了JPA的这套注解。当然,对于JPA中的一些不足,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 ...
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 ...
9. 继承映射(Inheritance Mappings) 9.1. 三种策略 9.1.1. 每个类分层结构一张表(Table per class hierarchy) 9.1.2. 每个子类一张表(Table per subclass) 9.1.3. 每个子类一张表(Table per subclass),使用辨别...
10.1.7. 隐式多态和其他继承映射混合使用 10.2. 限制 11. 与对象共事 11.1. Hibernate对象状态(object states) 11.2. 使对象持久化 11.3. 装载对象 11.4. 查询 11.4.1. 执行查询 11.4.1.1. 迭代式获取结果...
映射继承关系 EJB支持3种类型的继承。 • Table per Class Strategy: the <union-class> element in Hibernate 每个类一张表 • Single Table per Class Hierarchy Strategy: the <subclass> element in 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. 迭代式获取结果...
10.1.7. 隐式多态和其他继承映射混合使用 10.2. 限制 11. 与对象共事 11.1. Hibernate对象状态(object states) 11.2. 使对象持久化 11.3. 装载对象 11.4. 查询 11.4.1. 执行查询 11.4.1.1. 迭代式获取结果...