示例代码:
Address.java
package cn.itcast.compopent;
/**
* 组件
*/
public class Address {
private String street;
private String city;
private String province;
private String zipcode;
//表示Address所属的整体类
private Customer customer;
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public Address() {
}
public Address(String street, String city, String province, String zipcode) {
this.street = street;
this.city = city;
this.province = province;
this.zipcode = zipcode;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
}
Customer.java
package cn.itcast.compopent;
/**
* 一的一端
*/
@SuppressWarnings("serial")
public class Customer implements java.io.Serializable {
private Integer id;
private String name;
private Address homeAddress;
private Address comAddress;
public Address getHomeAddress() {
return homeAddress;
}
public void setHomeAddress(Address homeAddress) {
this.homeAddress = homeAddress;
}
public Address getComAddress() {
return comAddress;
}
public void setComAddress(Address comAddress) {
this.comAddress = comAddress;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Customer.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="cn.itcast.compopent.Customer" table="customers02">
<id name="id" type="integer">
<column name="id"/>
<generator class="increment"/>
</id>
<property name="name" type="string">
<column name="name"/>
</property>
<!-- 配置组件映射
component标签:表示 Address类是Customer类的组成部分,在hibernate中称之为组件
-->
<component name="homeAddress" class="cn.itcast.compopent.Address">
<!-- 该配置表示Address是Customer类的一部分 -->
<parent name="customer"/>
<property name="street" type="string">
<column name="home_street"></column>
</property>
<property name="city" type="string">
<column name="home_city"></column>
</property>
<property name="province" type="string">
<column name="home_province"></column>
</property>
<property name="zipcode" type="string">
<column name="home_zipcode"></column>
</property>
</component>
<component name="comAddress" class="cn.itcast.compopent.Address">
<parent name="customer"/>
<property name="street" type="string">
<column name="com_street"></column>
</property>
<property name="city" type="string">
<column name="com_city"></column>
</property>
<property name="province" type="string">
<column name="com_province"></column>
</property>
<property name="zipcode" type="string">
<column name="com_zipcode"></column>
</property>
</component>
</class>
</hibernate-mapping>
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<!-- 配置数据库的方言,让hibernate知道连接的是哪个数据库-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 配置利用javaBean和映射文件生成数据库中的表
hibernate.hbm2ddl.auto值
* create:执行时,先查找该表是否存在,如存在先删除表,在创建表
* none:不能创建表,只能往表中插入数据,如表不存在抛出异常,默认值
* update:执行时,
情况一:
先查找该表是否存在,如果表存在,直接插入,如果表不存在,先创建表,再插入数据.
情况二:
先查找该表是否存在,如果表存在,但表的结构不一样,要修改表的结构
-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 显示hibernate生成的sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 显示格式化得sql语句 -->
<property name="hibernate.format_sql">true</property>
</session-factory>
</hibernate-configuration>
App.java
package cn.itcast.compopent;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
public class App {
private static SessionFactory sf=null;
static{
Configuration config=new Configuration();
config.configure("cn/itcast/compopent/hibernate.cfg.xml");
config.addClass(Customer.class);
sf=config.buildSessionFactory();
}
/*
* 知识点1:测试插入
*/
@Test
public void saveCustomer(){
Session session=sf.openSession();
Transaction tx=session.beginTransaction();
Customer c=new Customer();
c.setName("高秀敏");
Address homeAddress=new Address("上地三街","北京","北京","100000");
Address comAddress=new Address("深圳街","长春","吉林省","100006");
c.setHomeAddress(homeAddress);
c.setComAddress(comAddress);
session.save(c);
tx.commit();
session.close();
}
//知识点2:测试查询
@Test
public void getCustomer(){
Session session=sf.openSession();
Transaction tx=session.beginTransaction();
Customer c=(Customer)session.get(Customer.class, 2);
System.out.println(c.getId()+" "+c.getName());
if(c.getHomeAddress()!=null){
System.out.println(c.getHomeAddress().getCity()+" "+c.getHomeAddress().getProvince());
}
if(c.getComAddress()!=null){
System.out.println(c.getComAddress().getCity()+" "+c.getComAddress().getProvince());
}
tx.commit();
session.close();
}
//知识点3:测试更新
@Test
public void updateCustomer(){
Session session=sf.openSession();
Transaction tx=session.beginTransaction();
Customer c=(Customer)session.get(Customer.class, 2);
c.setName("楚留香");
tx.commit();
session.close();
}
//知识点4:测试特殊的更新
@Test
public void updateOtherCustomer(){
Session session=sf.openSession();
Transaction tx=session.beginTransaction();
Customer c=(Customer)session.get(Customer.class, 2);
System.out.println(c.getId()+" "+c.getName());
Address ha=c.getHomeAddress();
if(ha==null){
System.out.println("homeAddress为空");
ha=new Address("上地七街","海淀区","北京","100000");
c.setHomeAddress(ha);
}
tx.commit();
session.close();
}
}