Hibernate配置一对多关系对像操作;
类1:
public class HouseOwner {
private Long id;
private String familyOwner;
private String familyNo;
private String familyTel;
private Long totalFee;
private Long availableFee;
private Set<MailName> mailNameList;
}
其中一个成员为mailNameList Set,这里有一对多关系;
类2:
private String familyNo;
}
需要定义两个映射关系:
<class name="HouseOwner" table="house_owner"
dynamic-insert="true" dynamic-update="true" lazy="false">
<id name="id" type="long">
<generator class="native"/>
</id>
<property name="familyOwner" type="string" not-null="true" unique="true"></property>
<property name="familyNo" type="string" not-null="true" ></property>
<property name="familyTel" type="string" not-null="true" ></property>
<property name="totalFee" type="long" ></property>
<property name="availableFee" type="long" ></property>
<set name="mailNameList" cascade="all-delete-orphan"
lazy="false">
<key>
<column name="child_id"/>
</key>
<one-to-many class="MailName" />
</set>
</class>
<class name="MailName"
table="mail_name" lazy="false" dynamic-insert="true" dynamic-update="true">
<id name="id" type="long">
<generator class="native"/>
</id>
<property name="mailName" type="string" ></property>
<property name="familyNo" type="string" ></property>
</class>
关键问题是建表,需要指定一个外键关系,外键即是一个引用,在java对象和对应的XML映射文件里没有定义,而是在引用对象那个类里定义的
这里即是需要在HouseOwner的关系里定义,仔细理解一下这是符合逻辑的。
建表SQL:
1、HouseOwner类的表:CREATE TABLE house_owner (id BIGINT NOT NULL AUTO_INCREMENT, familyOwner VARCHAR(255) NOT NULL,familyNo VARCHAR(255) NOT NULL,familyTel VARCHAR(255) NOT NULL, totalFee BIGINT NOT NULL, availableFee BIGINT NOT NULL,PRIMARY KEY (id));
2、MailName的表:
CREATE TABLE `mail_name` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`child_id` BIGINT,
`mailName` VARCHAR(255) DEFAULT NULL,
`familyNo` VARCHAR(255) DEFAULT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`child_id`) REFERENCES house_owner (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE
);
如此便可以进行一个对象的操作,这样一个对象的数据用多个表来存而做业务时完全不关心怎么去存,比较方便。
每个表的主键ID设置为自增模式,而且这个字段不负责业务表示,纯粹用于数据库管理。
类1:
public class HouseOwner {
private Long id;
private String familyOwner;
private String familyNo;
private String familyTel;
private Long totalFee;
private Long availableFee;
private Set<MailName> mailNameList;
}
其中一个成员为mailNameList Set,这里有一对多关系;
类2:
public class MailName {
private long id;
private String mailName;private String familyNo;
}
需要定义两个映射关系:
<class name="HouseOwner" table="house_owner"
dynamic-insert="true" dynamic-update="true" lazy="false">
<id name="id" type="long">
<generator class="native"/>
</id>
<property name="familyOwner" type="string" not-null="true" unique="true"></property>
<property name="familyNo" type="string" not-null="true" ></property>
<property name="familyTel" type="string" not-null="true" ></property>
<property name="totalFee" type="long" ></property>
<property name="availableFee" type="long" ></property>
<set name="mailNameList" cascade="all-delete-orphan"
lazy="false">
<key>
<column name="child_id"/>
</key>
<one-to-many class="MailName" />
</set>
</class>
<class name="MailName"
table="mail_name" lazy="false" dynamic-insert="true" dynamic-update="true">
<id name="id" type="long">
<generator class="native"/>
</id>
<property name="mailName" type="string" ></property>
<property name="familyNo" type="string" ></property>
</class>
关键问题是建表,需要指定一个外键关系,外键即是一个引用,在java对象和对应的XML映射文件里没有定义,而是在引用对象那个类里定义的
这里即是需要在HouseOwner的关系里定义,仔细理解一下这是符合逻辑的。
建表SQL:
1、HouseOwner类的表:CREATE TABLE house_owner (id BIGINT NOT NULL AUTO_INCREMENT, familyOwner VARCHAR(255) NOT NULL,familyNo VARCHAR(255) NOT NULL,familyTel VARCHAR(255) NOT NULL, totalFee BIGINT NOT NULL, availableFee BIGINT NOT NULL,PRIMARY KEY (id));
2、MailName的表:
CREATE TABLE `mail_name` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`child_id` BIGINT,
`mailName` VARCHAR(255) DEFAULT NULL,
`familyNo` VARCHAR(255) DEFAULT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`child_id`) REFERENCES house_owner (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE
);
如此便可以进行一个对象的操作,这样一个对象的数据用多个表来存而做业务时完全不关心怎么去存,比较方便。