hibernate中组件映射配置详细解析

         组合关系

                   一个类中包含了另外一个类。这2个类中就是组合关系。

                   需求: 汽车与车轮

    组件映射

        类组合关系的映射,也叫做组件映射!

        注意:组件类和被包含的组件类,共同映射到一张表!

       需求:汽车与车轮

      本例模仿汽车与车轮之间的组合关系来实现组合映射,下面是具体代码

1.hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <!-- 通常,一个session-factory节点代表一个数据库 -->
	<session-factory>
		<!-- 1.数据库连接配置 -->
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql:///hib-demo</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">123456</property>
		<!--数据库方法配置,hibernate在运行的时候,会根据不同的方言生成符合当前数据库语法的sql  -->
		<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
		
		<!-- 2.其他相关配置 -->
		   <!--2.1显示hibernate在运行的时候执行的sql语句  -->
		<property name="hibernate.show_sql">true</property>
		   <!-- 2.2格式化sql 
		<property name="hibernate.format_sql">true</property>-->
		<!--    2.3自动建表  -->
		<property name="hibernate.hbm2ddl.auto">create</property>
		<!-- 3.加载所有映射 
		<mapping resource="cn/itcast/a_hello/Employee.hbm.xml"/>-->
	</session-factory>
</hibernate-configuration>

2.Car

package cn.itcast.d_component;

public class Car {
	private int id;
	private String name;
	//车轮
	private Wheel wheel;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Wheel getWheel() {
		return wheel;
	}
	public void setWheel(Wheel wheel) {
		this.wheel = wheel;
	}
	
	

}

3.Wheel

package cn.itcast.d_component;
//车轮
public class Wheel {
	private int count;
	private int size;
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
	public int getSize() {
		return size;
	}
	public void setSize(int size) {
		this.size = size;
	}
	
	

}

4.Car.hbm.xml

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


<!--映射文件:映射一个实体类对象,描述一个对象最终可以直接保存对象数据到数据库中  -->
<!-- package:要映射的对象所在的包(可选,如果不指定,此文件下所有的类都要指定全路径) 
    auto-import 默认为true,在写HQL的时候自动导入包名
            如果指定为false,在写HQL的时候必须要写上类的全名-->
<hibernate-mapping package="cn.itcast.d_component">
	<class name="Car" table="t_car">
	   <id name="id">
	      <generator class="native"></generator>
	   </id>
	   <property name="name" length="20"></property>
	   <component name="wheel">
	       <property name="count"></property>
	       <property name="size"></property>
	   
	   </component>
	   
	</class>
	

</hibernate-mapping>

5.junit

package cn.itcast.d_component;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import org.junit.Test;

public class App {
	private static SessionFactory sf;
	static{
		sf=new Configuration()
		.configure()
		.addClass(Car.class)
		.buildSessionFactory();
	}
	@Test
	public void save(){
		Session session=sf.openSession();
		session.beginTransaction();
		//轮子
		Wheel wheel=new Wheel();
		wheel.setCount(23);
		wheel.setSize(88);
		
		Car car=new Car();
		car.setName("宝马");
		car.setWheel(wheel);
		
		session.save(car);
		
		
		
		session.getTransaction().commit();
		session.close();
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值