集合映射应用场景
1.用于一对多的场景
2.用于少量比较单一的数据
一、集合类型来分
1.List
@ElementCollection(targetEntity=String.Class)
@CollectionTable(name="t_name")//生成数据库的名字
@Orderby//必须加
@OrderColumn(name="orderindex") //排列字段
private List<String> names
List有序
@ElementCollection(targetEntit=String.Class)
@CollectionTable(name="t_names")
private Set<String> names;
Set无序不可重复
@ElementCollection(targehtEntity=String.Class)
@CollectionTable(name="t_names")
@MapKeyProperty(name="key")
private Map<String,String> names;
Map
二、.数据类型来分
基本数据类型
@ElementCollection(targetEntity=基本数据类型)
嵌入式数据类型 要求: 1.实现序列化接口 2.重写hashCode和equals方法 3.加@Embeddable
@ElementCollection(targetEntity=嵌入式数据类型)
联合主键(嵌入式类)
1.实现序列化接口
2.重写hashcode 和 equals方法
3.载类上加@Embeddable
一对多单向
package com.oracle.bean;
import lombok.Data;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.*;
import java.util.List;
import java.util.Set;
/**
* 产品的分类
*/
//营养套餐 水果礼盒 箱装水果 国产水果 进口水果 干货食品
@Entity
@Data
@Table(name="TCatalog")
public class Catalog {
@Id
@GeneratedValue(generator= "uuid")
@GenericGenerator(name="uuid",strategy = "uuid")
private String cataid;
private String cataname;
/**
* 在关系映射中一对多的情况和我们 平常在使用集合映射是一样
* List set Map来描述这种关系
* list @orderby
* map @mapKeyProperty
* @oneToMany 用于一方
* @joinColumn 通知多方去建立一个外键关联字段catalogid 来关联我的主键
* 在表中给你添加一个字段 存放就是顺序 1 2 3
*/
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name="catalogid")
@OrderBy("orderindex desc")
private List<Product> products;
}
package com.oracle.bean;
import lombok.Data;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.*;
import java.math.BigDecimal;
@Entity
@Data
@Table(name="TProduct")
public class Product {
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name="uuid",strategy = "uuid")
private String pid;
/**
* 商品的名称
*/
private String pname;
/**
* 商品简介
*/
private String pinfo;
/**
* 商品的编号
*/
private String productcode;
private String artno;
private String unit;
private BigDecimal price;
private BigDecimal saleprice;
private int score;
private BigDecimal discount;
/**
* 库存量
*/
private BigDecimal pcount;
private String remark;
private int orderindex;
}
package com.oracle.core;
import com.oracle.bean.Catalog;
import com.oracle.bean.Product;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class One2ManyTest {
private SessionFactory sessionFactory;
private Session session;
private Product product;
@Before
public void begin(){
Configuration cfg=new Configuration();
cfg.configure();
sessionFactory=cfg.buildSessionFactory();
session=sessionFactory.openSession();
session.beginTransaction();
}
//现在描述的关系 一对多 关系维护端 是 一的一方
//单向关联 操作一方 另外一方也会收到影响 增删查改的动作
//级联的保存 当我们保存分类的时候 级联的保存我分类下的所有的商品
//营养套餐 水果礼盒 箱装水果 国产水果 进口水果 干货食品
@Test
public void save()
{
Catalog shuiguolihe=new Catalog();
shuiguolihe.setCataname("进口水果");
Product p1=new Product();
p1.setPname("智利青苹果20个装");
p1.setPrice(new BigDecimal(108.00));
p1.setPcount(new BigDecimal(1000));
Product p2=new Product();
p2.setPname("新奇士美国脐橙20个装");
p2.setPrice(new BigDecimal(118.00));
p2.setPcount(new BigDecimal(1000));
Product p3=new Product();
p3.setPname("日本陆奥苹果1个装");
p3.setPrice(new BigDecimal(78.00));
p3.setPcount(new BigDecimal(1000));
List<Product> products=new ArrayList<>();
products.add(p1);
products.add(p2);
products.add(p3);
shuiguolihe.setProducts(products);
session.save(shuiguolihe);
}
@Test
public void save2()
{
Catalog shuiguolihe=new Catalog();
shuiguolihe.setCataname("礼品盒子");
Product p1=new Product();
p1.setPname("盒子1");
p1.setPrice(new BigDecimal(108.00));
p1.setPcount(new BigDecimal(1000));
Product p2=new Product();
p2.setPname("盒子2");
p2.setPrice(new BigDecimal(118.00));
p2.setPcount(new BigDecimal(1000));
Product p3=new Product();
p3.setPname("盒子3");
p3.setPrice(new BigDecimal(78.00));
p3.setPcount(new BigDecimal(1000));
List<Product> products=new ArrayList<>();
products.add(p1);
products.add(p2);
products.add(p3);
shuiguolihe.setProducts(products);
session.save(shuiguolihe);
}
//当我查询到商品分类的时候 可以查询商品分类下所有商品
@Test
public void query()
{
Catalog catalog=session.get(Catalog.class,"4028c9816386ccc9016386ccd24b0000");
List<Product> products=catalog.getProducts();
System.out.println(products);
}
@Test
public void delete()
{
Catalog catalog=session.get(Catalog.class,"4028c9816386c519016386c525160000");
session.delete(catalog);
}
@Test
public void update(){
Catalog jinkoushuiguo=session.get(Catalog.class,"4028c9816386d90b016386d91d860000");
Catalog shuiguolihe=session.get(Catalog.class,"4028c9816386ccc9016386ccd24b0000");
List<Product> jingkoushuiguoList = jinkoushuiguo.getProducts();
List<Product> shuiguoliheList=shuiguolihe.getProducts();
jinkoushuiguo.setProducts(shuiguoliheList);
shuiguolihe.setProducts(jingkoushuiguoList);
session.update(jinkoushuiguo);
session.update(shuiguolihe);
}
//将所有的水果礼盒分类删除掉 将水果礼盒的商品合并到进口水果中去
@Test
public void update2(){
Catalog shuiguolihe=session.get(Catalog.class,"4028c981638705ab01638705b22a0000");
Catalog jinkoushuiguo=session.get(Catalog.class,"4028c9816387079d01638707a3930000");
jinkoushuiguo.getProducts().addAll(shuiguolihe.getProducts());
shuiguolihe.getProducts().clear();
session.update(shuiguolihe);
session.update(jinkoushuiguo);
session.delete(shuiguolihe);
}
//--把某一个商品分类下的商品全部转移到另外一个分类下 并且删除该分类
//1.进口水果的id=4028c981638a56c601638a56eb7b0000
//2.礼品的id=4028c981638a58ed01638a5911550000
//大前提 目标:删除该分类 要删除存在关联关系的数据 必须先解除其关系
@Test
public void update3()
{
Catalog shuiguo = session.get(Catalog.class, "4028c981638a56c601638a56eb7b0000");
Catalog box = session.get(Catalog.class, "4028c981638a58ed01638a5911550000");
shuiguo.getProducts().addAll(box.getProducts()); //建立关系
box.getProducts().clear(); //解除关系
session.delete(box);
}
@Test
public void get()
{
Product p=new Product(); //瞬态的对象
product = session.get(Product.class, "4028c981638a58ed01638a5911b30003");
Product product2 = session.get(Product.class, "4028c981638a58ed01638a5911b30003");
//从瞬态变成持久化状态 commit的时候才会同步数据表
product.setPname(product.getPname());
//save delete 将对象从瞬态 变成 持久化状态
//做修改的动作 可以不需要 做session.update() 比较累赘
//session.save();
//session.delete();
}
@After
public void end(){
//commit只对持久化状态对象做同步
//检查你当前的内存中的对象和数据表的数据是否是一致的 如果不一致 则自动做操作
session.getTransaction().commit();
//对象依然存在在内存中 对象属于游离状态 操作 数据的同步的操作
product.setPname("小刘的礼盒");
session.close();
sessionFactory.close();
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.oracle</groupId>
<artifactId>One2Many</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>One2Many Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.1.0.Final</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
</dependency>
</dependencies>
<build>
<finalName>One2Many</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@10.10.44.161:1521:ORCL</property>
<property name="connection.username">javazhao</property>
<property name="connection.password">123456</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- DB schema will be updated if needed -->
<property name="hbm2ddl.auto">update</property>
<mapping class="com.oracle.bean.Product"></mapping>
<mapping class="com.oracle.bean.Catalog"></mapping>
</session-factory>
</hibernate-configuration>