JPA 系列教程3-单向多对一
生活随笔
收集整理的這篇文章主要介紹了
JPA 系列教程3-单向多对一
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
JPA中的@ManyToOne
主要屬性
- name(必需): 設(shè)定“many”方所包含的“one”方所對應(yīng)的持久化類的屬性名
- column(可選): 設(shè)定one方的主鍵,即持久化類的屬性對應(yīng)的表的外鍵
- class(可選): 設(shè)定one方對應(yīng)的持久化類的名稱,即持久化類屬性的類型
- not-null(可選): 如果為true,,表示需要建立相互關(guān)聯(lián)的兩個(gè)表之間的外鍵約束
- cascade(可選): 級(jí)聯(lián)操作選項(xiàng),默認(rèn)為none
單向多對一(@ManyToOne)關(guān)聯(lián)是最常見的單向關(guān)聯(lián)關(guān)系。假設(shè)多種商品(Product)可以有一個(gè)商品類型(ProductType),只關(guān)心商品(Product)實(shí)體找到對應(yīng)的商品類型(ProductType)實(shí)體,而無須關(guān)心從某個(gè)商品類型(ProductType)找到全部商品(Product).
單向多對一表的ddl語句
CREATE TABLE `t_product_type` (`id` bigint(20) NOT NULL AUTO_INCREMENT,`name` varchar(255) DEFAULT NULL,PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;CREATE TABLE `t_product` (`id` bigint(20) NOT NULL AUTO_INCREMENT,`name` varchar(255) DEFAULT NULL,`type_id` bigint(20) DEFAULT NULL,PRIMARY KEY (`id`),KEY `FK_oyt6r2g6hwbyee5adel4yj59e` (`type_id`),CONSTRAINT `FK_oyt6r2g6hwbyee5adel4yj59e` FOREIGN KEY (`type_id`) REFERENCES `t_product_type` (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;Product
package com.jege.jpa.many2one;import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table;/*** @author JE哥* @email 1272434821@qq.com* @description:單向:多個(gè)產(chǎn)品屬于同一個(gè)產(chǎn)品類型*/ @Entity @Table(name = "t_product") public class Product {@Id@GeneratedValueprivate Long id;private String name;// 多對一:optional=false表示外鍵type_id不能為空@ManyToOne(optional = true)@JoinColumn(name = "type_id")private ProductType type;public Product() {}public Product(String name, ProductType type) {this.name = name;this.type = type;}public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public ProductType getType() {return type;}public void setType(ProductType type) {this.type = type;}@Overridepublic String toString() {return "Product [id=" + id + ", name=" + name + "]";}}ProductType
package com.jege.jpa.many2one;import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table;/*** @author JE哥* @email 1272434821@qq.com* @description:單向*/ @Entity @Table(name = "t_product_type") public class ProductType {@Id@GeneratedValueprivate Long id;private String name;public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "ProductType [id=" + id + ", name=" + name + "]";}}Many2OneTest
package com.jege.jpa.many2one;import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence;import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; /*** @author JE哥* @email 1272434821@qq.com* @description:單向多對一Test*/ public class Many2OneTest {private static EntityManagerFactory entityManagerFactory = null;private EntityManager entityManager = null;@BeforeClasspublic static void setUpBeforeClass() throws Exception {entityManagerFactory = Persistence.createEntityManagerFactory("com.jege.jpa");}// 要求:一次性保存1個(gè)產(chǎn)品類型,保存2個(gè)產(chǎn)品// 單向多對一保存的時(shí)候必須先保存一方,否則會(huì)出現(xiàn)多余的update語句,從而影響性能@Beforepublic void persist() throws Exception {entityManager = entityManagerFactory.createEntityManager();entityManager.getTransaction().begin();ProductType type = new ProductType();type.setName("產(chǎn)品類型1");// 傳入type的本質(zhì)是處理數(shù)據(jù)庫product表的type_id外鍵Product product1 = new Product("產(chǎn)品1", null);Product product2 = new Product("產(chǎn)品2", type);System.out.println("保存之前:" + type);entityManager.persist(type);// JPA會(huì)自動(dòng)把保存后的主鍵放到當(dāng)前對象的id里面System.out.println("保存之后:" + type);entityManager.persist(product1);entityManager.persist(product2);System.out.println("++++++++++++++++++++");}// 可以通過多方Product獲取一方ProductType是否為null,來判斷是否有產(chǎn)品類型@Testpublic void find() throws Exception {Product product = entityManager.find(Product.class, 2L);System.out.println(product);ProductType type = product.getType();if (type == null) {System.out.println("當(dāng)前產(chǎn)品是沒有產(chǎn)品類型的");} else {System.out.println("當(dāng)前產(chǎn)品是有產(chǎn)品類型的");}}@Afterpublic void tearDown() throws Exception {entityManager.getTransaction().commit();if (entityManager != null && entityManager.isOpen())entityManager.close();}@AfterClasspublic static void tearDownAfterClass() throws Exception {if (entityManagerFactory != null && entityManagerFactory.isOpen())entityManagerFactory.close();}}其他關(guān)聯(lián)項(xiàng)目
- JPA 系列教程2-單表操作
http://blog.csdn.net/je_ge/article/details/53493352
源碼地址
https://github.com/je-ge/jpa
如果覺得我的文章對您有幫助,請打賞支持。您的支持將鼓勵(lì)我繼續(xù)創(chuàng)作!謝謝!
轉(zhuǎn)載于:https://www.cnblogs.com/je-ge/p/6152949.html
總結(jié)
以上是生活随笔為你收集整理的JPA 系列教程3-单向多对一的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: getSlotFromBufferLoc
- 下一篇: KD树小结