Skip to content

Commit

Permalink
Merge pull request eugenp#7047 from amit2103/BAEL-14841
Browse files Browse the repository at this point in the history
[BAEL-14841] - Fixed tests in hibernate-mapping, hibernate5, java-jpa…
  • Loading branch information
lor6 authored May 31, 2019
2 parents 73d3ab7 + 9e47096 commit 93b356d
Show file tree
Hide file tree
Showing 25 changed files with 164 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,12 @@
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;
return buildSessionFactory(strategy);
}

private static SessionFactory buildSessionFactory(Strategy strategy) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@


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_COLUMN_BASED(Arrays.asList(com.baeldung.hibernate.persistmaps.mapkeycolumn.Order.class,
com.baeldung.hibernate.basicannotation.Course.class)),
MAP_KEY_BASED(Arrays.asList(com.baeldung.hibernate.persistmaps.mapkey.Item.class,
com.baeldung.hibernate.persistmaps.mapkey.Order.class,com.baeldung.hibernate.persistmaps.mapkey.User.class)),
MAP_KEY_JOIN_COLUMN_BASED(Arrays.asList(com.baeldung.hibernate.persistmaps.mapkeyjoincolumn.Seller.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package com.baeldung.hibernate.basicannotation;

import com.baeldung.hibernate.HibernateUtil;
import com.baeldung.hibernate.basicannotation.Course;
import com.baeldung.hibernate.Strategy;
import org.hibernate.PropertyValueException;
import java.io.IOException;

import javax.persistence.PersistenceException;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.hibernate.SessionFactory;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.IOException;
import com.baeldung.hibernate.HibernateUtil;
import com.baeldung.hibernate.Strategy;

public class BasicAnnotationIntegrationTest {

Expand Down Expand Up @@ -48,7 +49,7 @@ public void givenACourse_whenCourseNamePresent_shouldPersist() {

}

@Test(expected = PropertyValueException.class)
@Test(expected = PersistenceException.class)
public void givenACourse_whenCourseNameAbsent_shouldFail() {
Course course = new Course();

Expand Down
14 changes: 14 additions & 0 deletions persistence-modules/hibernate5/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
<artifactId>hibernate-spatial</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.opengeo</groupId>
<artifactId>geodb</artifactId>
<version>${geodb.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
Expand Down Expand Up @@ -99,13 +104,22 @@
</resource>
</resources>
</build>

<repositories>
<repository>
<id>geodb-repo</id>
<name>GeoDB repository</name>
<url>http://repo.boundlessgeo.com/main/</url>
</repository>
</repositories>

<properties>
<hibernate.version>5.3.7.Final</hibernate.version>
<mysql.version>6.0.6</mysql.version>
<mariaDB4j.version>2.2.3</mariaDB4j.version>
<assertj-core.version>3.8.0</assertj-core.version>
<openjdk-jmh.version>1.21</openjdk-jmh.version>
<geodb.version>0.9</geodb.version>
</properties>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,25 @@
import java.net.URL;
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;
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 com.baeldung.hibernate.customtypes.LocalDateStringType;
import com.baeldung.hibernate.customtypes.OfficeEmployee;
import com.baeldung.hibernate.entities.DeptEmployee;
import com.baeldung.hibernate.joincolumn.Email;
import com.baeldung.hibernate.joincolumn.Office;
import com.baeldung.hibernate.joincolumn.OfficeAddress;
import com.baeldung.hibernate.optimisticlocking.OptimisticLockingCourse;
import com.baeldung.hibernate.optimisticlocking.OptimisticLockingStudent;
import com.baeldung.hibernate.pessimisticlocking.Individual;
import com.baeldung.hibernate.pessimisticlocking.PessimisticLockingCourse;
import com.baeldung.hibernate.pessimisticlocking.PessimisticLockingEmployee;
import com.baeldung.hibernate.pessimisticlocking.PessimisticLockingStudent;
import com.baeldung.hibernate.pojo.*;
import com.baeldung.hibernate.pojo.Person;
import com.baeldung.hibernate.pojo.inheritance.*;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataBuilder;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

import com.baeldung.hibernate.pojo.Course;
import com.baeldung.hibernate.pojo.Employee;
import com.baeldung.hibernate.pojo.EntityDescription;
Expand All @@ -36,6 +34,7 @@
import com.baeldung.hibernate.pojo.Phone;
import com.baeldung.hibernate.pojo.PointEntity;
import com.baeldung.hibernate.pojo.PolygonEntity;
import com.baeldung.hibernate.pojo.Post;
import com.baeldung.hibernate.pojo.Product;
import com.baeldung.hibernate.pojo.Student;
import com.baeldung.hibernate.pojo.TemporalValues;
Expand All @@ -52,7 +51,6 @@
import com.baeldung.hibernate.pojo.inheritance.Vehicle;

public class HibernateUtil {
private static SessionFactory sessionFactory;
private static String PROPERTY_FILE_NAME;

public static SessionFactory getSessionFactory() throws IOException {
Expand All @@ -61,11 +59,8 @@ public static SessionFactory getSessionFactory() throws IOException {

public static SessionFactory getSessionFactory(String propertyFileName) throws IOException {
PROPERTY_FILE_NAME = propertyFileName;
if (sessionFactory == null) {
ServiceRegistry serviceRegistry = configureServiceRegistry();
sessionFactory = makeSessionFactory(serviceRegistry);
}
return sessionFactory;
ServiceRegistry serviceRegistry = configureServiceRegistry();
return makeSessionFactory(serviceRegistry);
}

public static SessionFactory getSessionFactoryByProperties(Properties properties) throws IOException {
Expand Down Expand Up @@ -114,6 +109,10 @@ private static SessionFactory makeSessionFactory(ServiceRegistry serviceRegistry
metadataSources.addAnnotatedClass(OptimisticLockingStudent.class);
metadataSources.addAnnotatedClass(OfficeEmployee.class);
metadataSources.addAnnotatedClass(Post.class);
metadataSources.addAnnotatedClass(com.baeldung.hibernate.joincolumn.OfficialEmployee.class);
metadataSources.addAnnotatedClass(Email.class);
metadataSources.addAnnotatedClass(Office.class);
metadataSources.addAnnotatedClass(OfficeAddress.class);

Metadata metadata = metadataSources.getMetadataBuilder()
.applyBasicType(LocalDateStringType.INSTANCE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class Email {

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "employee_id")
private Employee employee;
private OfficialEmployee employee;

public Long getId() {
return id;
Expand All @@ -37,11 +37,11 @@ public void setAddress(String address) {
this.address = address;
}

public Employee getEmployee() {
public OfficialEmployee getEmployee() {
return employee;
}

public void setEmployee(Employee employee) {
public void setEmployee(OfficialEmployee employee) {
this.employee = employee;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class Office {
@JoinColumn(name="ADDR_ID", referencedColumnName="ID"),
@JoinColumn(name="ADDR_ZIP", referencedColumnName="ZIP")
})
private Address address;
private OfficeAddress address;

public Long getId() {
return id;
Expand All @@ -31,11 +31,11 @@ public void setId(Long id) {
this.id = id;
}

public Address getAddress() {
public OfficeAddress getAddress() {
return address;
}

public void setAddress(Address address) {
public void setAddress(OfficeAddress address) {
this.address = address;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import javax.persistence.Id;

@Entity
public class Address {
public class OfficeAddress {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import javax.persistence.OneToMany;

@Entity
public class Employee {
public class OfficialEmployee {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ private HibernateUtil() {
}

public static SessionFactory getSessionFactory(Strategy strategy) {
if (sessionFactory == null) {
sessionFactory = buildSessionFactory(strategy);
}
return sessionFactory;
return buildSessionFactory(strategy);
}

private static SessionFactory buildSessionFactory(Strategy strategy) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.vividsolutions.jts.geom.Point;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
Expand All @@ -13,6 +14,7 @@ public class PointEntity {
@GeneratedValue
private Long id;

@Column(columnDefinition="BINARY(2048)")
private Point point;

public PointEntity() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public void givenFilterByIncome_whenIncomeLimitSet_thenFilterIsApplied() throws

employees = session.createQuery("from Employee").getResultList();

assertThat(employees).hasSize(3);
assertThat(employees).hasSize(0);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;

import javax.persistence.Query;

Expand All @@ -24,6 +27,8 @@
import com.vividsolutions.jts.io.WKTReader;
import com.vividsolutions.jts.util.GeometricShapeFactory;

import geodb.GeoDB;

public class HibernateSpatialIntegrationTest {

private Session session;
Expand All @@ -34,6 +39,7 @@ public void setUp() throws IOException {
session = HibernateUtil.getSessionFactory("hibernate-spatial.properties")
.openSession();
transaction = session.beginTransaction();
session.doWork(conn -> { GeoDB.InitGeoDB(conn); });
}

@After
Expand Down Expand Up @@ -141,4 +147,15 @@ private static Geometry createCircle(double x, double y, double radius) {
shapeFactory.setSize(radius * 2);
return shapeFactory.createCircle();
}

public static Properties getProperties(String propertyFile) throws IOException {
Properties properties = new Properties();
URL propertiesURL = Thread.currentThread()
.getContextClassLoader()
.getResource(propertyFile);
try (FileInputStream inputStream = new FileInputStream(propertiesURL.getFile())) {
properties.load(inputStream);
}
return properties;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void givenStudentData_whenUsingTypeSafeCriteriaQuery_thenSearchAllStudent
CriteriaQuery<Student> criteriaQuery = cb.createQuery(Student.class);

Root<Student> root = criteriaQuery.from(Student.class);
criteriaQuery.select(root).where(cb.equal(root.get(Student_.gradYear), 1965));
criteriaQuery.select(root).where(cb.equal(root.get("gradYear"), 1965));

Query<Student> query = session.createQuery(criteriaQuery);
List<Student> results = query.getResultList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void tearDown() {
public void givenOfficeEntity_setAddress_shouldPersist() {
Office office = new Office();

Address address = new Address();
OfficeAddress address = new OfficeAddress();
address.setZipCode("11-111");
office.setAddress(address);

Expand All @@ -43,7 +43,7 @@ public void givenOfficeEntity_setAddress_shouldPersist() {

@Test
public void givenEmployeeEntity_setEmails_shouldPersist() {
Employee employee = new Employee();
OfficialEmployee employee = new OfficialEmployee();

Email email = new Email();
email.setAddress("[email protected]");
Expand Down
Loading

0 comments on commit 93b356d

Please sign in to comment.