hibernate 映射表

Today we will look into Hibernate Many to Many Mapping using XML and annotation configurations. Earlier we looked how to implement One To One and One To Many mapping in Hibernate.

今天,我们将研究使用XML和注释配置的Hibernate多对多映射 。 之前,我们研究了如何在Hibernate中实现一对一和一对多映射 。

Hibernate多对多 (Hibernate Many to Many)

Many-to-Many mapping is usually implemented in database using a Join Table. For example we can have Cart and Item table and Cart_Items table for many-to-many mapping. Every cart can have multiple items and every item can be part of multiple carts, so we have a many to many mapping here.

通常使用Join Table在数据库中实现多对多映射。 例如,我们可以使用CartItem表以及Cart_Items表进行多对多映射。 每个购物车可以有多个商品,每个商品可以是多个购物车的一部分,因此我们在这里有多对多映射。

Hibernate多对多映射数据库设置 (Hibernate Many to Many Mapping Database Setup)

Below script can be used to create our many-to-many example database tables, these scripts are for MySQL database. If you are using any other database, you might need to make small changes to get it working.

下面的脚本可用于创建我们的多对多示例数据库表,这些脚本用于MySQL数据库。 如果您正在使用其他任何数据库,则可能需要进行一些小的更改才能使其正常运行。

DROP TABLE IF EXISTS `Cart_Items`;
DROP TABLE IF EXISTS `Cart`;
DROP TABLE IF EXISTS `Item`;CREATE TABLE `Cart` (`cart_id` int(11) unsigned NOT NULL AUTO_INCREMENT,`cart_total` decimal(10,0) NOT NULL,PRIMARY KEY (`cart_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;CREATE TABLE `Item` (`item_id` int(11) unsigned NOT NULL AUTO_INCREMENT,`item_desc` varchar(20) NOT NULL,`item_price` decimal(10,0) NOT NULL,PRIMARY KEY (`item_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;CREATE TABLE `Cart_Items` (`cart_id` int(11) unsigned NOT NULL,`item_id` int(11) unsigned NOT NULL,PRIMARY KEY (`cart_id`,`item_id`),CONSTRAINT `fk_cart` FOREIGN KEY (`cart_id`) REFERENCES `Cart` (`cart_id`),CONSTRAINT `fk_item` FOREIGN KEY (`item_id`) REFERENCES `Item` (`item_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Notice that Cart_Items table doesn’t have any extra columns, actually it doesn’t make much sense to have extra columns in many-to-many mapping table. But if you have extra columns, the implementation changes little bit and we will look into that in another post.

注意,Cart_Items表没有任何额外的列,实际上,在多对多映射表中有额外的列并没有多大意义。 但是,如果您有多余的列,则实现会稍有变化,我们将在另一篇文章中进行探讨。

Below diagram shows the Entity relationship between these tables.

下图显示了这些表之间的实体关系。

Our Database setup is ready now, let’s move on to create the hibernate many-to-many mapping project.

现在我们的数据库设置已经准备就绪,让我们继续创建Hibernate的多对多映射项目。

Hibernate多对多映射项目结构 (Hibernate Many To Many Mapping Project Structure)

Create a maven project in Eclipse or your favorite IDE, below image shows the structure and different components in the application.

在Eclipse或您喜欢的IDE中创建一个maven项目,下图显示了应用程序的结构和不同组件。

We will first look into the XML based mapping implementations and then move over to using JPA annotations.

我们将首先研究基于XML的映射实现,然后继续使用JPA批注。

HibernateMaven依赖关系 (Hibernate Maven Dependencies)

Our final pom.xml contains Hibernate dependencies with latest version 4.3.5.Final and mysql driver dependencies.

我们的最终pom.xml包含最新版本为4.3.5的 Hibernate依赖项.Final和mysql驱动程序依赖项。

pom.xml

pom.xml

<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.journaldev.hibernate</groupId><artifactId>HibernateManyToManyMapping</artifactId><version>0.0.1-SNAPSHOT</version><dependencies><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-core</artifactId><version>4.3.5.Final</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.0.5</version></dependency></dependencies></project>

Hibernate多对多XML配置模型类 (Hibernate Many to Many XML Configuration Model Classes)

Cart.java

Cart.java

package com.journaldev.hibernate.model;import java.util.Set;public class Cart {private long id;private double total;private Set<Item> items;public double getTotal() {return total;}public void setTotal(double total) {this.total = total;}public long getId() {return id;}public void setId(long id) {this.id = id;}public Set<Item> getItems() {return items;}public void setItems(Set<Item> items) {this.items = items;}}

Item.java

Item.java

package com.journaldev.hibernate.model;import java.util.Set;public class Item {private long id;private double price;private String description;private Set<Cart> carts;public long getId() {return id;}public void setId(long id) {this.id = id;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}public String getDescription() {return description;}public void setDescription(String description) {this.description = description;}public Set<Cart> getCarts() {return carts;}public void setCarts(Set<Cart> carts) {this.carts = carts;}
}

Notice that Cart has set of Item and Item has set of Cart, this way we are implementing Bi-Directional associations. It means that we can configure it to save Item when we save Cart and vice versa.

请注意,购物车具有商品集,商品具有购物车集,这样我们就实现了双向关联。 这意味着我们可以在保存购物车时将其配置为保存商品,反之亦然。

For one-directional mapping, usually we have set in one of the model class. We will use annotations for one-directional mapping.

对于单向映射,通常我们设置了一个模型类。 我们将使用注释进行单向映射。

Hibernate多对多映射XML配置 (Hibernate Many To Many Mapping XML Configuration)

Let’s create hibernate many to many mapping xml configuration files for Cart and Item. We will implement bi-directional many-to-many mapping.

让我们为Cart和Item创建Hibernate的多对多映射xml配置文件。 我们将实现双向多对多映射。

cart.hbm.xml

cart.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""https://hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.journaldev.hibernate.model"><class name="Cart" table="CART"><id name="id" type="long"><column name="cart_id" /><generator class="identity" /></id><property name="total" type="double" column="cart_total" /><set name="items" table="CART_ITEMS" fetch="select" cascade="all"><key column="cart_id" /><many-to-many class="Item" column="item_id" /></set></class></hibernate-mapping>

Notice that set of items is mapped to CART_ITEMS table. Since Cart is the primary object, cart_id is the key and many-to-many mapping is using Item class item_id column.

请注意,项目集已映射到CART_ITEMS表。 由于Cart是主要对象,因此cart_id是键,并且many-to-many映射使用Item类item_id列。

item.hbm.xml

item.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"https://hibernate.org/dtd/hibernate-mapping-3.0.dtd" ><hibernate-mapping package="com.journaldev.hibernate.model"><class name="Item" table="ITEM"><id name="id" type="long"><column name="item_id" /><generator class="identity" /></id><property name="description" type="string" column="item_desc" /><property name="price" type="double" column="item_price" /><set name="carts" table="CART_ITEMS" fetch="select" cascade="all"><key column="item_id" /><many-to-many class="Cart" column="cart_id" /></set></class></hibernate-mapping>

As you can see from above, the mapping is very similar to Cart mapping configurations.

从上面可以看到,该映射与购物车映射配置非常相似。

基于XML的多对多映射的Hibernate配置 (Hibernate Configuration for XML Based Many to Many Mapping)

Our hibernate configuration file looks like below.

我们的Hibernate配置文件如下所示。

hibernate.cfg.xml

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""https://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration><session-factory><property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><property name="hibernate.connection.password">pankaj123</property><property name="hibernate.connection.url">jdbc:mysql://localhost/TestDB</property><property name="hibernate.connection.username">pankaj</property><property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property><property name="hibernate.current_session_context_class">thread</property><property name="hibernate.show_sql">true</property><mapping resource="cart.hbm.xml" /><mapping resource="item.hbm.xml" /></session-factory>
</hibernate-configuration>

Hibernate SessionFactory实用程序类,用于基于XML的映射 (Hibernate SessionFactory Utility Class for XML Based Mapping)

HibernateUtil.java

HibernateUtil.java

package com.journaldev.hibernate.util;import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;public class HibernateUtil {private static SessionFactory sessionFactory;private static SessionFactory buildSessionFactory() {try {// Create the SessionFactory from hibernate.cfg.xmlConfiguration configuration = new Configuration();configuration.configure("hibernate.cfg.xml");System.out.println("Hibernate Configuration loaded");ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();System.out.println("Hibernate serviceRegistry created");SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);return sessionFactory;} catch (Throwable ex) {System.err.println("Initial SessionFactory creation failed." + ex);ex.printStackTrace();throw new ExceptionInInitializerError(ex);}}public static SessionFactory getSessionFactory() {if (sessionFactory == null)sessionFactory = buildSessionFactory();return sessionFactory;}}

It’s a simple utility class that works as a factory for SessionFactory.

这是一个简单的实用程序类,可以用作SessionFactory的工厂。

Hibernate多对多映射XML配置测试程序 (Hibernate Many To Many Mapping XML Configuration Test Program)

Our hibernate many to many mapping setup is ready, let’s test it out. We will write two program, one is to save Cart and see that Item and Cart_Items information is also getting saved. Another one to save item data and check that corresponding Cart and Cart_Items are saved.

我们的Hibernate多对多映射设置已准备就绪,让我们对其进行测试。 我们将编写两个程序,一个程序是保存Cart,并看到Item和Cart_Items信息也得到保存。 另一个用于保存物料数据并检查是否保存了相应的Cart和Cart_Items。

HibernateManyToManyMain.java

HibernateManyToManyMain.java

package com.journaldev.hibernate.main;import java.util.HashSet;
import java.util.Set;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;import com.journaldev.hibernate.model.Cart;
import com.journaldev.hibernate.model.Item;
import com.journaldev.hibernate.util.HibernateUtil;public class HibernateManyToManyMain {//Saving many-to-many where Cart is primarypublic static void main(String[] args) {Item iphone = new Item();iphone.setPrice(100); iphone.setDescription("iPhone");Item ipod = new Item();ipod.setPrice(50); ipod.setDescription("iPod");Set<Item> items = new HashSet<Item>();items.add(iphone); items.add(ipod);Cart cart = new Cart();cart.setItems(items);cart.setTotal(150);Cart cart1 = new Cart();Set<Item> items1 = new HashSet<Item>();items1.add(iphone);cart1.setItems(items1);cart1.setTotal(100);SessionFactory sessionFactory = null;try{sessionFactory = HibernateUtil.getSessionFactory();Session session = sessionFactory.getCurrentSession();Transaction tx = session.beginTransaction();session.save(cart);session.save(cart1);System.out.println("Before committing transaction");tx.commit();sessionFactory.close();System.out.println("Cart ID="+cart.getId());System.out.println("Cart1 ID="+cart1.getId());System.out.println("Item1 ID="+iphone.getId());System.out.println("Item2 ID="+ipod.getId());}catch(Exception e){e.printStackTrace();}finally{if(sessionFactory != null && !sessionFactory.isClosed()) sessionFactory.close();}}}

When we execute above hibernate many to many mapping example program, we get following output.

当我们在Hibernate状态下执行多对多映射示例程序时,将得到以下输出。

Hibernate Configuration loaded
Hibernate serviceRegistry created
Hibernate: insert into CART (cart_total) values (?)
Hibernate: insert into ITEM (item_desc, item_price) values (?, ?)
Hibernate: insert into ITEM (item_desc, item_price) values (?, ?)
Hibernate: insert into CART (cart_total) values (?)
Before committing transaction
Hibernate: insert into CART_ITEMS (cart_id, item_id) values (?, ?)
Hibernate: insert into CART_ITEMS (cart_id, item_id) values (?, ?)
Hibernate: insert into CART_ITEMS (cart_id, item_id) values (?, ?)
Cart ID=1
Cart1 ID=2
Item1 ID=1
Item2 ID=2

Note that once the item data is saved through first cart, the item_id is generated and while saving second cart, it’s not saved again.

请注意,一旦通过第一个购物车保存了商品数据,就会生成item_id,并且在保存第二个购物车时不会再次保存它。

Another important point to note is that many-to-many join table data is getting saved when we are committing the transaction. It’s done for better performance incase we choose to rollback the transaction.

另一个需要注意的重要点是,在提交事务时,将保存多对多联接表数据。 如果我们选择回滚事务,这样做可以提高性能。

HibernateBiDirectionalManyToManyMain.java

HibernateBiDirectionalManyToManyMain.java

package com.journaldev.hibernate.main;import java.util.HashSet;
import java.util.Set;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;import com.journaldev.hibernate.model.Cart;
import com.journaldev.hibernate.model.Item;
import com.journaldev.hibernate.util.HibernateUtil;public class HibernateBiDirectionalManyToManyMain {//Saving many-to-many where Item is primarypublic static void main(String[] args) {Item iphone = new Item();iphone.setPrice(100); iphone.setDescription("iPhone");Item ipod = new Item();ipod.setPrice(50); ipod.setDescription("iPod");Cart cart = new Cart();cart.setTotal(150);Cart cart1 = new Cart();cart1.setTotal(100);Set<Cart> cartSet = new HashSet<Cart>();cartSet.add(cart);cartSet.add(cart1);Set<Cart> cartSet1 = new HashSet<Cart>();cartSet1.add(cart);iphone.setCarts(cartSet1);ipod.setCarts(cartSet);SessionFactory sessionFactory = null;try{sessionFactory = HibernateUtil.getSessionFactory();Session session = sessionFactory.getCurrentSession();Transaction tx = session.beginTransaction();session.save(iphone);session.save(ipod);tx.commit();sessionFactory.close();System.out.println("Cart ID="+cart.getId());System.out.println("Cart1 ID="+cart1.getId());System.out.println("Item1 ID="+iphone.getId());System.out.println("Item2 ID="+ipod.getId());}catch(Exception e){e.printStackTrace();}finally{if(sessionFactory != null && !sessionFactory.isClosed()) sessionFactory.close();}}}

Output of above program is:

上面程序的输出是:

Hibernate Configuration loaded
Hibernate serviceRegistry created
Hibernate: insert into ITEM (item_desc, item_price) values (?, ?)
Hibernate: insert into CART (cart_total) values (?)
Hibernate: insert into ITEM (item_desc, item_price) values (?, ?)
Hibernate: insert into CART (cart_total) values (?)
Hibernate: insert into CART_ITEMS (item_id, cart_id) values (?, ?)
Hibernate: insert into CART_ITEMS (item_id, cart_id) values (?, ?)
Hibernate: insert into CART_ITEMS (item_id, cart_id) values (?, ?)
Cart ID=3
Cart1 ID=4
Item1 ID=3
Item2 ID=4

You can easily relate it to the earlier test program, since we have configured bi-directional mapping we can save Item or Cart and mapped data will get saved automatically.

您可以轻松地将其与早期的测试程序相关联,因为我们已经配置了双向映射,所以我们可以保存Item或Cart,并且映射的数据将自动保存。

Hibernate多对多映射注释 (Hibernate Many To Many Mapping Annotation)

Now that we have seen how to configure many-to-many mapping using hibernate xml configurations, let’s see an example of implementing it through annotations. We will implement one-directional many-to-many mapping using JPA annotations.

既然我们已经了解了如何使用hibernate xml配置来配置多对多映射,那么让我们来看一个通过注释实现它的示例。 我们将使用JPA注释实现单向多对多映射。

Hibernate配置XML文件 (Hibernate Configuration XML File)

Our annotations based hibernate configuration file looks like below.

我们基于注释的Hibernate配置文件如下所示。

hibernate-annotation.cfg.xml

hibernate-annotation.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""https://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration><session-factory><property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><property name="hibernate.connection.password">pankaj123</property><property name="hibernate.connection.url">jdbc:mysql://localhost/TestDB</property><property name="hibernate.connection.username">pankaj</property><property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property><property name="hibernate.current_session_context_class">thread</property><property name="hibernate.show_sql">true</property><mapping class="com.journaldev.hibernate.model.Cart1" /><mapping class="com.journaldev.hibernate.model.Item1" /></session-factory>
</hibernate-configuration>

Hibernate SessionFactory实用程序类 (Hibernate SessionFactory utility class)

Our utility class to create SessionFactory looks like below.

我们用于创建SessionFactory的实用程序类如下所示。

HibernateAnnotationUtil.java

HibernateAnnotationUtil.java

package com.journaldev.hibernate.util;import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;public class HibernateAnnotationUtil {private static SessionFactory sessionFactory;private static SessionFactory buildSessionFactory() {try {// Create the SessionFactory from hibernate-annotation.cfg.xmlConfiguration configuration = new Configuration();configuration.configure("hibernate-annotation.cfg.xml");System.out.println("Hibernate Annotation Configuration loaded");ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();System.out.println("Hibernate Annotation serviceRegistry created");SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);return sessionFactory;}catch (Throwable ex) {System.err.println("Initial SessionFactory creation failed." + ex);ex.printStackTrace();throw new ExceptionInInitializerError(ex);}}public static SessionFactory getSessionFactory() {if(sessionFactory == null) sessionFactory = buildSessionFactory();return sessionFactory;}
}

Hibernate多对多映射注释模型类 (Hibernate Many to Many Mapping Annotation Model Classes)

This is the most important part for annotation based mapping, let’s first look at the Item table model class and then we will look into Cart table model class.

这是基于注释的映射最重要的部分,首先让我们看一下Item表模型类,然后再看一下Cart表模型类。

Item1.java

Item1.java

package com.journaldev.hibernate.model;import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;@Entity
@Table(name="ITEM")
public class Item1 {@Id@Column(name="item_id")@GeneratedValue(strategy=GenerationType.IDENTITY)private long id;@Column(name="item_price")private double price;@Column(name="item_desc")private String description;// Getter Setter methods
}

Item1 class looks simple, there is no relational mapping here.

Item1类看起来很简单,这里没有关系映射。

Cart1.java

Cart1.java

package com.journaldev.hibernate.model;import java.util.Set;import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;@Entity
@Table(name = "CART")
public class Cart1 {@Id@Column(name = "cart_id")@GeneratedValue(strategy = GenerationType.IDENTITY)private long id;@Column(name = "cart_total")private double total;@ManyToMany(targetEntity = Item1.class, cascade = { CascadeType.ALL })@JoinTable(name = "CART_ITEMS", joinColumns = { @JoinColumn(name = "cart_id") }, inverseJoinColumns = { @JoinColumn(name = "item_id") })private Set<Item1> items;//Getter Setter methods
}

Most important part here is the use of ManyToMany annotation and JoinTable annotation where we provide table name and columns to be used for many-to-many mapping.

这里最重要的部分是对ManyToMany注释和JoinTable注释的使用,其中我们提供了用于多对多映射的表名和列。

Hibernate多对多注释映射测试程序 (Hibernate Many To Many Annotation Mapping Test Program)

Here is a simple test program for our hibernate many to many mapping annotation based configuration.

这是一个简单的测试程序,用于我们的Hibernate多对多基于映射注释的配置。

HibernateManyToManyAnnotationMain.java

HibernateManyToManyAnnotationMain.java

package com.journaldev.hibernate.main;import java.util.HashSet;
import java.util.Set;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;import com.journaldev.hibernate.model.Cart1;
import com.journaldev.hibernate.model.Item1;
import com.journaldev.hibernate.util.HibernateAnnotationUtil;public class HibernateManyToManyAnnotationMain {public static void main(String[] args) {Item1 item1 = new Item1();item1.setDescription("samsung"); item1.setPrice(300);Item1 item2 = new Item1();item2.setDescription("nokia"); item2.setPrice(200);Cart1 cart = new Cart1();cart.setTotal(500);Set<Item1> items = new HashSet<Item1>();items.add(item1); items.add(item2);cart.setItems(items);SessionFactory sessionFactory = null;try{sessionFactory = HibernateAnnotationUtil.getSessionFactory();Session session = sessionFactory.getCurrentSession();Transaction tx = session.beginTransaction();session.save(cart);System.out.println("Before committing transaction");tx.commit();sessionFactory.close();System.out.println("Cart ID="+cart.getId());System.out.println("Item1 ID="+item1.getId());System.out.println("Item2 ID="+item2.getId());}catch(Exception e){e.printStackTrace();}finally{if(sessionFactory != null && !sessionFactory.isClosed()) sessionFactory.close();}}}

When we execute above program, it produces following output.

当我们执行以上程序时,它会产生以下输出。

Hibernate Annotation Configuration loaded
Hibernate Annotation serviceRegistry created
Hibernate: insert into CART (cart_total) values (?)
Hibernate: insert into ITEM (item_desc, item_price) values (?, ?)
Hibernate: insert into ITEM (item_desc, item_price) values (?, ?)
Before committing transaction
Hibernate: insert into CART_ITEMS (cart_id, item_id) values (?, ?)
Hibernate: insert into CART_ITEMS (cart_id, item_id) values (?, ?)
Cart ID=5
Item1 ID=6
Item2 ID=5

It’s clear that saving cart is also saving data into Item and Cart_Items table. If you will save only item information, you will notice that Cart and Cart_Items data is not getting saved.

显然,保存购物车还将数据保存到Item和Cart_Items表中。 如果仅保存项目信息,则会注意到Cart和Cart_Items数据没有得到保存。

That’s all for Hibernate Many-To-Many mapping example tutorial, you can download the sample project from below link and play around with it to learn more.

这就是Hibernate多对多映射示例教程的全部内容,您可以从下面的链接下载示例项目,并进行试用以了解更多信息。

Download Hibernate ManyToMany Mapping Project下载Hibernate ManyToMany映射项目

翻译自: https://www.journaldev.com/2934/hibernate-many-to-many-mapping-join-tables

hibernate 映射表

hibernate 映射表_Hibernate多对多映射-连接表相关推荐

  1. Hibernate多对多映射 - 连接表

    Hibernate多对多映射 - 连接表 今天我们将使用XML和注释配置来研究Hibernate多对多映射.之前我们看过如何在Hibernate中实现One To One和One To Many映射. ...

  2. Hibernate(五)多对多映射关系

    Many to Many 映射关系(没尝试映射hibernate支持的java类型) 双向多对多外键关联(XML/Annotation) (xml和annotation都实现了) 单向多对多外键关联( ...

  3. java hibernate 多对多_hibernate 多对多映射配置详解

    表关系 如图: Teacher.java文件: privateint id; private String name; private Set teachers; Student.java文件: pr ...

  4. mysql数据库如何创建冗余小的表_mysql – Hibernate创建冗余的多对多表

    在开发我的Spring Boot应用程序时,我不得不放弃我的数据库并让Hibernate使用hibernate.hbm2ddl.auto = update再次生成它.之后我想确保它完成了我想做的所有事 ...

  5. hibernate教程_Hibernate多对多教程

    hibernate教程 介绍: 在本教程中,我们将学习使用Hibernate @ManyToMany注释定义和使用多对多实体关联. 上下文构建: 为了继续学习本教程,我们假设我们有两个实体– 雇员和资 ...

  6. hibernate多对多映射拆成2个一对多映射(注解)

    hibernate的many to many确实很是方便我们处理实体和集合间的关系,并可以通过级联的方法处理集合,但有的时候many to many不能满足我们的需要,比如 用户<---> ...

  7. 【Hibernate步步为营】--多对多映射详解

    上篇文章详细讨论了一对多映射,在一对多映射中单向的关联映射会有很多问题,所以不建议使用如果非要采用一对多的映射的话可以考虑使用双向关联来优化之间的关系,一对多的映射其实质上是在一的一端使用<ma ...

  8. java集合——映射表+专用集合映射表类

    [0]README 0.1) 本文描述转自 core java volume 1, 源代码为原创,旨在理解 java集合--映射表+专用集合映射表类 的相关知识: 0.2) for full sour ...

  9. Hibernate第四篇【集合映射、一对多和多对一】

    前言 前面的我们使用的是一个表的操作,但我们实际的开发中不可能只使用一个表的-因此,本博文主要讲解关联映射 集合映射 需求分析:当用户购买商品,用户可能有多个地址. 数据库表 我们一般如下图一样设计数 ...

最新文章

  1. 《快速搞垮一个技术团队的20个“必杀技”》
  2. 互联网技术的主要组成
  3. 无线多操作系统启动之uInitrd阶段NFS挂载篇
  4. python入门之函数调用第一关_零基础学习 Python 之与函数的初次相见
  5. 中科大 计算机网络5 接入网和物理媒体
  6. 374. Guess Number Higher or Lower
  7. Nginx基本数据结构之ngx_str_t
  8. java判断字符串是否包含某个字符串_Bash技巧:使用[[命令的 =~ 操作符判断字符串的包含关系...
  9. 怎样完善和推广自己的理论模型?
  10. 24.23%!汉能高效硅异质结薄膜电池效率再次刷新中国纪录
  11. Centos 7 配置 apache 网站
  12. 计算机网络连接图标 红叉,win7系统网络连接成功但图标显示红叉的解决方法
  13. 【UVM源码】uvm_event
  14. 斗地主功能测试实战二之用例设计
  15. 腾讯越来越不懂游戏了
  16. 如何将 Excel 数据分组后按次序横向排列
  17. js正则验证手机号格式
  18. JavaScript: JSON基本概念带题解
  19. 为什么影子会相互吸引? - 《像乌鸦一样思考》
  20. 【tkinter组件专栏】LabelFrame:规划布局frame的小老弟

热门文章

  1. chromium 一些设置 --插件安装
  2. 回调函数 EnumFontFamProc
  3. Arrays.asList 方法注意事项
  4. Hacker News的全文输出RSS地址
  5. 基于Cookie的单点登录(SSO)系统介绍
  6. Jrebel激活方法
  7. 西安力邦智能医疗amp;可穿戴设备沙龙--第1期---苹果HealthKit、谷歌GoogleFit来袭,智能医疗要爆发吗?...
  8. GitHub 使用教程图文详解(转)
  9. java的前台与后台
  10. 二叉树的基本操作(C)