一、
1.
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping 3 PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping > 6 7 <class name="mypack.Monkey" table="MONKEYS" > 8 <id name="id" type="long" column="ID"> 9 <generator class="increment"/> 10 </id> 11 12 <property name="name" type="string" column="NAME" /> 13 14 <component name="homeAddress" class="mypack.Address"> 15 <parent name="monkey" /> 16 <property name="province" type="string" column="HOME_PROVINCE"/> 17 <property name="city" type="string" column="HOME_CITY"/> 18 <property name="street" type="string" column="HOME_STREET"/> 19 <property name="zipcode" type="string" column="HOME_ZIPCODE"/> 20 </component> 21 22 <component name="comAddress" class="mypack.Address"> 23 <parent name="monkey" /> 24 <property name="province" type="string" column="COM_PROVINCE"/> 25 <property name="city" type="string" column="COM_CITY"/> 26 <property name="street" type="string" column="COM_STREET"/> 27 <property name="zipcode" type="string" column="COM_ZIPCODE"/> 28 </component> 29 </class> 30 31 </hibernate-mapping>
2.
1 package mypack; 2 public class Monkey { 3 4 5 private long id; 6 private String name; 7 private Address homeAddress; 8 private Address comAddress; 9 10 public Monkey() { 11 } 12 13 public Monkey(String name, Address homeAddress, Address comAddress) { 14 this.name = name; 15 this.homeAddress = homeAddress; 16 this.comAddress = comAddress; 17 } 18 19 public long getId() { 20 return this.id; 21 } 22 23 public void setId(long id) { 24 this.id = id; 25 } 26 public String getName() { 27 return this.name; 28 } 29 30 public void setName(String name) { 31 this.name = name; 32 } 33 public Address getHomeAddress() { 34 return this.homeAddress; 35 } 36 37 public void setHomeAddress(Address homeAddress) { 38 this.homeAddress = homeAddress; 39 } 40 public Address getComAddress() { 41 return this.comAddress; 42 } 43 44 public void setComAddress(Address comAddress) { 45 this.comAddress = comAddress; 46 } 47 48 49 50 51 }
3.
1 package mypack; 2 public class Address { 3 4 5 private String province; 6 private String city; 7 private String street; 8 private String zipcode; 9 private Monkey monkey; 10 11 public Address() { 12 } 13 14 public Address(String province, String city, String street, String zipcode,Monkey monkey) { 15 this.province = province; 16 this.city = city; 17 this.street = street; 18 this.zipcode = zipcode; 19 this.monkey=monkey; 20 } 21 22 public String getProvince() { 23 return this.province; 24 } 25 26 public void setProvince(String province) { 27 this.province = province; 28 } 29 public String getCity() { 30 return this.city; 31 } 32 33 public void setCity(String city) { 34 this.city = city; 35 } 36 public String getStreet() { 37 return this.street; 38 } 39 40 public void setStreet(String street) { 41 this.street = street; 42 } 43 public String getZipcode() { 44 return this.zipcode; 45 } 46 47 public void setZipcode(String zipcode) { 48 this.zipcode = zipcode; 49 } 50 51 public Monkey getMonkey() { 52 return this.monkey; 53 } 54 55 public void setMonkey(Monkey monkey) { 56 this.monkey = monkey; 57 } 58 59 60 61 }
二、电脑例子
4.
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping 3 PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping > 6 7 <class name="mypack.Computer" table="COMPUTERS" > 8 <id name="id" type="long" column="ID"> 9 <generator class="increment"/> 10 </id> 11 12 <property name="type" type="string" > 13 <column name="COMPUTER_TYPE" /> 14 </property> 15 16 <component name="cpuBox" class="mypack.CpuBox"> 17 <parent name="computer" /> 18 19 <property name="type" type="string" > 20 <column name="CPUBOX_TYPE" /> 21 </property> 22 23 <component name="graphicsCard" class="mypack.GraphicsCard"> 24 <parent name="cpuBox" /> 25 26 <property name="type" type="string" > 27 <column name="GRAPHICSCARD_TYPE" /> 28 </property> 29 30 </component> 31 32 <many-to-one 33 name="vendor" 34 column="CPUBOX_VENDOR_ID" 35 class="mypack.Vendor" 36 not-null="true" 37 /> 38 </component> 39 </class> 40 41 42 </hibernate-mapping>
5.
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping 3 PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping > 6 7 <class name="mypack.Vendor" table="VENDORS" > 8 <id name="id" type="long" column="ID"> 9 <generator class="increment"/> 10 </id> 11 12 <property name="type" type="string" > 13 <column name="TYPE" length="15" /> 14 </property> 15 16 17 </class> 18 19 </hibernate-mapping>
6.
1 package mypack; 2 public class Computer { 3 4 5 private long id; 6 private String type; 7 private CpuBox cpuBox; 8 9 public Computer() { 10 } 11 12 13 public Computer(CpuBox cpuBox) { 14 this.cpuBox = cpuBox; 15 } 16 public Computer(String type, CpuBox cpuBox) { 17 this.type = type; 18 this.cpuBox = cpuBox; 19 } 20 21 public long getId() { 22 return this.id; 23 } 24 25 public void setId(long id) { 26 this.id = id; 27 } 28 public String getType() { 29 return this.type; 30 } 31 32 public void setType(String type) { 33 this.type = type; 34 } 35 public CpuBox getCpuBox() { 36 return this.cpuBox; 37 } 38 39 public void setCpuBox(CpuBox cpuBox) { 40 this.cpuBox = cpuBox; 41 } 42 43 44 45 46 }
7.
1 package mypack; 2 public class CpuBox { 3 4 5 private String type; 6 private GraphicsCard graphicsCard; 7 private Vendor vendor; 8 private Computer computer; 9 public CpuBox() { 10 } 11 12 13 public CpuBox(Vendor vendor) { 14 this.vendor = vendor; 15 } 16 public CpuBox(String type, GraphicsCard graphicsCard, Vendor vendor,Computer computer) { 17 this.type = type; 18 this.graphicsCard = graphicsCard; 19 this.vendor = vendor; 20 this.computer=computer; 21 } 22 23 public String getType() { 24 return this.type; 25 } 26 27 public void setType(String type) { 28 this.type = type; 29 } 30 public GraphicsCard getGraphicsCard() { 31 return this.graphicsCard; 32 } 33 34 public void setGraphicsCard(GraphicsCard graphicsCard) { 35 this.graphicsCard = graphicsCard; 36 } 37 public Vendor getVendor() { 38 return this.vendor; 39 } 40 41 public void setVendor(Vendor vendor) { 42 this.vendor = vendor; 43 } 44 45 public Computer getComputer() { 46 return this.computer; 47 } 48 49 public void setComputer(Computer computer) { 50 this.computer=computer; 51 } 52 53 54 55 }
8.
1 package mypack; 2 3 4 5 public class GraphicsCard { 6 7 8 private String type; 9 private CpuBox cpuBox; 10 11 public GraphicsCard() { 12 } 13 14 public GraphicsCard(String type,CpuBox cpuBox) { 15 this.type = type; 16 this.cpuBox=cpuBox; 17 } 18 19 public String getType() { 20 return this.type; 21 } 22 23 public void setType(String type) { 24 this.type = type; 25 } 26 27 public CpuBox getCpuBox() { 28 return this.cpuBox; 29 } 30 31 public void setCpuBox(CpuBox cpuBox) { 32 this.cpuBox = cpuBox; 33 } 34 35 36 }
9.
1 package mypack; 2 public class Vendor { 3 4 5 private long id; 6 private String type; 7 8 public Vendor() { 9 } 10 11 public Vendor(String type) { 12 this.type = type; 13 } 14 15 public long getId() { 16 return this.id; 17 } 18 19 public void setId(long id) { 20 this.id = id; 21 } 22 public String getType() { 23 return this.type; 24 } 25 26 public void setType(String type) { 27 this.type = type; 28 } 29 30 31 32 33 }
10.
1 use sampledb; 2 drop table if exists COMPUTERS; 3 drop table if exists MONKEYS; 4 drop table if exists VENDORS; 5 6 create table MONKEYS (ID bigint not null, NAME varchar(15), HOME_PROVINCE varchar(255), 7 HOME_CITY varchar(255), HOME_STREET varchar(255), HOME_ZIPCODE varchar(255), 8 COM_PROVINCE varchar(255), COM_CITY varchar(255), 9 COM_STREET varchar(255), COM_ZIPCODE varchar(255), primary key (ID)); 10 11 create table COMPUTERS (ID bigint not null, COMPUTER_TYPE varchar(15), 12 CPUBOX_TYPE varchar(15), GRAPHICSCARD_TYPE varchar(15), 13 CPUBOX_VENDOR_ID bigint not null, primary key (ID)); 14 15 create table VENDORS (ID bigint not null, TYPE varchar(15), primary key (ID)); 16 17 alter table COMPUTERS add index IDX_VENDOR (CPUBOX_VENDOR_ID), 18 add constraint FK_VENDOR foreign key (CPUBOX_VENDOR_ID) 19 references VENDORS (ID);
11.
1 <?xml version="1.0" encoding="utf-8" ?> 2 <!DOCTYPE hibernate-configuration 3 PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" 4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5 6 <hibernate-configuration> 7 <session-factory> 8 <property name="dialect"> 9 org.hibernate.dialect.MySQLDialect 10 </property> 11 <property name="connection.driver_class"> 12 com.mysql.jdbc.Driver 13 </property> 14 <property name="connection.url"> 15 jdbc:mysql://localhost:3306/sampledb 16 </property> 17 <property name="connection.username"> 18 root 19 </property> 20 <property name="connection.password"> 21 1234 22 </property> 23 24 <property name="show_sql">true</property> 25 26 <mapping resource="mypack/Monkey.hbm.xml" /> 27 <mapping resource="mypack/Computer.hbm.xml" /> 28 <mapping resource="mypack/Vendor.hbm.xml" /> 29 30 </session-factory> 31 </hibernate-configuration>