Problem description:
A person has a home address and a unit address. However, the "Address" does not mean the existence of a person. Therefore, it is a pojo that loses independence;
Therefore, the address cannot be mapped to an object. In this case, you need to map the address to a component and contain the address in the person's information. Is the relationship between the whole and the part.
However, there may be multiple addresses. Such as the company address, birth address, and home address. Therefore, you need to use map to store multiple
Address. Key indicates the address, and value indicates the corresponding address. How to map?
Package COM. ccay; <br/> Import Java. util. map; <br/> Import javax. persistence. elementcollection; <br/> Import javax. persistence. embedded; <br/> Import javax. persistence. entity; <br/> Import javax. persistence. generatedvalue; <br/> Import javax. persistence. ID; <br/> @ entity <br/> public class person {<br/> private int ID; <br/> private string name; <br/> private Map <string, address> address; </P> <p> @ elementcollection <br/> Public Map <string, address> getaddress () {<br/> return address; <br/>}< br/> Public void setaddress (Map <string, address> address) {<br/> This. address = address; <br/>}< br/> @ ID <br/> @ generatedvalue <br/> Public int GETID () {<br/> return ID; <br/>}< br/> Public void setid (int id) {<br/> This. id = ID; <br/>}< br/> Public String getname () {<br/> return name; <br/>}< br/> Public void setname (string name) {<br/> This. name = Name; <br/>}< br/>}
Package COM. ccay; <br/> Import javax. persistence. embeddable; </P> <p> @ embeddable <br/> Public Class address {<br/>/** <br/> * province <br/> */<br/> private string; <br/>/** <br/> * city <br/> */<br/> private string B; <br/>/** <br/> * County <br/> */<br/> private string C; <br/> Public String geta () {<br/> return a; <br/>}< br/> Public void Seta (string a) {<br/> This. A = A; <br/>}< br/> Public String getb () {<br/> return B; <br/>}< br/> Public void SETB (string B) {<br/> This. B = B; <br/>}< br/> Public String GETC () {<br/> return C; <br/>}< br/> Public void SETC (string c) {<br/> This. C = C; <br/>}< br/>
Package COM. ccay; <br/> Import Java. util. hashmap; <br/> Import Java. util. map; <br/> Import Org. hibernate. sessionfactory; <br/> Import Org. hibernate. cfg. annotationconfiguration; <br/> Import Org. hibernate. cfg. configuration; <br/> Import Org. hibernate. classic. session; <br/> Import Org. JUnit. test; </P> <p> public class mytest {</P> <p> @ test <br/> Public void test () {<br/> Configuration CFG = new annotationconfiguration (). configure (). configure (); <br/> sessionfactory = cfg. buildsessionfactory (); <br/> person P = new person (); <br/> P. setname ("Lala"); <br/> address a = new address (); <br/>. seta ("Shaanxi"); <br/>. SETB ("Baoji"); <br/>. SETC (" bin"); </P> <p> address B = new address (); <br/> B. seta ("Sichuan"); <br/> B. SETB ("Chengdu"); <br/> B. SETC ("Chengdu"); </P> <p> Map <string, address> adds = new hashmap <string, address> (); </P> <p> adds. put ("family", a); <br/> adds. put ("company", B); </P> <p> P. setaddress (ADDs); <br/> session = sessionfactory. opensession (); <br/> session. begintransaction (); <br/> session. save (p); <br/> session. gettransaction (). commit (); <br/> session. close (); <br/> sessionfactory. close (); <br/>}</P> <p >}< br/>
The table creation statement generated by hibernate:
Create Table 'person '(
'Id' int (11) not null auto_increment,
'Name' varchar (255) default null,
Primary Key ('id ')
) Engine = InnoDB auto_increment = 2 default charset = utf8;
Create Table 'person _ address '(
'Person _ id' int (11) not null,
'A' varchar (255) default null,
'B' varchar (255) default null,
'C' varchar (255) default null,
'Address _ key' varchar (255) not null default '',
Primary Key ('person _ id', 'address _ key '),
Key 'fke83f852a356feae3 '('person _ id '),
Constraint 'fke83f852a356feae3 'foreign key ('person _ id') References 'body' ('id ')
) Engine = InnoDB default charset = utf8;
Reverse Engineering legend: