Hibernate_10 _ inherit from instance _ single table

Source: Internet
Author: User

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.







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.