[JAVA-11332] Upgrade H2 version and fix tests (#12056)
* [JAVA-11332] Upgrade H2 version and fix tests * [JAVA-11332] Revert formatting changes * [JAVA-11332] Remove config files and code clean up
This commit is contained in:
+40
-16
@@ -1,20 +1,27 @@
|
||||
package com.baeldung.hibernate.customtypes;
|
||||
|
||||
import com.baeldung.hibernate.HibernateUtil;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.query.Query;
|
||||
import org.hibernate.boot.Metadata;
|
||||
import org.hibernate.boot.MetadataSources;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
import org.hibernate.cfg.Environment;
|
||||
import org.hibernate.service.ServiceRegistry;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import javax.persistence.TypedQuery;
|
||||
import java.time.LocalDate;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class HibernateCustomTypesManualTest {
|
||||
public class HibernateCustomTypesIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void givenEmployee_whenSavedWithCustomTypes_thenEntityIsSaved() throws IOException {
|
||||
public void givenEmployee_whenSavedWithCustomTypes_thenEntityIsSaved() {
|
||||
|
||||
final OfficeEmployee e = new OfficeEmployee();
|
||||
e.setDateOfJoining(LocalDate.now());
|
||||
@@ -39,13 +46,13 @@ public class HibernateCustomTypesManualTest {
|
||||
doInHibernate(this::sessionFactory, session -> {
|
||||
session.save(e);
|
||||
boolean contains = session.contains(e);
|
||||
Assert.assertTrue(contains);
|
||||
assertTrue(contains);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployee_whenCustomTypeInQuery_thenReturnEntity() throws IOException {
|
||||
public void givenEmployee_whenCustomTypeInQuery_thenReturnEntity() {
|
||||
|
||||
final OfficeEmployee e = new OfficeEmployee();
|
||||
e.setDateOfJoining(LocalDate.now());
|
||||
@@ -69,22 +76,39 @@ public class HibernateCustomTypesManualTest {
|
||||
doInHibernate(this::sessionFactory, session -> {
|
||||
session.save(e);
|
||||
|
||||
Query query = session.createQuery("FROM OfficeEmployee OE WHERE OE.empAddress.zipcode = :pinCode");
|
||||
TypedQuery<OfficeEmployee> query = session.createQuery("FROM OfficeEmployee OE WHERE OE.empAddress.zipcode = :pinCode", OfficeEmployee.class);
|
||||
query.setParameter("pinCode",100);
|
||||
int size = query.list().size();
|
||||
int size = query.getResultList().size();
|
||||
|
||||
Assert.assertEquals(1, size);
|
||||
assertEquals(1, size);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private SessionFactory sessionFactory() {
|
||||
try {
|
||||
return HibernateUtil.getSessionFactory("hibernate-customtypes.properties");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
|
||||
.applySettings(getProperties())
|
||||
.build();
|
||||
|
||||
return null;
|
||||
MetadataSources metadataSources = new MetadataSources(serviceRegistry);
|
||||
Metadata metadata = metadataSources
|
||||
.addAnnotatedClass(OfficeEmployee.class)
|
||||
.getMetadataBuilder()
|
||||
.applyBasicType(LocalDateStringType.INSTANCE)
|
||||
.build();
|
||||
|
||||
return metadata.buildSessionFactory();
|
||||
}
|
||||
|
||||
private static Map<String, String> getProperties() {
|
||||
Map<String, String> dbSettings = new HashMap<>();
|
||||
dbSettings.put(Environment.URL, "jdbc:h2:mem:mydb1;DB_CLOSE_DELAY=-1");
|
||||
dbSettings.put(Environment.USER, "sa");
|
||||
dbSettings.put(Environment.PASS, "");
|
||||
dbSettings.put(Environment.DRIVER, "org.h2.Driver");
|
||||
dbSettings.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
|
||||
dbSettings.put(Environment.SHOW_SQL, "true");
|
||||
dbSettings.put(Environment.HBM2DDL_AUTO, "create");
|
||||
return dbSettings;
|
||||
}
|
||||
}
|
||||
+43
-7
@@ -1,14 +1,21 @@
|
||||
package com.baeldung.hibernate.joincolumn;
|
||||
|
||||
import com.baeldung.hibernate.HibernateUtil;
|
||||
import java.io.IOException;
|
||||
import com.baeldung.hibernate.customtypes.LocalDateStringType;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.Transaction;
|
||||
|
||||
import org.hibernate.boot.Metadata;
|
||||
import org.hibernate.boot.MetadataSources;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.cfg.Environment;
|
||||
import org.hibernate.service.ServiceRegistry;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class JoinColumnIntegrationTest {
|
||||
|
||||
@@ -16,9 +23,8 @@ public class JoinColumnIntegrationTest {
|
||||
private Transaction transaction;
|
||||
|
||||
@Before
|
||||
public void setUp() throws IOException {
|
||||
session = HibernateUtil.getSessionFactory("hibernate-spatial.properties")
|
||||
.openSession();
|
||||
public void setUp() {
|
||||
session = sessionFactory().openSession();
|
||||
transaction = session.beginTransaction();
|
||||
}
|
||||
|
||||
@@ -54,4 +60,34 @@ public class JoinColumnIntegrationTest {
|
||||
session.clear();
|
||||
}
|
||||
|
||||
}
|
||||
private SessionFactory sessionFactory() {
|
||||
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
|
||||
.applySettings(getProperties())
|
||||
.build();
|
||||
|
||||
MetadataSources metadataSources = new MetadataSources(serviceRegistry);
|
||||
Metadata metadata = metadataSources
|
||||
.addAnnotatedClass(Email.class)
|
||||
.addAnnotatedClass(Office.class)
|
||||
.addAnnotatedClass(OfficeAddress.class)
|
||||
.addAnnotatedClass(OfficialEmployee.class)
|
||||
.getMetadataBuilder()
|
||||
.applyBasicType(LocalDateStringType.INSTANCE)
|
||||
.build();
|
||||
|
||||
return metadata.buildSessionFactory();
|
||||
}
|
||||
|
||||
private static Map<String, String> getProperties() {
|
||||
Map<String, String> dbSettings = new HashMap<>();
|
||||
dbSettings.put(Environment.URL, "jdbc:h2:mem:mydbJoinColumn;DB_CLOSE_DELAY=-1");
|
||||
dbSettings.put(Environment.USER, "sa");
|
||||
dbSettings.put(Environment.PASS, "");
|
||||
dbSettings.put(Environment.DRIVER, "org.h2.Driver");
|
||||
dbSettings.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
|
||||
dbSettings.put(Environment.SHOW_SQL, "true");
|
||||
dbSettings.put(Environment.HBM2DDL_AUTO, "create");
|
||||
return dbSettings;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
-14
@@ -1,14 +0,0 @@
|
||||
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.spatial.dialect.h2geodb.GeoDBDialect
|
||||
hibernate.show_sql=true
|
||||
hibernate.hbm2ddl.auto=create-drop
|
||||
|
||||
hibernate.c3p0.min_size=5
|
||||
hibernate.c3p0.max_size=20
|
||||
hibernate.c3p0.acquire_increment=5
|
||||
hibernate.c3p0.timeout=1800
|
||||
Reference in New Issue
Block a user