java 集合對映
阿新 • • 發佈:2018-12-19
集合對映應用場景
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
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>