Inheritance ing of Hibernate association relationship ing and hibernate Association
First, an Article class (Article) contains attributes such as id, title, content, and postTime.
package entity;import java.util.Date;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 content) {this.content = content;}public Date getPostTime() {return postTime;}public void setPostTime(Date postTime) {this.postTime = postTime;}}
Then let's look at its sub-classes, Topic classes and Reply classes. Apart from the attributes of the parent class, they also have their own unique attributes.
package entity;public class Topic extends Article{private int type;public int getType() {return type;}public void setType(int type) {this.type = type;}}
package entity;public class Reply extends Article{private int floor;public int getFloor() {return floor;}public void setFloor(int floor) {this.floor = floor;}}
There are three methods to inherit ing:
Method 1: store the information of the parent class and Child class in the same table. Then, there is a field in the data table to indicate the type of the record. The unique attribute of the Child class can be null. Let's take a look at the ing configuration file.
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
Method 2: the parent class and subclass are not in the same table, and each class has a table. The abstract class corresponds to a table. This is the configuration subclass using joined-subclass.
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
Method 3: each class has a separate table, and the abstract class does not correspond to a table. The database table corresponding to the subclass corresponds to all attributes, including information inherited from the parent class.
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
Then let's take a look at the test classes. The three methods are the same. Only the save methods in method 3 cannot store the information of their parent classes because the abstract classes do not have separate data tables.
Package test; import org. hibernate. session; import org. hibernate. transaction; import entity. article; import entity. reply; import entity. topic; import factory. hibernateSessionFactory; public class Test {private Session session = null; private Transaction tran = null; @ org. junit. testpublic void save () {session = HibernateSessionFactory. getSession (); tran = session. beginTransaction (); try {Article article = new Article (); article. setId (1); article. setTitle ("this is an Article"); Topic topic = new Topic (); topic. setId (2); topic. setTitle ("this is a Topic"); Reply reply = new Reply (); reply. setId (3); reply. setTitle ("this is a reply"); session. save (article); session. save (topic); session. save (reply); tran. commit ();} catch (Exception e) {tran. rollback () ;}@ org. junit. testpublic void Get () {session = HibernateSessionFactory. getSession (); tran = session. beginTransaction (); try {Article a = (Article) session. get (Article. class, 1); Topic t = (Topic) session. get (Topic. class, 2); Reply r = (Reply) session. get (Reply. class, 3); System. out. println (. getTitle (); System. out. println (t. getTitle (); System. out. println (r. getTitle (); tran. commit ();} catch (Exception e) {tran. rollback ();}}}
Inheritance relationship ing in hibernate
First, the primary key entity in the foreign key must be manually initialized (new primary key entity ())
Configuration saves time by adding fetch = "join" to the properties of <fail-to-one> and <set>"
In addition, only by searching for the hibernate primary key in hibernate will the entities in the foreign key be automatically assigned a value to the set in the primary key.
How to Implement hibernate relationship ing and inheritance? What is the concept?
* 1) the parent class and subclass have corresponding tables. Use <joined-subclass> to define the inheritance relationship. The procedure is as follows:
A. Generate a basic ing between the parent table and the child table for a single table
B. Add a subclass TO THE extends parent class.
C. Use <joined-subclass> to define subclass ing
From the version of Hibernate3.0, you can extract <joined-subclass> and put it in an hbm. xml file. The format is as follows:
<Hibernate-mapping>
<Joined-subclass name = "subclass type"
Extends = "parent class type"
Table = "subclass table">
<Key column = "subtable join field"/>
// Property field ing
</Joined-subclass>
</Hibernate-mapping>
* 2) the parent class and subclass use the same data table.
Multiple choice questions:
Number, question, difficulty, option, and answer
Short answer:
Number, question, difficulty, and answer
Data Table Question
Number, question, difficulty, option, answer selection, answer, question type
Use <subclass> to define subclass ing. The procedure is as follows:
A. Modify the object class and define it as the structure of the parent class and subclass.
B. Define the ing in the ing File
<Hibernate-mapping>
<Class name = "parent type" table = "data table">
// Define the ing of attributes in the parent class
<Discriminator column = "differentiate fields"/>
<Subclass name = "subclass 1"
Discriminator-value = "zone score">
// Define the ing of attributes in subclass 1
</Subclass>
<Subclass name = "subclass 2"
Discriminator-value = "zone score">
// Define the ing of attributes in subclass 2
</Subclass>
</Class>
<Hibernate-mapping>
The above two types of inheritance relationship ing. Note that the ing configured in <joined-subclass> corresponds to two tables and two entity classes. The IDS of the two tables must be one-to-one, that is, the same id represents a physical object, and another one defines the ing. Even if you use hibernate for query ...... remaining full text>