[Hibernate] One-To-Many 配置文件和注解的方式以及HQL语句
一对多需要在一的类配置多的类的set泛型集合.
多的一端需要添加一的类作为属性,其和数据库对应的是对应表的主键.
一个购物车有多个商品,购物车有个cart_id作为主键,商品除了自己的items_id作为主键外,还有一个cart_id作为外键.
需要在Cart类中声明一个Set<Item> items,在Items类中声明一个Cart cart属性.
在配置文件Cart.hbm.xml中配置
set>name=集合名 table=表名
key>column> name=外键
one-to-many> class=类名

在items.hbm.xml中配置

添加处理:
public static void main(String[] args) {
Cart cart = new Cart();
cart.setName("MyCart");
Items item1 = new Items("I1", 10, 1, cart);
Items item2 = new Items("I2", 20, 2, cart);
Set<Items> itemsSet = new HashSet<Items>();
itemsSet.add(item1); itemsSet.add(item2);
cart.setItems(itemsSet);
cart.setTotal(10*1 + 20*2);
SessionFactory sessionFactory = null;
Session session = null;
Transaction tx = null;
try{
//Get Session
sessionFactory = HibernateUtil.getSessionFactory();
session = sessionFactory.getCurrentSession();
System.out.println("Session created");
//start transaction
tx = session.beginTransaction();
//Save the Model objects
session.save(cart);
session.save(item1);
session.save(item2);
tx.commit();
if(!sessionFactory.isClosed()){
sessionFactory.close();
}}}
因为Cart实体类中没有声明构造函数,所以只能通过默认构造函数Cart cart=new Cart();
然后set属性来实现增加Cart对象.
其中set属性的顺序只要在save在save之前即可.记得要cart.setItems(itemsSet);one-to-many;
但是不需要items.setCart(cart);//自动指定?但为什么cart需要set?
需要同时save cart和item1,item2.
然后提交.
虽然item的主键和外键都没有指定,但其主键在配置文件中如:

所以实现了自增.
hibernate帮助将其外键对应的cart_id添加到外键column上.
===============
注解方法则是需要在id上注解@Id和@GeneratedValue(strategy=GenerationType.策略)
在Set<Items1> items1上注解@OneToMany(mappedBy="cart1")
Items1对应的有:

==========
此外配置文件中,配置文件方法的mapping是
<mapping resource="cart.hbm.xml"/>
注解方法是:
<mapping class="com.journaldev.hibernate.model.Cart1"/>
=============
附录:服务器配置:
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/hibernatedb</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
----------------------------
<!-- Database connection settings -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="connection.username">scott</property>
<property name="connection.password">orcl</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
=======================HQL=======================
[Hibernate] One-To-Many 配置文件和注解的方式以及HQL语句的更多相关文章
- Spring+AOP+Log4j 用注解的方式记录指定某个方法的日志
一.spring aop execution表达式说明 在使用spring框架配置AOP的时候,不管是通过XML配置文件还是注解的方式都需要定义pointcut"切入点" 例如定义 ...
- Hibernate入门(六)---------HQL语句
Query: 代表面向对象的一个Hibernate查询操作.在Hibernate中,通常使用session.createQuery()方法接收一个HQL语句,然后调用Query的 list()或uni ...
- Hibernate写hql语句与不写hql语句的区别?
写hql语句与不写hql语句的区别? 写hql语句:书写HQL语句,所有的查询与投影的设计均使用HQL语句完成. 不写hql语句:没有任何查询语句,所有的查询与投影的设计使用面向对象格式完成. 二者选 ...
- spring+hibernate+jpa+Druid的配置文件,spring整合Druid
spring+hibernate+jpa+Druid的配置文件 spring+hibernate+jpa+Druid的完整配置 spring+hibernate+jpa+Druid的数据源配置 spr ...
- hibernate之映射文件VS映射注解
前言 对于java开发者而言,注解应该不是一个陌生的概念,早在JavaSE阶段,例如@Override标记重写父类方法或实现接口方法,@Test标记单元测试方法,所以我们可以简单地把它理解为一种有特殊 ...
- hibernate用注解的方式实现orm
hibernate 有两种方式实现把一张表映射成一个对象,一种是配置文件的方式,一种是注解的方式.这里用hibernate提供的注解的方式实现一个对象和一张表之间的对应. 思路: 首先在hiberna ...
- Eclipse安装Hibernate插件快速生成配置文件
Eclipse安装Hibernate插件快速生成配置文件 插件链接: http://pan.baidu.com/s/1mi3KVtI 密码: kmjg 1.安装插件: 1.在eclipse顶部窗口he ...
- Spring的AOP配置文件和注解实例解析
1.1 Spring的AOP配置文件和注解实例解析 AOP它利用一种称为"横切"的技术,将那些与核心业务无关,却为业务模块所共同调用的逻辑或责任封装起来,便于减 ...
- hibernate中1对1的注解配置
hibernate中1对1的注解配置分为:外键关联映射和主键关联映射 1.外键配置 //一方@Entity@Table(name="test_classinfo")public c ...
随机推荐
- mybatis+oracle实现简单的模糊查询
第一种 concat select * from cat_table where cat_name like concat(#{catName},'%') --单个百分号 select * from ...
- soapui调用redis,获取短信验证码
1.首先,调用redis需要引入redis的jar包,放入到soapui指定目录中,例如我的目录D:\Program Files\SmartBear\SoapUI-Pro-5.1.2\bin\ext ...
- 学习gstreamer
1. 对gst 的框架认识. 第一篇文章有结构图说明,清楚易懂:第二篇文章介绍了gst的简单使用 http://www.cnblogs.com/jingzhishen/p/3709639.html h ...
- javascript callback
https://javascriptissexy.com/understand-javascript-callback-functions-and-use-them/ MDN web docs htt ...
- upload-labs
upload-labs是一个和sqli-labs类似的靶场平台,只不过是一个专门学习文件上传的.整理的很好,虽然并不能将服务器解析漏洞考虑进去,但毕竟一个靶场不可能多个web容器吧,关键是思路很重要, ...
- laravel框架基础(2)---laravel项目加载机制
当我们,通过浏览器请求laravel的时候 laravel就会根据我们的请求链接来选择对应的方法执行并返回我们所需要的实际结果. 那么这个过程是怎样的呢? 1.生命周期 2018-12-28 17:0 ...
- 让hive的表注释和字段注释支持中文
此处用的数据库类型为mysql.发现hive在初始化创建这些表的时候,大部分字段的字符集给设置成了latin1,然后collation设成了latin1_bin. 但是我们在hive中创建表时,表注释 ...
- python3.x 读写文件要使用UTF8编码的话需要。。
读写文件常遇到编码不正确的情况,都用UTF8读写文件就好了,在读写的时候加上编码格式:encoding='UTF-8'如下:with open(filename, 'r', encoding='UTF ...
- C# AsyncCallback异步回调用法示例
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- 【Spark-core学习之四】 Spark任务提交
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk1.8 scala-2.10.4(依赖jdk1.8) spark ...