Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
1347e1517c
@ -14,9 +14,7 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.NoSuchElementException;
|
import java.util.NoSuchElementException;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.*;
|
||||||
import static org.assertj.core.api.Assertions.entry;
|
|
||||||
import static org.assertj.core.api.Assertions.withPrecision;
|
|
||||||
|
|
||||||
public class AssertJCoreTest {
|
public class AssertJCoreTest {
|
||||||
|
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
package com.baeldung.assertj.introduction;
|
package com.baeldung.assertj.introduction;
|
||||||
|
|
||||||
import com.google.common.base.Optional;
|
import com.google.common.base.Optional;
|
||||||
|
import com.google.common.collect.ArrayListMultimap;
|
||||||
import com.google.common.collect.HashBasedTable;
|
import com.google.common.collect.HashBasedTable;
|
||||||
import com.google.common.collect.Multimap;
|
import com.google.common.collect.Multimap;
|
||||||
import com.google.common.collect.Multimaps;
|
import com.google.common.collect.Multimaps;
|
||||||
import com.google.common.collect.Range;
|
import com.google.common.collect.Range;
|
||||||
import com.google.common.collect.Sets;
|
|
||||||
import com.google.common.collect.Table;
|
import com.google.common.collect.Table;
|
||||||
import com.google.common.collect.TreeRangeMap;
|
import com.google.common.collect.TreeRangeMap;
|
||||||
import com.google.common.io.Files;
|
import com.google.common.io.Files;
|
||||||
@ -14,6 +14,7 @@ import org.junit.Test;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
|
|
||||||
import static org.assertj.guava.api.Assertions.assertThat;
|
import static org.assertj.guava.api.Assertions.assertThat;
|
||||||
import static org.assertj.guava.api.Assertions.entry;
|
import static org.assertj.guava.api.Assertions.entry;
|
||||||
@ -22,17 +23,17 @@ public class AssertJGuavaTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenTwoEmptyFiles_whenComparingContent_thenEqual() throws Exception {
|
public void givenTwoEmptyFiles_whenComparingContent_thenEqual() throws Exception {
|
||||||
final File temp = File.createTempFile("bael", "dung");
|
final File temp1 = File.createTempFile("bael", "dung1");
|
||||||
final File temp2 = File.createTempFile("bael", "dung2");
|
final File temp2 = File.createTempFile("bael", "dung2");
|
||||||
|
|
||||||
assertThat(Files.asByteSource(temp))
|
assertThat(Files.asByteSource(temp1))
|
||||||
.hasSize(0)
|
.hasSize(0)
|
||||||
.hasSameContentAs(Files.asByteSource(temp2));
|
.hasSameContentAs(Files.asByteSource(temp2));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenMultimap_whenVerifying_thenCorrect() throws Exception {
|
public void givenMultimap_whenVerifying_thenCorrect() throws Exception {
|
||||||
final Multimap<Integer, String> mmap = Multimaps.newMultimap(new HashMap<>(), Sets::newHashSet);
|
final Multimap<Integer, String> mmap = ArrayListMultimap.create();
|
||||||
mmap.put(1, "one");
|
mmap.put(1, "one");
|
||||||
mmap.put(1, "1");
|
mmap.put(1, "1");
|
||||||
|
|
||||||
@ -43,6 +44,30 @@ public class AssertJGuavaTest {
|
|||||||
.contains(entry(1, "1"));
|
.contains(entry(1, "1"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenMultimaps_whenVerifyingContent_thenCorrect() throws Exception {
|
||||||
|
final Multimap<Integer, String> mmap1 = ArrayListMultimap.create();
|
||||||
|
mmap1.put(1, "one");
|
||||||
|
mmap1.put(1, "1");
|
||||||
|
mmap1.put(2, "two");
|
||||||
|
mmap1.put(2, "2");
|
||||||
|
|
||||||
|
final Multimap<Integer, String> mmap1_clone = Multimaps.newSetMultimap(new HashMap<>(), HashSet::new);
|
||||||
|
mmap1_clone.put(1, "one");
|
||||||
|
mmap1_clone.put(1, "1");
|
||||||
|
mmap1_clone.put(2, "two");
|
||||||
|
mmap1_clone.put(2, "2");
|
||||||
|
|
||||||
|
final Multimap<Integer, String> mmap2 = Multimaps.newSetMultimap(new HashMap<>(), HashSet::new);
|
||||||
|
mmap2.put(1, "one");
|
||||||
|
mmap2.put(1, "1");
|
||||||
|
|
||||||
|
assertThat(mmap1)
|
||||||
|
.containsAllEntriesOf(mmap2)
|
||||||
|
.containsAllEntriesOf(mmap1_clone)
|
||||||
|
.hasSameEntriesAs(mmap1_clone);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenOptional_whenVerifyingContent_thenShouldBeEqual() throws Exception {
|
public void givenOptional_whenVerifyingContent_thenShouldBeEqual() throws Exception {
|
||||||
final Optional<String> something = Optional.of("something");
|
final Optional<String> something = Optional.of("something");
|
||||||
|
@ -9,6 +9,7 @@
|
|||||||
- [Sorting with Hibernate](http://www.baeldung.com/hibernate-sort)
|
- [Sorting with Hibernate](http://www.baeldung.com/hibernate-sort)
|
||||||
- [Auditing with JPA, Hibernate, and Spring Data JPA](http://www.baeldung.com/database-auditing-jpa)
|
- [Auditing with JPA, Hibernate, and Spring Data JPA](http://www.baeldung.com/database-auditing-jpa)
|
||||||
- [Stored Procedures with Hibernate](http://www.baeldung.com/stored-procedures-with-hibernate-tutorial)
|
- [Stored Procedures with Hibernate](http://www.baeldung.com/stored-procedures-with-hibernate-tutorial)
|
||||||
|
- [Hibernate: save, persist, update, merge, saveOrUpdate](http://www.baeldung.com/hibernate-save-persist-update-merge-saveorupdate/)
|
||||||
|
|
||||||
### Quick Start
|
### Quick Start
|
||||||
|
|
||||||
|
@ -149,6 +149,13 @@
|
|||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hsqldb</groupId>
|
||||||
|
<artifactId>hsqldb</artifactId>
|
||||||
|
<version>2.3.4</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -0,0 +1,31 @@
|
|||||||
|
package com.baeldung.persistence.model;
|
||||||
|
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Person {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
private 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;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,272 @@
|
|||||||
|
package com.baeldung.persistence.save;
|
||||||
|
|
||||||
|
|
||||||
|
import com.baeldung.persistence.model.Person;
|
||||||
|
import org.hibernate.HibernateException;
|
||||||
|
import org.hibernate.Session;
|
||||||
|
import org.hibernate.SessionFactory;
|
||||||
|
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||||
|
import org.hibernate.cfg.Configuration;
|
||||||
|
import org.hibernate.dialect.HSQLDialect;
|
||||||
|
import org.hibernate.service.ServiceRegistry;
|
||||||
|
import org.junit.*;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Testing specific implementation details for different methods:
|
||||||
|
* persist, save, merge, update, saveOrUpdate.
|
||||||
|
*/
|
||||||
|
public class SaveMethodsTest {
|
||||||
|
|
||||||
|
private static SessionFactory sessionFactory;
|
||||||
|
|
||||||
|
private Session session;
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void beforeTests() {
|
||||||
|
Configuration configuration = new Configuration()
|
||||||
|
.addAnnotatedClass(Person.class)
|
||||||
|
.setProperty("hibernate.dialect", HSQLDialect.class.getName())
|
||||||
|
.setProperty("hibernate.connection.driver_class", org.hsqldb.jdbcDriver.class.getName())
|
||||||
|
.setProperty("hibernate.connection.url", "jdbc:hsqldb:mem:test")
|
||||||
|
.setProperty("hibernate.connection.username", "sa")
|
||||||
|
.setProperty("hibernate.connection.password", "")
|
||||||
|
.setProperty("hibernate.hbm2ddl.auto", "update");
|
||||||
|
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
|
||||||
|
configuration.getProperties()).build();
|
||||||
|
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
session = sessionFactory.openSession();
|
||||||
|
session.beginTransaction();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenPersistTransient_thenSavedToDatabaseOnCommit() {
|
||||||
|
|
||||||
|
Person person = new Person();
|
||||||
|
person.setName("John");
|
||||||
|
session.persist(person);
|
||||||
|
|
||||||
|
session.getTransaction().commit();
|
||||||
|
session.close();
|
||||||
|
|
||||||
|
session = sessionFactory.openSession();
|
||||||
|
session.beginTransaction();
|
||||||
|
|
||||||
|
assertNotNull(session.get(Person.class, person.getId()));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenPersistPersistent_thenNothingHappens() {
|
||||||
|
|
||||||
|
Person person = new Person();
|
||||||
|
person.setName("John");
|
||||||
|
|
||||||
|
session.persist(person);
|
||||||
|
Long id1 = person.getId();
|
||||||
|
|
||||||
|
session.persist(person);
|
||||||
|
Long id2 = person.getId();
|
||||||
|
|
||||||
|
assertEquals(id1, id2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = HibernateException.class)
|
||||||
|
public void whenPersistDetached_thenThrowsException() {
|
||||||
|
|
||||||
|
Person person = new Person();
|
||||||
|
person.setName("John");
|
||||||
|
session.persist(person);
|
||||||
|
session.evict(person);
|
||||||
|
|
||||||
|
session.persist(person);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSaveTransient_thenIdGeneratedImmediately() {
|
||||||
|
|
||||||
|
Person person = new Person();
|
||||||
|
person.setName("John");
|
||||||
|
|
||||||
|
assertNull(person.getId());
|
||||||
|
|
||||||
|
Long id = (Long) session.save(person);
|
||||||
|
|
||||||
|
assertNotNull(id);
|
||||||
|
|
||||||
|
session.getTransaction().commit();
|
||||||
|
session.close();
|
||||||
|
|
||||||
|
assertEquals(id, person.getId());
|
||||||
|
|
||||||
|
session = sessionFactory.openSession();
|
||||||
|
session.beginTransaction();
|
||||||
|
|
||||||
|
assertNotNull(session.get(Person.class, person.getId()));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSavePersistent_thenNothingHappens() {
|
||||||
|
|
||||||
|
Person person = new Person();
|
||||||
|
person.setName("John");
|
||||||
|
Long id1 = (Long) session.save(person);
|
||||||
|
Long id2 = (Long) session.save(person);
|
||||||
|
assertEquals(id1, id2);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSaveDetached_thenNewInstancePersisted() {
|
||||||
|
|
||||||
|
Person person = new Person();
|
||||||
|
person.setName("John");
|
||||||
|
Long id1 = (Long) session.save(person);
|
||||||
|
session.evict(person);
|
||||||
|
|
||||||
|
Long id2 = (Long) session.save(person);
|
||||||
|
assertNotEquals(id1, id2);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenMergeDetached_thenEntityUpdatedFromDatabase() {
|
||||||
|
|
||||||
|
Person person = new Person();
|
||||||
|
person.setName("John");
|
||||||
|
session.save(person);
|
||||||
|
session.evict(person);
|
||||||
|
|
||||||
|
person.setName("Mary");
|
||||||
|
Person mergedPerson = (Person) session.merge(person);
|
||||||
|
|
||||||
|
assertNotSame(person, mergedPerson);
|
||||||
|
assertEquals("Mary", mergedPerson.getName());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenMergeTransient_thenNewEntitySavedToDatabase() {
|
||||||
|
|
||||||
|
Person person = new Person();
|
||||||
|
person.setName("John");
|
||||||
|
Person mergedPerson = (Person) session.merge(person);
|
||||||
|
|
||||||
|
session.getTransaction().commit();
|
||||||
|
session.beginTransaction();
|
||||||
|
|
||||||
|
assertNull(person.getId());
|
||||||
|
assertNotNull(mergedPerson.getId());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenMergePersistent_thenReturnsSameObject() {
|
||||||
|
|
||||||
|
Person person = new Person();
|
||||||
|
person.setName("John");
|
||||||
|
session.save(person);
|
||||||
|
|
||||||
|
Person mergedPerson = (Person) session.merge(person);
|
||||||
|
|
||||||
|
assertSame(person, mergedPerson);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUpdateDetached_thenEntityUpdatedFromDatabase() {
|
||||||
|
|
||||||
|
Person person = new Person();
|
||||||
|
person.setName("John");
|
||||||
|
session.save(person);
|
||||||
|
session.evict(person);
|
||||||
|
|
||||||
|
person.setName("Mary");
|
||||||
|
session.update(person);
|
||||||
|
assertEquals("Mary", person.getName());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = HibernateException.class)
|
||||||
|
public void whenUpdateTransient_thenThrowsException() {
|
||||||
|
|
||||||
|
Person person = new Person();
|
||||||
|
person.setName("John");
|
||||||
|
session.update(person);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUpdatePersistent_thenNothingHappens() {
|
||||||
|
|
||||||
|
Person person = new Person();
|
||||||
|
person.setName("John");
|
||||||
|
session.save(person);
|
||||||
|
|
||||||
|
session.update(person);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSaveOrUpdateDetached_thenEntityUpdatedFromDatabase() {
|
||||||
|
|
||||||
|
Person person = new Person();
|
||||||
|
person.setName("John");
|
||||||
|
session.save(person);
|
||||||
|
session.evict(person);
|
||||||
|
|
||||||
|
person.setName("Mary");
|
||||||
|
session.saveOrUpdate(person);
|
||||||
|
assertEquals("Mary", person.getName());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSaveOrUpdateTransient_thenSavedToDatabaseOnCommit() {
|
||||||
|
|
||||||
|
Person person = new Person();
|
||||||
|
person.setName("John");
|
||||||
|
session.saveOrUpdate(person);
|
||||||
|
|
||||||
|
session.getTransaction().commit();
|
||||||
|
session.close();
|
||||||
|
|
||||||
|
session = sessionFactory.openSession();
|
||||||
|
session.beginTransaction();
|
||||||
|
|
||||||
|
assertNotNull(session.get(Person.class, person.getId()));
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSaveOrUpdatePersistent_thenNothingHappens() {
|
||||||
|
|
||||||
|
Person person = new Person();
|
||||||
|
person.setName("John");
|
||||||
|
session.save(person);
|
||||||
|
|
||||||
|
session.saveOrUpdate(person);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void tearDown() {
|
||||||
|
session.getTransaction().commit();
|
||||||
|
session.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterClass
|
||||||
|
public static void afterTests() {
|
||||||
|
sessionFactory.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user