您的位置:首页 > 其它

Hibernate逍遥游记-第8章 映射组成关系(<component>、<parent>)

2016-03-20 15:29 260 查看
一、

1.

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping >

<class name="mypack.Monkey" table="MONKEYS" >
<id name="id" type="long" column="ID">
<generator class="increment"/>
</id>

<property name="name" type="string" column="NAME" />

<component name="homeAddress" class="mypack.Address">
<parent name="monkey" />
<property name="province" type="string" column="HOME_PROVINCE"/>
<property name="city" type="string" column="HOME_CITY"/>
<property name="street" type="string" column="HOME_STREET"/>
<property name="zipcode" type="string" column="HOME_ZIPCODE"/>
</component>

<component name="comAddress" class="mypack.Address">
<parent name="monkey" />
<property name="province" type="string" column="COM_PROVINCE"/>
<property name="city" type="string" column="COM_CITY"/>
<property name="street" type="string" column="COM_STREET"/>
<property name="zipcode" type="string" column="COM_ZIPCODE"/>
</component>
</class>

</hibernate-mapping>








2.

package mypack;
public class Monkey {

private long id;
private String name;
private Address homeAddress;
private Address comAddress;

public Monkey() {
}

public Monkey(String name, Address homeAddress, Address comAddress) {
this.name = name;
this.homeAddress = homeAddress;
this.comAddress = comAddress;
}

public long getId() {
return this.id;
}

public void setId(long id) {
this.id = id;
}
public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}
public Address getHomeAddress() {
return this.homeAddress;
}

public void setHomeAddress(Address homeAddress) {
this.homeAddress = homeAddress;
}
public Address getComAddress() {
return this.comAddress;
}

public void setComAddress(Address comAddress) {
this.comAddress = comAddress;
}

}


3.

package mypack;
public class Address {

private String province;
private String city;
private String street;
private String zipcode;
private Monkey monkey;

public Address() {
}

public Address(String province, String city, String street, String zipcode,Monkey monkey) {
this.province = province;
this.city = city;
this.street = street;
this.zipcode = zipcode;
this.monkey=monkey;
}

public String getProvince() {
return this.province;
}

public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return this.city;
}

public void setCity(String city) {
this.city = city;
}
public String getStreet() {
return this.street;
}

public void setStreet(String street) {
this.street = street;
}
public String getZipcode() {
return this.zipcode;
}

public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}

public Monkey getMonkey() {
return this.monkey;
}

public void setMonkey(Monkey monkey) {
this.monkey = monkey;
}

}


二、电脑例子

4.

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping >

<class name="mypack.Computer" table="COMPUTERS" >
<id name="id" type="long" column="ID">
<generator class="increment"/>
</id>

<property name="type" type="string" >
<column name="COMPUTER_TYPE" />
</property>

<component name="cpuBox" class="mypack.CpuBox">
<parent name="computer" />

<property name="type" type="string" >
<column name="CPUBOX_TYPE" />
</property>

<component name="graphicsCard" class="mypack.GraphicsCard">
<parent name="cpuBox" />

<property name="type" type="string" >
<column name="GRAPHICSCARD_TYPE" />
</property>

</component>

<many-to-one
name="vendor"
column="CPUBOX_VENDOR_ID"
class="mypack.Vendor"
not-null="true"
/>
</component>
</class>

</hibernate-mapping>


5.

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping >

<class name="mypack.Vendor" table="VENDORS" >
<id name="id" type="long" column="ID">
<generator class="increment"/>
</id>

<property name="type" type="string" >
<column name="TYPE" length="15" />
</property>

</class>

</hibernate-mapping>


6.

package mypack;
public class Computer {

private long id;
private String type;
private CpuBox cpuBox;

public Computer() {
}

public Computer(CpuBox cpuBox) {
this.cpuBox = cpuBox;
}
public Computer(String type, CpuBox cpuBox) {
this.type = type;
this.cpuBox = cpuBox;
}

public long getId() {
return this.id;
}

public void setId(long id) {
this.id = id;
}
public String getType() {
return this.type;
}

public void setType(String type) {
this.type = type;
}
public CpuBox getCpuBox() {
return this.cpuBox;
}

public void setCpuBox(CpuBox cpuBox) {
this.cpuBox = cpuBox;
}

}


7.

package mypack;
public class CpuBox {

private String type;
private GraphicsCard graphicsCard;
private Vendor vendor;
private Computer computer;
public CpuBox() {
}

public CpuBox(Vendor vendor) {
this.vendor = vendor;
}
public CpuBox(String type, GraphicsCard graphicsCard, Vendor vendor,Computer computer) {
this.type = type;
this.graphicsCard = graphicsCard;
this.vendor = vendor;
this.computer=computer;
}

public String getType() {
return this.type;
}

public void setType(String type) {
this.type = type;
}
public GraphicsCard getGraphicsCard() {
return this.graphicsCard;
}

public void setGraphicsCard(GraphicsCard graphicsCard) {
this.graphicsCard = graphicsCard;
}
public Vendor getVendor() {
return this.vendor;
}

public void setVendor(Vendor vendor) {
this.vendor = vendor;
}

public Computer getComputer() {
return this.computer;
}

public void setComputer(Computer computer) {
this.computer=computer;
}

}


8.

package mypack;

public class GraphicsCard  {

private String type;
private CpuBox cpuBox;

public GraphicsCard() {
}

public GraphicsCard(String type,CpuBox cpuBox) {
this.type = type;
this.cpuBox=cpuBox;
}

public String getType() {
return this.type;
}

public void setType(String type) {
this.type = type;
}

public CpuBox getCpuBox() {
return this.cpuBox;
}

public void setCpuBox(CpuBox cpuBox) {
this.cpuBox = cpuBox;
}

}


9.

package mypack;
public class Vendor {

private long id;
private String type;

public Vendor() {
}

public Vendor(String type) {
this.type = type;
}

public long getId() {
return this.id;
}

public void setId(long id) {
this.id = id;
}
public String getType() {
return this.type;
}

public void setType(String type) {
this.type = type;
}

}


10.

use sampledb;
drop table if exists COMPUTERS;
drop table if exists MONKEYS;
drop table if exists VENDORS;

create table MONKEYS (ID bigint not null, NAME varchar(15), HOME_PROVINCE varchar(255),
HOME_CITY varchar(255), HOME_STREET varchar(255), HOME_ZIPCODE varchar(255),
COM_PROVINCE varchar(255), COM_CITY varchar(255),
COM_STREET varchar(255), COM_ZIPCODE varchar(255), primary key (ID));

create table COMPUTERS (ID bigint not null, COMPUTER_TYPE varchar(15),
CPUBOX_TYPE varchar(15), GRAPHICSCARD_TYPE varchar(15),
CPUBOX_VENDOR_ID bigint not null, primary key (ID));

create table VENDORS (ID bigint not null, TYPE varchar(15), primary key (ID));

alter table COMPUTERS add index IDX_VENDOR (CPUBOX_VENDOR_ID),
add constraint FK_VENDOR foreign key (CPUBOX_VENDOR_ID)
references VENDORS (ID);


11.

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/sampledb
</property>
<property name="connection.username">
root
</property>
<property name="connection.password">
1234
</property>

<property name="show_sql">true</property>

<mapping resource="mypack/Monkey.hbm.xml" />
<mapping resource="mypack/Computer.hbm.xml" />
<mapping resource="mypack/Vendor.hbm.xml" />

</session-factory>
</hibernate-configuration>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: