标签:
CREATE TABLE tb_customer
(
id bigint NOT NULL auto_increment COMMENT ‘ID‘,
name varchar(50) NOT NULL COMMENT ‘客户名称‘,
home_address bigint COMMENT ‘客户家庭地址‘,
PRIMARY KEY (id)
) COMMENT = ‘客户信息表‘;
CREATE TABLE tb_address
(
id bigint NOT NULL auto_increment COMMENT ‘ID‘,
zipcode varchar(50) NOT NULL COMMENT ‘邮政编码‘,
address varchar(200) NOT NULL COMMENT ‘地址‘,
PRIMARY KEY (id)
) COMMENT = ‘地址信息表‘;
-- 可选的外键约束
ALTER TABLE tb_customer ADD CONSTRAINT fk_tb_customer_tb_address_1 FOREIGN KEY (home_address) REFERENCES tb_address (id);
CREATE TABLE tb_customer
(
id bigint NOT NULL auto_increment COMMENT ‘ID‘,
name varchar(50) NOT NULL COMMENT ‘客户名称‘,
PRIMARY KEY (id)
) COMMENT = ‘客户信息表‘;
CREATE TABLE tb_address
(
id bigint NOT NULL auto_increment COMMENT ‘ID‘,
zipcode varchar(50) NOT NULL COMMENT ‘邮政编码‘,
address varchar(200) NOT NULL COMMENT ‘地址‘,
PRIMARY KEY (id)
) COMMENT = ‘地址信息表‘;
-- 可选的外键约束
- ALTER TABLE tb_address ADD CONSTRAINT fk_tb_address_tb_customer_1 FOREIGN KEY (id) REFERENCES tb_customer (id);
package model;
public class Address
{
private Long id;
private String zipcode;
private String address;
private Customer customer;
@Override
public String toString()
{
return "Address [id=" + id + ", zipcode=" + zipcode + ", address=" + address + ", customer=" + customer.getName() + "]";
}
// 省略setter、getter...
}
package model;
public class Customer
{
private Long id;
private String name;
private Address homeAddress;
@Override
public String toString()
{
return "Customer [id=" + id + ", name=" + name + ", homeAddress=" + homeAddress.getAddress() + "]";
}
// 省略setter、getter...
}
<hibernate-mapping package="model">
<class name="Address" table="tb_address">
<id name="id">
<generator class="native"></generator>
</id>
<property name="zipcode" column="zipcode" />
<property name="address" column="address" />
<one-to-one name="customer" class="model.Customer" property-ref="homeAddress"/>
</class>
</hibernate-mapping>
<hibernate-mapping package="model">
<class name="Customer" table="tb_customer">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name" column="name" />
<many-to-one name="homeAddress" class="model.Address" column="home_address" unique="true" cascade="all"/>
</class>
</hibernate-mapping>
public static void main(String[] args)
{
Customer customer;
Address address;
Configuration cfg = new Configuration();
cfg.configure();
ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
SessionFactory sf = cfg.buildSessionFactory(sr);
System.out.println("连接数据库");
Session session = sf.openSession();
Transaction transaction = session.beginTransaction();
try
{
customer=new Customer();
address=new Address();
customer.setName("测试客户01");
address.setZipcode("123456");
address.setAddress("湖北省武汉市");
customer.setHomeAddress(address);
address.setCustomer(customer);
session.save(customer);
//配置使用了级联保存transaction.commit();
}
catch (Exception e)
{
transaction.rollback();
System.out.println("错误:" + e);
}
finally
{
session.close();
System.out.println("关闭数据库");
}
System.exit(0);
}
连接数据库
Hibernate: insert into tb_address (zipcode, address) values (?, ?)
Hibernate: insert into tb_customer (name, home_address) values (?, ?)
关闭数据库
<hibernate-mapping package="model">
<class name="Address" table="tb_address">
<id name="id">
<generator class="foreign">
<param name="property">customer</param>
</generator>
</id>
<property name="zipcode" column="zipcode" />
<property name="address" column="address" />
<one-to-one name="customer" class="model.Customer" constrained="true"/>
</class>
</hibernate-mapping>
<hibernate-mapping package="model">
<class name="Customer" table="tb_customer">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name" column="name" />
<one-to-one name="homeAddress" class="model.Address" cascade="all"/>
</class>
</hibernate-mapping>
连接数据库
Hibernate: insert into tb_customer (name) values (?)
Hibernate: insert into tb_address (zipcode, address) values (?, ?)
关闭数据库
<one-to-one name="PropertyName"
access="field|property"
class="ClassName"
property-ref="PropertyNameFromAssociatedClass"
constrained="true|false"
formula="arbitrary SQL expression"
cascade="all|none|save-update|delete"
fetch="join|select"
outer-join="true|false"
foreign-key="foreign-key"
lazy="true|false"
embed-xml="true|false"
entity-name="EntityName"
node="element-name"/>
标签:
原文地址:http://www.cnblogs.com/LiZhiW/p/4279005.html