SSH注解 关于hibernate一对一双向外键级联关系 查询两表的内容 例子

  由于要查询两张表的内容,所以去了解了下hibernate 的一对一级联 。现在写了个简单的SSH 注解的 一对一双向级联。

关于一对一双向级联详解我推荐这个博客。我感觉写的很好。

    hibernate(五) hibernate一对一关系映射详解

关于一些注解关键字了解 可以看看这个博客

    hibernate 常用注解

 

下面我将给出我的相关代码:

CarDetail类

/** @author ljt* create on 2018/11/28* @汽车租赁
*/@Entity
@Table(name="zl_cars_detail")
public class CarDetail {/** carId		序号* carName		车辆名称* carCost		租车费用* carPedestal	座位数* carColor		颜色* carBrand		品牌* carSeries	车系* carAstyle	年代款* carCmodel	配置款* carDoors		车门数* carFtype		燃料类型* carTtype		变数箱类型* carCC		排量* carFlabel	燃油标号* carDmode		驱动方式* carEip		发动机进气形式* carLouver	天窗* carFtank		油箱容量* carVbox		音箱* carSeat		座椅* carBradar	倒车雷达* carGasbag	气囊数* carDVD		DVD OR CD* carGps		是否有GPS导航* CarSimple   为了在详细类中能查询到对应的相关信息*/	@Id@GeneratedValue(strategy = GenerationType.IDENTITY)@Column(name="Car_id")private int carId;//一对一外键关联,从表,mappedBy字段必须是主表的字段@OneToOne(cascade=CascadeType.ALL ,mappedBy="carDetail")private CarSimple carSimple;@Column(name="Car_name")private String carName;//省略其他字段//** getter,setter方法}

CarSimple 类

** @Autor ljt* create on 2018/11/28* @汽车租赁*/@Entity
@Table(name="zl_cars")public class CarSimple {/** ID           自增序号* carID  		外键序号* carNum 		车牌号* carLeaseIs	是否在租 1 已经租赁 0没有租赁(default)* userID		租赁人ID* CarDetail    关联carSimple类*/@Id@GeneratedValue(strategy = GenerationType.IDENTITY)@Column(name="ID")private int ID;//一对一外键关联,主表,外键字段是Car_id,对应的表是CarDetail。@OneToOne@JoinColumn(name="Car_id",insertable=true,unique=true)private CarDetail carDetail;@Column(name="Car_num")private String carNum;@Column(name="Car_lease_is")private int carLeaseIs=0;@Column(name="UserID")private int userID;//getter setter
}

DAO层接口

/** 获取carSimple carDetail 表的内容 (测试级联)* @param void * @return 查询的记录集合*/public List<CarSimple > queryTest();

DaoImpl层查询HQL

/** 获取carSimple carDetail 表的内容 (测试级联)* @param void * @return 查询的记录集合*/public List<CarSimple> queryTest() {@SuppressWarnings("unchecked")List<CarSimple> carSimpleList = sessionFactory.getCurrentSession().createQuery("from CarSimple").list();return carSimpleList;}

Service接口

/** 测试级联查询两表的内容* @return 返回CarSimple 级联所查询的内容*/public List<CarSimple> queryTest();

serviceImpl实现

/** 测试级联查询两表的内容* @return 返回CarSimple 级联所查询的内容*/@Transactional(propagation = Propagation.REQUIRES_NEW, isolation=Isolation.READ_COMMITTED)public List<CarSimple> queryTest() {List<CarSimple> carSimpleList = carSimpleDao.queryTest();System.out.println("开始测试****");for(CarSimple carSimple:carSimpleList) {System.out.println(carSimple.getCarNum());System.out.println(carSimple.getCarDetail().getCarName());}return carSimpleList;}

Action

** @Author ljt* create on 2018/11/29* @汽车租赁*/public class CarSimpleAction extends ActionSupport{/*** 序列化*/private static final long serialVersionUID = 1L;@Autowiredprivate CarSimpleService carSimpleService ;private List<CarSimple> carSimpleTest;//注解映射获取action,并返回相应的jsp页面(测试级联)@Action(value = "CarSimpleQueryDefaultPage2", results = { @Result(name ="CarSimpleQueryDefaultPageSuccess2", location = "/manage_test2.jsp")})public String CarSimpleQueryDefaultPage2() {carSimpleTest = carSimpleService.queryTest();return "CarSimpleQueryDefaultPageSuccess2";}public List<CarSimple> getCarSimpleTest() {return carSimpleTest;}public void setCarSimpleTest(List<CarSimple> carSimpleTest) {this.carSimpleTest = carSimpleTest;}}

前端页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试-后台管理员</title><!-- 新 Bootstrap 核心 CSS 文件 --><link href="${pageContext.request.contextPath}/bootstrap-3.3.7/dist/css/bootstrap.min.css" rel="stylesheet"><!-- jQuery文件。务必在bootstrap.min.js 之前引入 --><script src="${pageContext.request.contextPath}/bootstrap-3.3.7/jquery-2.1.1/jquery.min.js"></script><!-- 最新的 Bootstrap 核心 JavaScript 文件 --><script src="${pageContext.request.contextPath}/bootstrap-3.3.7/dist/js/bootstrap.min.js"></script></head>
<body>
<s:iterator value="carSimpleTest"><table><tr>carSimple:<s:property value="ID"/>用户ID: <s:property value="carDetail.carId"/>用户名:<s:property value="carDetail.carName"/>排量:<s:property value="carDetail.carCC"/>编号:<s:property value="carNum"/></tr></table>
</s:iterator>
</body>
</html>

大概内容是这样的: