Skip to content

Commit

Permalink
Upgrade to Spring 5 and Hibernate 5 - TRUNK-5498
Browse files Browse the repository at this point in the history
  • Loading branch information
dkayiwa committed Mar 12, 2020
1 parent 7da5be1 commit 82c887e
Show file tree
Hide file tree
Showing 26 changed files with 300 additions and 149 deletions.
3 changes: 2 additions & 1 deletion api/src/main/java/org/openmrs/Allergy.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
Expand All @@ -41,7 +42,7 @@ public class Allergy extends BaseChangeableOpenmrsData {
public static final long serialVersionUID = 1;

@Id
@GeneratedValue
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "allergy_id")
private Integer allergyId;

Expand Down
3 changes: 2 additions & 1 deletion api/src/main/java/org/openmrs/Condition.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
Expand All @@ -44,7 +45,7 @@ public class Condition extends BaseChangeableOpenmrsData {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "condition_id")
private Integer conditionId;

Expand Down
3 changes: 2 additions & 1 deletion api/src/main/java/org/openmrs/Diagnosis.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
Expand All @@ -38,7 +39,7 @@ public class Diagnosis extends BaseChangeableOpenmrsData {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "diagnosis_id")
private Integer diagnosisId;

Expand Down
2 changes: 1 addition & 1 deletion api/src/main/java/org/openmrs/Encounter.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class Encounter extends BaseChangeableOpenmrsData {

// Fields
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "encounter_id")
private Integer encounterId;

Expand Down
2 changes: 1 addition & 1 deletion api/src/main/java/org/openmrs/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public class Person extends BaseChangeableOpenmrsData {
private static final Logger log = LoggerFactory.getLogger(Person.class);

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "person_id")
@DocumentId
protected Integer personId;
Expand Down
2 changes: 1 addition & 1 deletion api/src/main/java/org/openmrs/Visit.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
public class Visit extends BaseCustomizableData<VisitAttribute> implements Auditable, Customizable<VisitAttribute> {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "visit_id")
private Integer visitId;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,8 @@ public SessionFactory getSessionFactory() {
* @throws HibernateException Indicates problems cleaning up.
*/
public Connection close() throws HibernateException {
return getSession().close();
getSession().close();
return null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
import org.hibernate.Criteria;
import org.hibernate.FlushMode;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
Expand All @@ -26,8 +30,14 @@
import org.hibernate.type.StringType;
import org.hibernate.type.TextType;
import org.hibernate.type.Type;
import org.openmrs.Allergy;
import org.openmrs.Condition;
import org.openmrs.Diagnosis;
import org.openmrs.Encounter;
import org.openmrs.GlobalProperty;
import org.openmrs.OpenmrsObject;
import org.openmrs.Patient;
import org.openmrs.Visit;
import org.openmrs.api.APIException;
import org.openmrs.api.db.AdministrationDAO;
import org.openmrs.api.db.DAOException;
Expand Down Expand Up @@ -61,6 +71,8 @@ public class HibernateAdministrationDAO implements AdministrationDAO, Applicatio

private Configuration configuration;

private Metadata metaData;

private ApplicationContext applicationContext;

public HibernateAdministrationDAO() {
Expand Down Expand Up @@ -189,7 +201,17 @@ public int getMaximumPropertyLength(Class<? extends OpenmrsObject> aClass, Strin
configuration = sessionFactoryBean.getConfiguration();
}

PersistentClass persistentClass = configuration.getClassMapping(aClass.getName().split("_")[0]);
if (metaData == null) {
StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder()
.configure().applySettings(configuration.getProperties()).build();

metaData = new MetadataSources(standardRegistry).addAnnotatedClass(Allergy.class)
.addAnnotatedClass(Patient.class).addAnnotatedClass(Encounter.class)
.addAnnotatedClass(Diagnosis.class).addAnnotatedClass(Condition.class)
.addAnnotatedClass(Visit.class).getMetadataBuilder().build();
}

PersistentClass persistentClass = metaData.getEntityBinding(aClass.getName().split("_")[0]);
if (persistentClass == null) {
throw new APIException("Couldn't find a class in the hibernate configuration named: " + aClass.getName());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.SessionFactoryUtils;
import org.springframework.orm.hibernate4.SessionHolder;
import org.springframework.orm.hibernate5.SessionFactoryUtils;
import org.springframework.orm.hibernate5.SessionHolder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronizationManager;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import org.slf4j.LoggerFactory;
import org.slf4j.MarkerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;

public class HibernateSessionFactoryBean extends LocalSessionFactoryBean {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@

import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.id.Configurable;
import org.hibernate.id.IdentityGenerator;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.type.Type;

/**
Expand Down Expand Up @@ -48,17 +48,12 @@ public Serializable generate(SessionImplementor session, Object entity) throws H
}
return id;
}

/**
* @see org.hibernate.id.Configurable#configure(org.hibernate.type.Type, java.util.Properties,
* org.hibernate.dialect.Dialect)
*/

@Override
public void configure(Type type, Properties params, Dialect dialect) throws MappingException {
public void configure(Type type, Properties params, ServiceRegistry serviceRegistry) throws MappingException {
this.entityName = params.getProperty(ENTITY_NAME);
if (entityName == null) {
throw new MappingException("no entity name");
}
}

}
110 changes: 110 additions & 0 deletions api/src/main/java/org/springframework/core/JdkVersion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.core;

/**
* Internal helper class used to find the Java/JVM version that Spring is
* operating on, to allow for automatically adapting to the present platform's
* capabilities.
*
* <p>Note that Spring requires JVM 1.6 or higher, as of Spring 4.0.
*
* @author Rod Johnson
* @author Juergen Hoeller
* @author Rick Evans
* @author Sam Brannen
*/
public abstract class JdkVersion {

/**
* Constant identifying the 1.3.x JVM (JDK 1.3).
*/
public static final int JAVA_13 = 0;

/**
* Constant identifying the 1.4.x JVM (J2SE 1.4).
*/
public static final int JAVA_14 = 1;

/**
* Constant identifying the 1.5 JVM (Java 5).
*/
public static final int JAVA_15 = 2;

/**
* Constant identifying the 1.6 JVM (Java 6).
*/
public static final int JAVA_16 = 3;

/**
* Constant identifying the 1.7 JVM (Java 7).
*/
public static final int JAVA_17 = 4;

/**
* Constant identifying the 1.8 JVM (Java 8).
*/
public static final int JAVA_18 = 5;

/**
* Constant identifying the 1.9 JVM (Java 9).
*/
public static final int JAVA_19 = 6;

private static final String javaVersion;

private static final int majorJavaVersion;

static {
javaVersion = System.getProperty("java.version");
// version String should look like "1.4.2_10"
if (javaVersion.contains("1.9.")) {
majorJavaVersion = JAVA_19;
} else if (javaVersion.contains("1.8.")) {
majorJavaVersion = JAVA_18;
} else if (javaVersion.contains("1.7.")) {
majorJavaVersion = JAVA_17;
} else {
// else leave 1.6 as default (it's either 1.6 or unknown)
majorJavaVersion = JAVA_16;
}
}

/**
* Return the full Java version string, as returned by
* {@code System.getProperty("java.version")}.
* @return the full Java version string
* @see System#getProperty(String)
*/
public static String getJavaVersion() {
return javaVersion;
}

/**
* Get the major version code. This means we can do things like
* {@code if (getMajorJavaVersion() >= JAVA_17)}.
* @return a code comparable to the {@code JAVA_XX} codes in this class
* @see #JAVA_16
* @see #JAVA_17
* @see #JAVA_18
* @see #JAVA_19
*/
public static int getMajorJavaVersion() {
return majorJavaVersion;
}

}
Loading

0 comments on commit 82c887e

Please sign in to comment.