BAEL-2822 Persisting Maps with Hibernate (#6692)

* BAEL-2822 Persisting Maps with Hibernate

* BAEL-2822 Move PersistMaps Tutorial to new module

* BAEL-2822 Move PersistMaps Tutorial to new module

*     BAEL-2822 Move PersistMaps Tutorial to new module
This commit is contained in:
Raghav Jha 2019-04-20 10:04:34 +05:30 committed by Josh Cummings
parent 1a01445b6f
commit 9da56d97b6
21 changed files with 1133 additions and 0 deletions

View File

@ -0,0 +1,52 @@
<?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">
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../</relativePath>
</parent>
<artifactId>hibernate-mapping</artifactId>
<version>1.0.0-SNAPSHOT</version>
<modelVersion>4.0.0</modelVersion>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj-core.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2database.version}</version>
</dependency>
</dependencies>
<build>
<finalName>hibernate-mapping</finalName>
<resources>
<resource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<properties>
<hibernate.version>5.3.7.Final</hibernate.version>
<h2database.version>1.4.196</h2database.version>
<assertj-core.version>3.8.0</assertj-core.version>
</properties>
</project>

View File

@ -0,0 +1,64 @@
package com.baeldung.hibernate;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.service.ServiceRegistry;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;
public class HibernateUtil {
private static SessionFactory sessionFactory;
private HibernateUtil() {
}
public static SessionFactory getSessionFactory(Strategy strategy) {
if (sessionFactory == null) {
sessionFactory = buildSessionFactory(strategy);
}
return sessionFactory;
}
private static SessionFactory buildSessionFactory(Strategy strategy) {
try {
ServiceRegistry serviceRegistry = configureServiceRegistry();
MetadataSources metadataSources = new MetadataSources(serviceRegistry);
for (Class<?> entityClass : strategy.getEntityClasses()) {
metadataSources.addAnnotatedClass(entityClass);
}
Metadata metadata = metadataSources.getMetadataBuilder()
.build();
return metadata.getSessionFactoryBuilder()
.build();
} catch (IOException ex) {
throw new ExceptionInInitializerError(ex);
}
}
private static ServiceRegistry configureServiceRegistry() throws IOException {
Properties properties = getProperties();
return new StandardServiceRegistryBuilder().applySettings(properties)
.build();
}
private static Properties getProperties() throws IOException {
Properties properties = new Properties();
URL propertiesURL = Thread.currentThread()
.getContextClassLoader()
.getResource("hibernate.properties");
try (FileInputStream inputStream = new FileInputStream(propertiesURL.getFile())) {
properties.load(inputStream);
}
return properties;
}
}

View File

@ -0,0 +1,31 @@
package com.baeldung.hibernate;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public enum Strategy {
//See that the classes belongs to different packages
MAP_KEY_COLUMN_BASED(Collections.singletonList(com.baeldung.hibernate.persistmaps.mapkeycolumn.Order.class)),
MAP_KEY_BASED(Arrays.asList(com.baeldung.hibernate.persistmaps.mapkey.Item.class,
com.baeldung.hibernate.persistmaps.mapkey.Order.class)),
MAP_KEY_JOIN_COLUMN_BASED(Arrays.asList(com.baeldung.hibernate.persistmaps.mapkeyjoincolumn.Seller.class,
com.baeldung.hibernate.persistmaps.mapkeyjoincolumn.Item.class,
com.baeldung.hibernate.persistmaps.mapkeyjoincolumn.Order.class)),
MAP_KEY_ENUMERATED_BASED(Arrays.asList(com.baeldung.hibernate.persistmaps.mapkeyenumerated.Order.class,
com.baeldung.hibernate.persistmaps.mapkey.Item.class)),
MAP_KEY_TEMPORAL_BASED(Arrays.asList(com.baeldung.hibernate.persistmaps.mapkeytemporal.Order.class,
com.baeldung.hibernate.persistmaps.mapkey.Item.class));
private List<Class<?>> entityClasses;
Strategy(List<Class<?>> entityClasses) {
this.entityClasses = entityClasses;
}
public List<Class<?>> getEntityClasses() {
return entityClasses;
}
}

View File

@ -0,0 +1,6 @@
package com.baeldung.hibernate.persistmaps;
public enum ItemType {
JEANS,
TSHIRTS
}

View File

@ -0,0 +1,95 @@
package com.baeldung.hibernate.persistmaps.mapkey;
import com.baeldung.hibernate.persistmaps.ItemType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import java.util.Date;
import java.util.Objects;
@Entity
@Table(name = "item")
public class Item {
@Id
@GeneratedValue
@Column(name = "id")
private int id;
@Column(name = "name")
private String itemName;
@Column(name = "price")
private double itemPrice;
@Enumerated(EnumType.STRING)
@Column(name = "item_type")
private ItemType itemType;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created_on")
private Date createdOn;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public double getItemPrice() {
return itemPrice;
}
public void setItemPrice(double itemPrice) {
this.itemPrice = itemPrice;
}
public ItemType getItemType() {
return itemType;
}
public void setItemType(ItemType itemType) {
this.itemType = itemType;
}
public Date getCreatedOn() {
return createdOn;
}
public void setCreatedOn(Date createdOn) {
this.createdOn = createdOn;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Item item = (Item) o;
return id == item.id &&
Double.compare(item.itemPrice, itemPrice) == 0 &&
Objects.equals(itemName, item.itemName) &&
itemType == item.itemType &&
Objects.equals(createdOn, item.createdOn);
}
@Override
public int hashCode() {
return Objects.hash(id, itemName, itemPrice, itemType, createdOn);
}
}

View File

@ -0,0 +1,44 @@
package com.baeldung.hibernate.persistmaps.mapkey;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.MapKey;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.util.Map;
@Entity
@Table(name = "orders")
public class Order {
@Id
@GeneratedValue
@Column(name = "id")
private int id;
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "order_item_mapping", joinColumns = {@JoinColumn(name = "order_id", referencedColumnName = "id")},
inverseJoinColumns = {@JoinColumn(name = "item_id", referencedColumnName = "id")})
@MapKey(name = "itemName")
private Map<String, Item> itemMap;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Map<String, Item> getItemMap() {
return itemMap;
}
public void setItemMap(Map<String, Item> itemMap) {
this.itemMap = itemMap;
}
}

View File

@ -0,0 +1,43 @@
package com.baeldung.hibernate.persistmaps.mapkeycolumn;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.MapKeyColumn;
import javax.persistence.Table;
import java.util.Map;
@Entity
@Table(name = "orders")
public class Order {
@Id
@GeneratedValue
@Column(name = "id")
private int id;
@ElementCollection
@CollectionTable(name = "order_item_mapping", joinColumns = {@JoinColumn(name = "order_id", referencedColumnName = "id")})
@MapKeyColumn(name = "item_name")
@Column(name = "price")
private Map<String, Double> itemPriceMap;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Map<String, Double> getItemPriceMap() {
return itemPriceMap;
}
public void setItemPriceMap(Map<String, Double> itemPriceMap) {
this.itemPriceMap = itemPriceMap;
}
}

View File

@ -0,0 +1,48 @@
package com.baeldung.hibernate.persistmaps.mapkeyenumerated;
import com.baeldung.hibernate.persistmaps.ItemType;
import com.baeldung.hibernate.persistmaps.mapkey.Item;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.MapKeyEnumerated;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.util.Map;
@Entity
@Table(name = "orders")
public class Order {
@Id
@GeneratedValue
@Column(name = "id")
private int id;
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "order_item_mapping", joinColumns = {@JoinColumn(name = "order_id", referencedColumnName = "id")},
inverseJoinColumns = {@JoinColumn(name = "item_id", referencedColumnName = "id")})
@MapKeyEnumerated(EnumType.STRING)
private Map<ItemType, Item> itemMap;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Map<ItemType, Item> getItemMap() {
return itemMap;
}
public void setItemMap(Map<ItemType, Item> itemMap) {
this.itemMap = itemMap;
}
}

View File

@ -0,0 +1,112 @@
package com.baeldung.hibernate.persistmaps.mapkeyjoincolumn;
import com.baeldung.hibernate.persistmaps.ItemType;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import java.util.Date;
import java.util.Objects;
@Entity
@Table(name = "item")
public class Item {
@Id
@GeneratedValue
@Column(name = "id")
private int id;
@Column(name = "name")
private String itemName;
@Column(name = "price")
private double itemPrice;
@Column(name = "item_type")
@Enumerated(EnumType.STRING)
private ItemType itemType;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created_on")
private Date createdOn;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "seller_id")
private Seller seller;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public double getItemPrice() {
return itemPrice;
}
public void setItemPrice(double itemPrice) {
this.itemPrice = itemPrice;
}
public ItemType getItemType() {
return itemType;
}
public void setItemType(ItemType itemType) {
this.itemType = itemType;
}
public Date getCreatedOn() {
return createdOn;
}
public void setCreatedOn(Date createdOn) {
this.createdOn = createdOn;
}
public Seller getSeller() {
return seller;
}
public void setSeller(Seller seller) {
this.seller = seller;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Item item = (Item) o;
return id == item.id &&
Double.compare(item.itemPrice, itemPrice) == 0 &&
Objects.equals(itemName, item.itemName) &&
itemType == item.itemType &&
Objects.equals(createdOn, item.createdOn) &&
Objects.equals(seller, item.seller);
}
@Override
public int hashCode() {
return Objects.hash(id, itemName, itemPrice, itemType, createdOn, seller);
}
}

View File

@ -0,0 +1,44 @@
package com.baeldung.hibernate.persistmaps.mapkeyjoincolumn;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.MapKeyJoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.util.Map;
@Entity
@Table(name = "orders")
public class Order {
@Id
@GeneratedValue
@Column(name = "id")
private int id;
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "order_item_mapping", joinColumns = {@JoinColumn(name = "order_id", referencedColumnName = "id")},
inverseJoinColumns = {@JoinColumn(name = "item_id", referencedColumnName = "id")})
@MapKeyJoinColumn(name = "seller_id")
private Map<Seller, Item> sellerItemMap;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Map<Seller, Item> getSellerItemMap() {
return sellerItemMap;
}
public void setSellerItemMap(Map<Seller, Item> sellerItemMap) {
this.sellerItemMap = sellerItemMap;
}
}

View File

@ -0,0 +1,51 @@
package com.baeldung.hibernate.persistmaps.mapkeyjoincolumn;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Objects;
@Entity
@Table(name = "seller")
public class Seller {
@Id
@GeneratedValue
@Column(name = "id")
private int id;
@Column(name = "name")
private String sellerName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSellerName() {
return sellerName;
}
public void setSellerName(String sellerName) {
this.sellerName = sellerName;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Seller seller = (Seller) o;
return Objects.equals(sellerName, seller.sellerName);
}
@Override
public int hashCode() {
return Objects.hash(sellerName);
}
}

View File

@ -0,0 +1,48 @@
package com.baeldung.hibernate.persistmaps.mapkeytemporal;
import com.baeldung.hibernate.persistmaps.mapkey.Item;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.MapKeyTemporal;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.TemporalType;
import java.util.Date;
import java.util.Map;
@Entity
@Table(name = "orders")
public class Order {
@Id
@GeneratedValue
@Column(name = "id")
private int id;
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "order_item_mapping", joinColumns = {@JoinColumn(name = "order_id", referencedColumnName = "id")},
inverseJoinColumns = {@JoinColumn(name = "item_id", referencedColumnName = "id")})
@MapKeyTemporal(TemporalType.TIMESTAMP)
private Map<Date, Item> itemMap;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Map<Date, Item> getItemMap() {
return itemMap;
}
public void setItemMap(Map<Date, Item> itemMap) {
this.itemMap = itemMap;
}
}

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>
</configuration>

View File

@ -0,0 +1,79 @@
package com.baeldung.hibernate.persistmaps;
import com.baeldung.hibernate.HibernateUtil;
import com.baeldung.hibernate.Strategy;
import com.baeldung.hibernate.persistmaps.mapkeycolumn.Order;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
public class MapKeyColumnIntegrationTest {
private static SessionFactory sessionFactory;
private Session session;
@BeforeClass
public static void beforeTests() {
sessionFactory = HibernateUtil.getSessionFactory(Strategy.MAP_KEY_COLUMN_BASED);
}
@Before
public void setUp() {
session = sessionFactory.openSession();
session.beginTransaction();
}
@Test
public void givenData_whenInsertUsingMapKeyColumn_thenPersistMap() {
Map<String, Double> itemPriceMap = new HashMap<>();
itemPriceMap.put("Wrangler Jeans", 150.0);
itemPriceMap.put("Lee Jeans", 180.0);
Order order = new Order();
order.setItemPriceMap(itemPriceMap);
session.persist(order);
session.getTransaction().commit();
assertInsertedData();
}
private void assertInsertedData() {
@SuppressWarnings("unchecked")
List<Order> orderList = session.createQuery("FROM Order").list();
assertNotNull(orderList);
assertEquals(1, orderList.size());
Order order = orderList.get(0);
Map<String, Double> itemPriceMap = order.getItemPriceMap();
assertNotNull(itemPriceMap);
assertEquals(itemPriceMap.size(), 2);
assertEquals((Double) 150.0, itemPriceMap.get("Wrangler Jeans"));
assertEquals((Double) 180.0, itemPriceMap.get("Lee Jeans"));
}
@After
public void tearDown() {
session.close();
}
@AfterClass
public static void afterTests() {
sessionFactory.close();
}
}

View File

@ -0,0 +1,95 @@
package com.baeldung.hibernate.persistmaps;
import com.baeldung.hibernate.HibernateUtil;
import com.baeldung.hibernate.Strategy;
import com.baeldung.hibernate.persistmaps.mapkey.Item;
import com.baeldung.hibernate.persistmaps.mapkeyenumerated.Order;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import java.time.Instant;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
public class MapKeyEnumeratedIntegrationTest {
private static SessionFactory sessionFactory;
private Session session;
@BeforeClass
public static void beforeTests() {
sessionFactory = HibernateUtil.getSessionFactory(Strategy.MAP_KEY_ENUMERATED_BASED);
}
@Before
public void setUp() {
session = sessionFactory.openSession();
session.beginTransaction();
}
@Test
public void givenData_whenInsertUsingMapKeyEnumerated_thenPersistMap() {
Item item1 = new Item();
item1.setItemName("Wrangler Jeans");
item1.setItemPrice(150.0);
item1.setItemType(ItemType.JEANS);
item1.setCreatedOn(Date.from(Instant.ofEpochSecond(1554926573)));
Item item2 = new Item();
item2.setItemName("Armani Tshirts");
item2.setItemPrice(180.0);
item2.setItemType(ItemType.TSHIRTS);
item2.setCreatedOn(Date.from(Instant.ofEpochSecond(1554890573)));
Map<ItemType, Item> itemMap = new HashMap<>();
itemMap.put(item1.getItemType(), item1);
itemMap.put(item2.getItemType(), item2);
Order order = new Order();
order.setItemMap(itemMap);
session.persist(order);
session.getTransaction().commit();
assertInsertedData(item1, item2);
}
private void assertInsertedData(Item expectedItem1, Item expectedItem2) {
@SuppressWarnings("unchecked")
List<Order> orderList = session.createQuery("FROM Order").list();
assertNotNull(orderList);
assertEquals(1, orderList.size());
Order order = orderList.get(0);
Map<ItemType, Item> itemMap = order.getItemMap();
assertNotNull(itemMap);
assertEquals(2, itemMap.size());
assertEquals(expectedItem1, itemMap.get(ItemType.JEANS));
assertEquals(expectedItem2, itemMap.get(ItemType.TSHIRTS));
}
@After
public void tearDown() {
session.close();
}
@AfterClass
public static void afterTests() {
sessionFactory.close();
}
}

View File

@ -0,0 +1,95 @@
package com.baeldung.hibernate.persistmaps;
import com.baeldung.hibernate.HibernateUtil;
import com.baeldung.hibernate.Strategy;
import com.baeldung.hibernate.persistmaps.mapkey.Item;
import com.baeldung.hibernate.persistmaps.mapkey.Order;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import java.time.Instant;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
public class MapKeyIntegrationTest {
private static SessionFactory sessionFactory;
private Session session;
@BeforeClass
public static void beforeTests() {
sessionFactory = HibernateUtil.getSessionFactory(Strategy.MAP_KEY_BASED);
}
@Before
public void setUp() {
session = sessionFactory.openSession();
session.beginTransaction();
}
@Test
public void givenData_whenInsertUsingMapKey_thenPersistMap() {
Item item1 = new Item();
item1.setItemName("Wrangler Jeans");
item1.setItemPrice(150.0);
item1.setItemType(ItemType.JEANS);
item1.setCreatedOn(Date.from(Instant.ofEpochSecond(1554926573)));
Item item2 = new Item();
item2.setItemName("Armani Tshirts");
item2.setItemPrice(180.0);
item2.setItemType(ItemType.TSHIRTS);
item2.setCreatedOn(Date.from(Instant.ofEpochSecond(1554890573)));
Map<String, Item> itemMap = new HashMap<>();
itemMap.put(item1.getItemName(), item1);
itemMap.put(item2.getItemName(), item2);
Order order = new Order();
order.setItemMap(itemMap);
session.persist(order);
session.getTransaction().commit();
assertInsertedData(item1, item2);
}
private void assertInsertedData(Item expectedItem1, Item expectedItem2) {
@SuppressWarnings("unchecked")
List<Order> orderList = session.createQuery("FROM Order").list();
assertNotNull(orderList);
assertEquals(1, orderList.size());
Order order = orderList.get(0);
Map<String, Item> itemMap = order.getItemMap();
assertNotNull(itemMap);
assertEquals(2, itemMap.size());
assertEquals(expectedItem1, itemMap.get("Wrangler Jeans"));
assertEquals(expectedItem2, itemMap.get("Armani Tshirts"));
}
@After
public void tearDown() {
session.close();
}
@AfterClass
public static void afterTests() {
sessionFactory.close();
}
}

View File

@ -0,0 +1,105 @@
package com.baeldung.hibernate.persistmaps;
import com.baeldung.hibernate.HibernateUtil;
import com.baeldung.hibernate.Strategy;
import com.baeldung.hibernate.persistmaps.mapkeyjoincolumn.Item;
import com.baeldung.hibernate.persistmaps.mapkeyjoincolumn.Order;
import com.baeldung.hibernate.persistmaps.mapkeyjoincolumn.Seller;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import java.time.Instant;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
public class MapKeyJoinColumnIntegrationTest {
private static SessionFactory sessionFactory;
private Session session;
@BeforeClass
public static void beforeTests() {
sessionFactory = HibernateUtil.getSessionFactory(Strategy.MAP_KEY_JOIN_COLUMN_BASED);
}
@Before
public void setUp() {
session = sessionFactory.openSession();
session.beginTransaction();
}
@Test
public void givenData_whenInsertUsingMapKeyJoinColumn_thenPersistMap() {
Seller seller1 = new Seller();
seller1.setSellerName("Walmart");
Item item1 = new Item();
item1.setItemName("Wrangler Jeans");
item1.setItemPrice(150.0);
item1.setItemType(ItemType.JEANS);
item1.setCreatedOn(Date.from(Instant.ofEpochSecond(1554926573)));
item1.setSeller(seller1);
Seller seller2 = new Seller();
seller2.setSellerName("Amazon");
Item item2 = new Item();
item2.setItemName("Armani Tshirts");
item2.setItemPrice(180.0);
item2.setItemType(ItemType.TSHIRTS);
item2.setCreatedOn(Date.from(Instant.ofEpochSecond(1554890573)));
item2.setSeller(seller2);
Map<Seller, Item> itemSellerMap = new HashMap<>();
itemSellerMap.put(seller1, item1);
itemSellerMap.put(seller2, item2);
Order order = new Order();
order.setSellerItemMap(itemSellerMap);
session.persist(order);
session.getTransaction().commit();
assertInsertedData(seller1, item1, seller2, item2);
}
private void assertInsertedData(Seller seller1, Item expectedItem1, Seller seller2, Item expectedItem2) {
@SuppressWarnings("unchecked")
List<Order> orderList = session.createQuery("FROM Order").list();
assertNotNull(orderList);
assertEquals(1, orderList.size());
Order order = orderList.get(0);
Map<Seller, Item> sellerItemMap = order.getSellerItemMap();
assertNotNull(sellerItemMap);
assertEquals(2, sellerItemMap.size());
assertEquals(expectedItem1, sellerItemMap.get(seller1));
assertEquals(expectedItem2, sellerItemMap.get(seller2));
}
@After
public void tearDown() {
session.close();
}
@AfterClass
public static void afterTests() {
sessionFactory.close();
}
}

View File

@ -0,0 +1,96 @@
package com.baeldung.hibernate.persistmaps;
import com.baeldung.hibernate.HibernateUtil;
import com.baeldung.hibernate.Strategy;
import com.baeldung.hibernate.persistmaps.mapkey.Item;
import com.baeldung.hibernate.persistmaps.mapkeytemporal.Order;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import java.time.Instant;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
public class MapKeyTemporalIntegrationTest {
private static SessionFactory sessionFactory;
private Session session;
@BeforeClass
public static void beforeTests() {
sessionFactory = HibernateUtil.getSessionFactory(Strategy.MAP_KEY_TEMPORAL_BASED);
}
@Before
public void setUp() {
session = sessionFactory.openSession();
session.beginTransaction();
}
@Test
public void givenData_whenInsertUsingMapKeyEnumerated_thenPersistMap() {
Date item1CreatedOn = Date.from(Instant.ofEpochSecond(1554926573));
Item item1 = new Item();
item1.setItemName("Wrangler Jeans");
item1.setItemPrice(150.0);
item1.setItemType(ItemType.JEANS);
item1.setCreatedOn(item1CreatedOn);
Date item2CreatedOn = Date.from(Instant.ofEpochSecond(1554890573));
Item item2 = new Item();
item2.setItemName("Armani Tshirts");
item2.setItemPrice(180.0);
item2.setItemType(ItemType.TSHIRTS);
item2.setCreatedOn(item2CreatedOn);
Map<Date, Item> itemMap = new HashMap<>();
itemMap.put(item1CreatedOn, item1);
itemMap.put(item2CreatedOn, item2);
Order order = new Order();
order.setItemMap(itemMap);
session.persist(order);
session.getTransaction().commit();
assertInsertedData(item1CreatedOn, item1, item2CreatedOn, item2);
}
private void assertInsertedData(Date item1CreatedOn, Item expectedItem1, Date item2CreatedOn, Item expectedItem2) {
@SuppressWarnings("unchecked")
List<Order> orderList = session.createQuery("FROM Order").list();
assertNotNull(orderList);
assertEquals(1, orderList.size());
Order order = orderList.get(0);
Map<Date, Item> itemMap = order.getItemMap();
assertNotNull(itemMap);
assertEquals(2, itemMap.size());
assertEquals(expectedItem1, itemMap.get(item1CreatedOn));
assertEquals(expectedItem2, itemMap.get(item2CreatedOn));
}
@After
public void tearDown() {
session.close();
}
@AfterClass
public static void afterTests() {
sessionFactory.close();
}
}

View File

@ -0,0 +1,10 @@
hibernate.connection.driver_class=org.h2.Driver
hibernate.connection.url=jdbc:h2:mem:mydb1;DB_CLOSE_DELAY=-1
hibernate.connection.username=sa
hibernate.connection.autocommit=true
jdbc.password=
hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=create-drop

View File

@ -22,6 +22,7 @@
<module>hbase</module>
<module>hibernate5</module>
<module>hibernate-ogm</module>
<module>hibernate-mapping</module>
<module>influxdb</module>
<module>java-cassandra</module>
<module>java-cockroachdb</module>

View File

@ -967,6 +967,7 @@
<module>libraries</module> <!-- very long running -->
<module>persistence-modules/hibernate5</module>
<module>persistence-modules/hibernate-mapping</module>
<module>persistence-modules/java-jpa</module>
<module>persistence-modules/java-mongodb</module>
<module>persistence-modules/jnosql</module>