There are many factors to consider in this article. Some of them are more detailed, but the configuration does not involve...
First, we need to understand why we need to use the JPA inheritance relationship! Let's use 2 simple objects to elaborate! Student, teacher!
Student: name, age, gender, learning
INSTRUCTOR: name, age, gender, teaching
From the above two categories, we can understand that the three attributes of students and teachers are the same. The only difference is the difference between learning and teaching!
In traditional mode, without using the inheritance relationship, it is necessary to create pojo for students and teachers...
Package CN. vicky. test01; </P> <p> Import Java. io. serializable; </P> <p> Import javax. persistence. column; <br/> Import javax. persistence. entity; <br/> Import javax. persistence. enumtype; <br/> Import javax. persistence. enumerated; <br/> Import javax. persistence. generatedvalue; <br/> Import javax. persistence. generationtype; <br/> Import javax. persistence. ID; </P> <p> @ entity <br/> public class student implements serializable {</P> <p> Private Static final long serialversionuid = 1l; </P> <p> // parameter </P> <p> @ ID <br/> @ generatedvalue (Strategy = generationtype. identity) <br/> private integer ID; </P> <p> @ column (length = 20, unique = true, nullable = false) <br/> private string name; </P> <p> @ Enumerated (enumtype. string) @ column (length = 5, nullable = false) <br/> private sex = sex. man; </P> <p> @ column (length = 20, nullable = false) <br/> private string study = "Java "; </P> <p> // getter & setter <br/>
Teacher. Java
Package CN. vicky. test01; </P> <p> Import Java. io. serializable; </P> <p> Import javax. persistence. column; <br/> Import javax. persistence. entity; <br/> Import javax. persistence. enumtype; <br/> Import javax. persistence. enumerated; <br/> Import javax. persistence. generatedvalue; <br/> Import javax. persistence. generationtype; <br/> Import javax. persistence. ID; </P> <p> @ entity <br/> public class teacher implements serializable {</P> <p> Private Static final long serialversionuid = 1l; </P> <p> // parameter </P> <p> @ ID <br/> @ generatedvalue (Strategy = generationtype. identity) <br/> private integer ID; </P> <p> @ column (length = 20, unique = true, nullable = false) <br/> private string name; </P> <p> @ Enumerated (enumtype. string) <br/> @ column (length = 5, nullable = false) <br/> private sex = sex. man; </P> <p> @ column (length = 20, nullable = false) <br/> private string teach = ""; </P> <p> // getter & setter <br/>
Compile the junit4 unit test class: jpatest. Java
Package CN. vicky. test01; </P> <p> Import javax. persistence. entitymanagerfactory; <br/> Import javax. persistence. persistence; </P> <p> Import Org. apache. log4j. logger; <br/> Import Org. JUnit. afterclass; <br/> Import Org. JUnit. assert; <br/> Import Org. JUnit. beforeclass; <br/> Import Org. JUnit. test; </P> <p> public class jpatest {</P> <p> Private Static final logger = logger. getlogger (jpatest. class. getname (); </P> <p> Private Static entitymanagerfactory factory; </P> <p> @ beforeclass <br/> Public static void setupbeforeclass () throws exception {<br/> factory = persistence. createentitymanagerfactory ("Vicky"); <br/>}</P> <p> @ test <br/> Public void testinit () {<br/> try {<br/> assert. assertnotnull (factory); <br/>} catch (exception e) {<br/> logger. error (E); <br/>}</P> <p> @ afterclass <br/> Public static void teardownafterclass () throws exception {<br/> factory. close (); <br/>}</P> <p >}< br/>
Run: view the generated database:
Mysql> DESC student;
+ ------- + ------------- + ------ + ----- + --------- + ---------------- +
| FIELD | type | null | key | default | extra |
+ ------- + ------------- + ------ + ----- + --------- + ---------------- +
| ID | int (11) | no | pri | null | auto_increment |
| Name | varchar (20) | no | uni | null |
| Sex | varchar (5) | no | null |
| Study | varchar (20) | no | null |
+ ------- + ------------- + ------ + ----- + --------- + ---------------- +
4 rows in SET (0.00 Sec)
Mysql> DESC teacher;
+ ------- + ------------- + ------ + ----- + --------- + ---------------- +
| FIELD | type | null | key | default | extra |
+ ------- + ------------- + ------ + ----- + --------- + ---------------- +
| ID | int (11) | no | pri | null | auto_increment |
| Name | varchar (20) | no | uni | null |
| Sex | varchar (5) | no | null |
| Teach | varchar (20) | no | null |
+ ------- + ------------- + ------ + ----- + --------- + ---------------- +
4 rows in SET (0.00 Sec)