Hibernate_10 _ inherit from instance _ single table
Create only one table. All attributes are contained in this table. Use discriminatorTo distinguish the parent class from the Child class.
1) parent class (Article ):
public class Article {private Integer id;private String title;private String content;private Date postTime;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getContent() {return content;}public void setContent(String string) {this.content = string;}public Date getPostTime() {return postTime;}public void setPostTime(Date postTime) {this.postTime = postTime;}@Overridepublic String toString() {return "Article [id=" + id + ", title=" + title + ", content="+ content + ", postTime=" + postTime + "]";}}
2) subclass (Topic ):
public class Topic extends Article {private Integer type;public Integer getType() {return type;}public void setType(Integer type) {this.type = type;}@Overridepublic String toString() {return "Topic [id=" + getId() + ", title=" + getTitle() + ", content=" + getContent() + ", postTime=" + getPostTime() + ", type=" + type + "]";}}
3) subclass (Reply ):
public class Reply extends Article {private Integer floor;public Integer getFloor() {return floor;}public void setFloor(Integer floor) {this.floor = floor;}@Overridepublic String toString() {return "Reply [id=" + getId() + ", title=" + getTitle() + ", content=" + getContent() + ", postTime=" + getPostTime() + ", floor=" + floor + "]";}}
4) Persistence Layer:
Package extends_1; import java. SQL. date; import org. hibernate. session; import org. hibernate. transaction; import org. junit. test; public class ExtendsDao {/*** save Method */@ Testpublic void testSave () {Session session = SessionFactoryTools. getSession (); Transaction tx = null; try {tx = session. beginTransaction (); // ================================================ ====/// create an object and set the attribute Article article = new Article (); article. s EtTitle ("article"); article. setContent ("You are My Rose! "); Article. setPostTime (new Date (20140731); Topic topic = new Topic (); topic. setTitle ("topic"); Reply reply = new Reply (); reply. setTitle ("reply"); // Save the object session. save (article); session. save (topic); session. save (reply ); // ================================================ ====== tx. commit ();} catch (RuntimeException e) {if (tx! = Null) {tx. rollback ();} throw e;} finally {session. close () ;}/ *** getById Method */@ Testpublic void testGetById () {Session session = SessionFactoryTools. getSession (); Transaction tx = null; try {tx = session. beginTransaction (); // ================================================ ==============================/// read data and output Article article = (Article) session. get (Article. class, 1); System. out. println (article); Article topic = (Article) Session. get (Topic. class, 2); System. out. println (topic); Article reply = (Article) session. get (Reply. class, 3); System. out. println (reply ); // ================================================ ================================= tx. commit ();} catch (RuntimeException e) {if (tx! = Null) {tx. rollback () ;}throw e ;}finally {session. close ();}}}
5) configure the Article. hbm. xml file:
6) configure the master file.