数据库中Delivery表:id,recipient,phone,address,postcode,delivery_type,parcel_number,express_company,express_number
新建java projet项目:chapter14_hierarch
Add Hibernate Capalities
在项目的src目录下面:com.b510.examples包中手工编写:
Delivery.java PostDelivery.java ExpressDelivery.java(其中:PostDeliver.java和ExpressDelivery.java都继承Delivery.java)
Delivery.java
代码:
/**
*
*/
package com.b510.examples;
/**
*
* @author XHW
*
* @date 2011-7-9
*
*/
public class Delivery {
private Integer id;
private String recipient;
private String phone;
private String address;
private String postcode;
/**
* @return the id
*/
public Integer getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(Integer id) {
this.id = id;
}
/**
* @return the recipient
*/
public String getRecipient() {
return recipient;
}
/**
* @param recipient the recipient to set
*/
public void setRecipient(String recipient) {
this.recipient = recipient;
}
/**
* @return the phone
*/
public String getPhone() {
return phone;
}
/**
* @param phone the phone to set
*/
public void setPhone(String phone) {
this.phone = phone;
}
/**
* @return the address
*/
public String getAddress() {
return address;
}
/**
* @param address the address to set
*/
public void setAddress(String address) {
this.address = address;
}
/**
* @return the postcode
*/
public String getPostcode() {
return postcode;
}
/**
* @param postcode the postcode to set
*/
public void setPostcode(String postcode) {
this.postcode = postcode;
}
}
PostDelivery.java
代码:
/**
*
*/
package com.b510.examples;
/**
*
* @author XHW
*
* @date 2011-7-9
*
*/
public class PostDelivery extends Delivery{
private String parcelNumber;
/**
* @return the parcelNumber
*/
public String getparcelNumber() {
return parcelNumber;
}
/**
* @param parcelNumber the parcelNumber to set
*/
public void setparcelNumber(String parcelNumber) {
this.parcelNumber = parcelNumber;
}
}
ExpressDelivery.java
代码:
/**
*
*/
package com.b510.examples;
/**
*
* @author XHW
*
* @date 2011-7-9
*
*/
public class ExpressDelivery extends Delivery {
private String expressNumber;
private String expressCompany;
/**
* @return the expressNumber
*/
public String getExpressNumber() {
return expressNumber;
}
/**
* @param expressNumber the expressNumber to set
*/
public void setExpressNumber(String expressNumber) {
this.expressNumber = expressNumber;
}
/**
* @return the expressCompany
*/
public String getExpressCompany() {
return expressCompany;
}
/**
* @param expressCompany the expressCompany to set
*/
public void setExpressCompany(String expressCompany) {
this.expressCompany = expressCompany;
}
}
用Hibernate逆向工程单独生成Delivery.hbm.xml映射文件:
Delivery.hbm.xml
代码:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.b510.examples.Delivery" table="delivery" catalog="users">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="increment" />
</id>
<!-- 在这里加入判别是那一种邮递方式 -->
<discriminator column="delivery_type"></discriminator>
<property name="recipient" type="java.lang.String">
<column name="recipient" length="200" />
</property>
<property name="phone" type="java.lang.String">
<column name="phone" length="200" />
</property>
<property name="address" type="java.lang.String">
<column name="address" length="400" />
</property>
<property name="postcode" type="java.lang.String">
<column name="postcode" length="10" />
</property>
<!-- 为delivery_type 传递值 Post和Express -->
<subclass name="com.b510.examples.PostDelivery" discriminator-value="Post">
<property name="parcelNumber" type="java.lang.String">
<column name="parcel_number" length="30" />
</property>
</subclass>
<subclass name="com.b510.examples.ExpressDelivery" discriminator-value="Express">
<property name="expressCompany" type="java.lang.String">
<column name="express_company" length="40" />
</property>
<property name="expressNumber" type="java.lang.String">
<column name="express_number" length="40" />
</property>
</subclass>
</class>
</hibernate-mapping>
HibernateTest.java
代码:
/**
*
*/
package com.b510.examples;
import org.hibernate.Session;
import org.hibernate.Transaction;
/**
*
* @author XHW
*
* @date 2011-7-9
*
*/
public class HibernateTest {
/**
* @param args
*/
public static void main(String[] args) {
new HibernateTest().insertDelivery();
}
public void insertDelivery() {
Session session = HibernateSessionFactoryUtil.getSessionFactory()
.openSession();
Transaction tx = session.beginTransaction();
PostDelivery post = new PostDelivery();
post.setRecipient("Hongten");
post.setAddress("广州中医药大学");
post.setPhone("119");
post.setPostcode("510006");
post.setparcelNumber("订单号:abc123f23");
ExpressDelivery express = new ExpressDelivery();
express.setRecipient("HongWei");
express.setAddress("云南昆明");
express.setPhone("123465");
express.setPostcode("456789");
express.setExpressNumber("订单号:456qwe87ewer");
express.setExpressCompany("顺丰快递公司");
try {
session.save(post);
session.save(express);
} catch (Exception e) {
e.printStackTrace();
if (tx.isActive()) {
tx.rollback();
}
}
session.getTransaction().commit();
}
}
运行效果:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate:
select
max(id)
from
delivery
Hibernate:
insert
into
users.delivery
(recipient, phone, address, postcode, parcel_number, delivery_type, id)
values
(?, ?, ?, ?, ?, 'Post', ?)
Hibernate:
insert
into
users.delivery
(recipient, phone, address, postcode, express_company, express_number, delivery_type, id)
values
(?, ?, ?, ?, ?, ?, 'Express', ?)