Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

(14) Hibernate inheritance mapping

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

People don't know the nature of things until they lose them.

Demand: animals, cats, monkeys

1. Simple inheritance mapping

Animal.java

Package com.rk.hibernate.n_inheritance1;public abstract class Animal {private int id; private String name; public int getId () {return id;} public void setId (int id) {this.id = id;} public String getName () {return name } public void setName (String name) {this.name = name;}}

Cat.java

Package com.rk.hibernate.n_inheritance1;public class Cat extends Animal {private String catchMouse; public String getCatchMouse () {return catchMouse;} public void setCatchMouse (String catchMouse) {this.catchMouse = catchMouse @ Override public String toString () {return "Cat [id=" + getId () + ", name=" + getName () + ", catchMouse=" + catchMouse + "]";}}

Cat.hbm.xml

App.java

Package com.rk.hibernate.n_inheritance1;import java.util.List;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import org.junit.Test;public class App {private static SessionFactory sf Static {sf = new Configuration () .configure () .addClass (Cat.class) .buildSessionFactory () } @ Test public void testSave () {Session session = sf.openSession (); session.beginTransaction (); / / Cat Cat cat = new Cat (); cat.setName ("big cat") Cat.setCatchMouse ("catch mice"); / / Save session.save (cat); session.getTransaction (). Commit (); session.close () } @ Test public void testGetCat () {Session session = sf.openSession (); session.beginTransaction (); / / query Cat Query q = session.createQuery ("from Cat") through HQL; List list = q.list () System.out.println (list); session.getTransaction (). Commit (); session.close ();} @ Test public void testGetAnimal () {Session session = sf.openSession (); session.beginTransaction () / / Note when getting: when writing a hql query, the full name of the class Query Q = session.createQuery ("from com.rk.hibernate.n_inheritance1.Animal"); List list = q.list (); System.out.println (list) must be written through the parent query. Session.getTransaction (). Commit (); session.close ();}

Generated TCat table

Summary: simple inheritance mapping, how many subclasses, how many mapping files to write! 2. Inheritance mapping

The following three methods all use only one mapping file (Animal.hbm.xml file), but different methods generate a different number of database tables.

2.1. All subclasses are mapped to the same table (1 table)

Under what circumstances?

There are many subclasses, and the subclasses are relatively simple, that is, there are only individual attributes!

Benefit: because of the use of a mapping file, the number of mapping files is reduced.

Disadvantages: (does not comply with database design principles)

Animal.java

Package com.rk.hibernate.n_inheritance2;public abstract class Animal {private int id; private String name; public int getId () {return id;} public void setId (int id) {this.id = id;} public String getName () {return name } public void setName (String name) {this.name = name;}}

Cat.java

Package com.rk.hibernate.n_inheritance2;public class Cat extends Animal {private String catchMouse; public String getCatchMouse () {return catchMouse;} public void setCatchMouse (String catchMouse) {this.catchMouse = catchMouse @ Override public String toString () {return "Cat [id=" + getId () + ", name=" + getName () + ", catchMouse=" + catchMouse + "]";}}

Monkey.java

Package com.rk.hibernate.n_inheritance2;public class Monkey extends Animal {private String eatBanana; public String getEatBanana () {return eatBanana;} public void setEatBanana (String eatBanana) {this.eatBanana = eatBanana;}}

Animal.hbm.xml

App.java

Package com.rk.hibernate.n_inheritance2;import java.util.List;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import org.junit.Test;public class App {private static SessionFactory sf Static {sf = new Configuration () .configure () .addClass (Animal.class) .buildSessionFactory () } @ Test public void testSave () {Session session = sf.openSession (); session.beginTransaction (); / / Cat Cat cat = new Cat (); cat.setName ("big cat") Cat.setCatchMouse ("catching mice"); / / Monkey Monkey monkey = new Monkey (); monkey.setName ("naughty monkey"); monkey.setEatBanana ("eating yellow bananas"); / / Save session.save (cat) Session.save (monkey); session.getTransaction (). Commit (); session.close ();}}

Generated TAnimals table

Summary: the way to write it is relatively simple: all subclasses use a mapping file and map to a table! But the database design is unreasonable! It is not recommended.

2.2. Each class maps one table (3 tables)

Animal.java

Package com.rk.hibernate.n_inheritance3;public abstract class Animal {private int id; private String name; public int getId () {return id;} public void setId (int id) {this.id = id;} public String getName () {return name } public void setName (String name) {this.name = name;}}

Cat.java

Package com.rk.hibernate.n_inheritance3;public class Cat extends Animal {private String catchMouse; public String getCatchMouse () {return catchMouse;} public void setCatchMouse (String catchMouse) {this.catchMouse = catchMouse @ Override public String toString () {return "Cat [id=" + getId () + ", name=" + getName () + ", catchMouse=" + catchMouse + "]";}}

Monkey.java

Package com.rk.hibernate.n_inheritance3;public class Monkey extends Animal {private String eatBanana; public String getEatBanana () {return eatBanana;} public void setEatBanana (String eatBanana) {this.eatBanana = eatBanana;}}

Animal.hbm.xml

App.java

Package com.rk.hibernate.n_inheritance3;import java.util.List;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import org.junit.Test;public class App {private static SessionFactory sf Static {sf = new Configuration () .configure () .addClass (Animal.class) .buildSessionFactory () } @ Test public void testSave () {Session session = sf.openSession (); session.beginTransaction (); / / Cat Cat cat = new Cat (); cat.setName ("big cat") Cat.setCatchMouse ("catching mice"); / / Monkey Monkey monkey = new Monkey (); monkey.setName ("naughty monkey"); monkey.setEatBanana ("eating yellow bananas"); / / Save session.save (cat) Session.save (monkey); session.getTransaction (). Commit (); session.close ();}}

Generated Tunable Animal _ Each table

Generated Tunable Catcher _ Each table

Generated Tunable Monkey _ Each table

Summary: a mapping file that stores all subclasses; subclasses parent classes are corresponding tables; disadvantages: table structure is more responsible, insert a piece of subclass information, need to use 2 sql: insert to the parent class, insert to the subclass!

2.3. (recommended) each subclass maps one table, and the parent class does not correspond to the table (2 tables)

Animal.java

Package com.rk.hibernate.n_inheritance4;public abstract class Animal {private String id; private String name; public String getId () {return id;} public void setId (String id) {this.id = id;} public String getName () {return name } public void setName (String name) {this.name = name;}}

Cat.java

Package com.rk.hibernate.n_inheritance4;public class Cat extends Animal {private String catchMouse; public String getCatchMouse () {return catchMouse;} public void setCatchMouse (String catchMouse) {this.catchMouse = catchMouse @ Override public String toString () {return "Cat [id=" + getId () + ", name=" + getName () + ", catchMouse=" + catchMouse + "]";}}

Monkey.java

Package com.rk.hibernate.n_inheritance4;public class Monkey extends Animal {private String eatBanana; public String getEatBanana () {return eatBanana;} public void setEatBanana (String eatBanana) {this.eatBanana = eatBanana;}}

Animal.hbm.xml

App.java

Package com.rk.hibernate.n_inheritance4;import java.util.List;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import org.junit.Test;public class App {private static SessionFactory sf Static {sf = new Configuration () .configure () .addClass (Animal.class) .buildSessionFactory () } @ Test public void testSave () {Session session = sf.openSession (); session.beginTransaction (); / / Cat Cat cat = new Cat (); cat.setName ("big cat") Cat.setCatchMouse ("catching mice"); / / Monkey Monkey monkey = new Monkey (); monkey.setName ("naughty monkey"); monkey.setEatBanana ("eating yellow bananas"); / / Save session.save (cat) Session.save (monkey); session.getTransaction (). Commit (); session.close ();}}

The generated T_Animal_Final table, although the table will be generated, will not store data

Generated Tunable _ final table

Generated Tunable Monkey _ final table

Summary: all subclasses are written to a mapping file; the parent class does not correspond to the table; each subclass corresponds to a table.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report