hibernate 映射-一对多双向

本文介绍了一对多关系映射在Spring、Hibernate和Struts2框架中的实现方法。通过商品与超市之间的关系实例,详细解析了如何配置实体类及Hibernate映射文件,并解释了延迟加载机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目名称:shop_goods

使用spring ,hibernate,struts2,分别的版本如下:

spring :3.2.3.RELEASE

hibernate:4.2.2.Final

struts2:2.3.4.1

使用xml配置,使用maven构建。

这里涉及两个实体类:商品,超市。因为要讲解一对多的关系映射,所以假设商品和超市之间是多对一联系。

一个超市有多个商品,一个商品只属于一个超市。

实体类代码如下(省略setter,getter方法)

 

package com.shop.jn.entity;

import java.io.Serializable;
import java.sql.Date;

/**
 * entity:goods  商品
 * @author huangwei
 *
 */
public class Goods  implements Serializable{
	private static final long serialVersionUID = 586940311263079808L;
	private int id;
	/**
	 * goods name
	 */
	private String name;
	/**
	 * alias of goods
	 */
	private String alias;
	/**
	 * when goods was brought
	 */
	private java.util.Date buyDateTime;
	/**
	 * when this record was modified
	 */
	private Date latestModDateTime;
	/**
	 * the price of goods
	 */
	private double price;
	/**
	 * the detail of the goods
	 */
	private String description;
	/**
	 * the supermarket the goods belong
	 */
	private Supermarket supermarket;
}

package com.shop.jn.entity;

import java.io.Serializable;
import java.util.List;
/**
 * entity:shop   超市
 * @author huangwei
 *
 */
public class Supermarket  implements Serializable{

	private static final long serialVersionUID = 6517742699077464699L;
	private int id;
	/**
	 * the name of the shop
	 */
	private String name;
	private String description;
	private List<Goods> goods;
	/**
	 * the sum of goods 
	 */
	private int goodsAmount;
	}

 hibernate配置文件如下

 

Goods.hbm.xml(多的一方):

 

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping>
    <class name="com.shop.jn.entity.Goods" table="t_goods" lazy="true">
        <!--<cache usage="read-write"/>
        --><id name="id" type="int">
            <column name="ID" precision="19" scale="0">
                <comment>主键id</comment>
            </column>
            <generator class="identity"/>
        </id>
         <property name="name">
            <column name="name">
                <comment>商品的名称</comment>
            </column>
        </property>
       
        <property name="alias"  >
            <column name="alias">
            
                <comment>商品的别名</comment>
            </column>
        </property>
       
        <property name="buyDateTime">
            <column name="buyDateTime" >
                <comment>购买时间</comment>
            </column>
        </property>
        <property name="latestModDateTime">
            <column name="latestModDateTime">
                <comment>最后修改时间</comment>
            </column>
        </property>
        <property name="price">
            <column name="price">
                <comment>商品价格</comment>
            </column>
        </property>
        <property name="description">
            <column name="description">
                <comment>商品的具体信息</comment>
            </column>
        </property>
        <!-- fetch=FetchType.EAGER is equal lazy=false -->
       <many-to-one name="supermarket" class="com.shop.jn.entity.Supermarket" lazy="false" cascade="all" insert="true" update="true" >
            <column name="supermarketId"  >
                <comment>商店</comment>
            </column>
            
        </many-to-one>
      
    </class>
</hibernate-mapping>

 Supermarket.hbm.xml(一的一方):

 

 

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping>
	<class name="com.shop.jn.entity.Supermarket" table="t_supermarket"
		lazy="true">
		<!--<cache usage="read-write"/> -->
		<id name="id" type="int">
			<column name="ID"><!-- precision="19" scale="0" -->
				<comment>主键id</comment>
			</column>
			<generator class="identity" />
		</id>
		<property name="name">
			<column name="name">
				<comment>商店的名称</comment>
			</column>
		</property>
		<!--<property name="goodsAmount"> <formula>(select count(*) from t_goods 
			g where g.supermarketId=id)</formula> </property> -->
		<property name="description">
			<column name="description">
				<comment>商店的详细信息</comment>
			</column>
		</property>
		<bag name="goods" lazy="false" fetch="subselect" inverse="true">
			<key column="supermarketId"></key>
			<one-to-many class="com.shop.jn.entity.Goods" />
		</bag>


	</class>
</hibernate-mapping>

 主要对bag标签进行详细的说明

 

bag标签中有如下属性

lazy(可选--默认为 true)可以用来关闭延迟加载(false)

如果指定lazy为false,则在查询supermarket(一的一方)时会把supermarket中的goods(多的一方)也查询出来,查询的的策略有三种:subselect,select,join

这里使用的策略是fetch属性指定的subselect,执行的SQL语句如下:

 

Hibernate: 
    /* criteria query */ select
        this_.ID as ID1_1_0_,
        this_.name as name2_1_0_,
        this_.description as descript3_1_0_ 
    from
        t_supermarket this_
Hibernate: 
    /* load one-to-many com.shop.jn.entity.Supermarket.goods */ select
        goods0_.supermarketId as supermar8_1_1_,
        goods0_.ID as ID1_0_1_,
        goods0_.ID as ID1_0_0_,
        goods0_.name as name2_0_0_,
        goods0_.alias as alias3_0_0_,
        goods0_.buyDateTime as buyDateT4_0_0_,
        goods0_.latestModDateTime as latestMo5_0_0_,
        goods0_.price as price6_0_0_,
        goods0_.description as descript7_0_0_,
        goods0_.supermarketId as supermar8_0_0_ 
    from
        t_goods goods0_ 
    where
        goods0_.supermarketId=?

 

如果我设置lazy为true呢?

 

调用supermarket.getGoods().size()时就会报错:

 

10:31:14,097  WARN  - Caught an exception while evaluating expression '0==goods.size' against value stack
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.shop.jn.entity.Supermarket.goods, could not initialize proxy - no Session

 因为使用的是懒加载,查询supermarket时没有把goods查询出来。

 

 

inverse(可选 — 默认为 false)标记这个集合作为双向关联关系中的方向一端。因为这里是双向关联,所以设置inverse为true

资源下载链接为: https://pan.quark.cn/s/9648a1f24758 这个HTML文件是一个专门设计的网页,适合在告白或纪念日这样的特殊时刻送给女朋友,给她带来惊喜。它通过HTML技术,将普通文字转化为富有情感和创意的表达方式,让数字媒体也能传递深情。HTML(HyperText Markup Language)是构建网页的基础语言,通过标签描述网页结构和内容,让浏览器正确展示页面。在这个特效网页中,开发者可能使用了HTML5的新特性,比如音频、视频、Canvas画布或WebGL图形,来提升视觉效果和交互体验。 原本这个文件可能是基于ASP.NET技术构建的,其扩展名是“.aspx”。ASP.NET是微软开发的一个服务器端Web应用程序框架,支持种编程语言(如C#或VB.NET)来编写动态网页。但为了在本地直接运行,不依赖服务器,开发者将其转换为纯静态的HTML格式,只需浏览器即可打开查看。 在使用这个HTML特效页时,建议使用Internet Explorer(IE)浏览器,因为一些老的或特定的网页特效可能只在IE上表现正常,尤其是那些依赖ActiveX控件或IE特有功能的页面。不过,由于IE逐渐被淘汰,现代网页可能不再对其进行优化,因此在其他现代浏览器上运行可能会出现问题。 压缩包内的文件“yangyisen0713-7561403-biaobai(html版本)_1598430618”是经过压缩的HTML文件,可能包含图片、CSS样式表和JavaScript脚本等资源。用户需要先解压,然后在浏览器中打开HTML文件,就能看到预设的告白或纪念日特效。 这个项目展示了HTML作为动态和互动内容载体的强大能力,也提醒我们,尽管技术在进步,但有时复古的方式(如使用IE浏览器)仍能唤起怀旧之情。在准备类似的个性化礼物时,掌握基本的HTML和网页制作技巧非常
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值