Spring JPA: no time for comments
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Thu, 21 Jul 2016 20:05:14 +0000 (22:05 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Thu, 21 Jul 2016 20:05:14 +0000 (22:05 +0200)
SpringJava/JPA/spring-jpa-bom/pom.xml
SpringJava/JPA/spring-jpa-persistence/src/main/java/de/spring/example/context/StaticContextHolder.java [new file with mode: 0644]
SpringJava/JPA/spring-jpa-persistence/src/main/java/de/spring/example/context/UsernameThreadContext.java
SpringJava/JPA/spring-jpa-persistence/src/main/java/de/spring/example/persistence/converters/LocalDateTimeAttributeConverter.java [new file with mode: 0644]
SpringJava/JPA/spring-jpa-persistence/src/main/java/de/spring/example/persistence/domain/audit/MyCustomRevisionListener.java
SpringJava/JPA/spring-jpa-persistence/src/main/resources/spring-configuration/configuration.xml
SpringJava/JPA/spring-jpa-persistence/src/main/resources/spring-configuration/liquibase/ddlChangelog.xml
SpringJava/JPA/spring-jpa-services/src/main/resources/spring-configuration/configuration.xml

index 0cdeef2..a5d1208 100644 (file)
                        <version>1.7.21</version>
                </dependency>
                
+               
+               <dependency>
+                       <groupId>org.springframework</groupId>
+                       <artifactId>spring-context</artifactId>
+                       <version>${spring.version}</version>
+                       <!--
+                               Required dependency for getting rid of commons logging and use my 
+                               own logging library (in my case I decided to use log4j 2 under slf4j)
+                       -->
+                       <exclusions>
+                               <exclusion>
+                                       <groupId>commons-logging</groupId>
+                                       <artifactId>commons-logging</artifactId>
+                               </exclusion>
+                       </exclusions>
+               </dependency>
+               
                <dependency>
                        <groupId>javax.inject</groupId>
                        <artifactId>javax.inject</artifactId>
        </dependencies>
        <dependencyManagement>
                <dependencies>
-                       <dependency>
-                               <groupId>org.springframework</groupId>
-                               <artifactId>spring-context</artifactId>
-                               <version>${spring.version}</version>
-                               <!--
-                                       Required dependency for getting rid of commons logging and use my 
-                                       own logging library (in my case I decided to use log4j 2 under slf4j)
-                               -->
-                               <exclusions>
-                                       <exclusion>
-                                               <groupId>commons-logging</groupId>
-                                               <artifactId>commons-logging</artifactId>
-                                       </exclusion>
-                               </exclusions>
-                       </dependency>
                        
                        <!-- REST API -->
                        <dependency>
                        <dependency>
                                <groupId>com.fasterxml.jackson.core</groupId>
                                <artifactId>jackson-databind</artifactId>
-                               <version>2.6.4</version>
+                               <version>2.8.1</version>
                        </dependency>
 
 
                <dependency>
                        <groupId>com.fasterxml.jackson.core</groupId>
                        <artifactId>jackson-databind</artifactId>
-                       <version>2.6.4</version>
+                       <version>2.8.1</version>
                </dependency>
                -->
                <!-- 
                <dependency>
                <groupId>com.fasterxml.jackson.datatype</groupId>
                <artifactId>jackson-datatype-jsr310</artifactId>
-               <version>2.8.0.rc2</version>
+               <version>2.8.1</version>
                </dependency>
 
 
diff --git a/SpringJava/JPA/spring-jpa-persistence/src/main/java/de/spring/example/context/StaticContextHolder.java b/SpringJava/JPA/spring-jpa-persistence/src/main/java/de/spring/example/context/StaticContextHolder.java
new file mode 100644 (file)
index 0000000..b117739
--- /dev/null
@@ -0,0 +1,39 @@
+package de.spring.example.context;
+
+import javax.inject.Named;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.BeanFactory;
+import org.springframework.beans.factory.BeanFactoryAware;
+
+/**
+ * JPA Entity classes do not allow you to inject beans.
+ * I tried to use this class for injecting beans in them but it did not either work :(
+ * No way of injecting beans in JPA Entity classes :( 
+ */
+@Named("staticContextHolder")
+public class StaticContextHolder implements BeanFactoryAware {
+       private static final Logger LOGGER = LoggerFactory.getLogger(StaticContextHolder.class);
+       
+       private static BeanFactory CONTEXT;
+       
+       public StaticContextHolder() {
+               
+       }
+       
+       public static Object getBean(String bean) {
+               return CONTEXT.getBean(bean);
+       }
+       
+       @Override
+       public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
+               if (CONTEXT != null) {
+                       LOGGER.warn("CONTEXT is not null!!!");
+               }
+
+               CONTEXT = beanFactory;
+       }
+
+}
index 4d698ee..19e3ec0 100644 (file)
@@ -1,26 +1,28 @@
 package de.spring.example.context;
 
-import javax.inject.Named;
-
 import org.springframework.util.Assert;
 
-@Named("userNameThreadContext")
+/**
+ * I had to implement this class in a static way because JPA Entity objects do not allow you
+ * to inject beans. StaticContextHolder did not either work :(
+ * No way of injecting beans in JPA Entity classes :( 
+ */
 public class UsernameThreadContext {
        public static final String USERNAME_HEADER = "USERNAME";
        
-       private final ThreadLocal<String> contextHolder = new ThreadLocal<>();
+       private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();
        
-       public void setUsername(String username) {
+       public static final void setUsername(String username) {
                Assert.notNull(username);
                
                contextHolder.set(username);
        }
        
-       public String getUsername() {
+       public static final String getUsername() {
                return contextHolder.get();
        }
        
-       public void clearUsername() {
+       public static final void clearUsername() {
                contextHolder.remove();
        }
 }
diff --git a/SpringJava/JPA/spring-jpa-persistence/src/main/java/de/spring/example/persistence/converters/LocalDateTimeAttributeConverter.java b/SpringJava/JPA/spring-jpa-persistence/src/main/java/de/spring/example/persistence/converters/LocalDateTimeAttributeConverter.java
new file mode 100644 (file)
index 0000000..3dc89a8
--- /dev/null
@@ -0,0 +1,34 @@
+package de.spring.example.persistence.converters;
+
+import java.sql.Timestamp;
+import java.time.LocalDateTime;
+
+import javax.persistence.AttributeConverter;
+import javax.persistence.Converter;
+
+@Converter(autoApply = true)
+public class LocalDateTimeAttributeConverter implements AttributeConverter<LocalDateTime, Timestamp> {
+
+       @Override
+       public Timestamp convertToDatabaseColumn(LocalDateTime localDateTime) {
+               Timestamp timestamp = null;
+               
+               if (localDateTime != null) {
+                       timestamp = Timestamp.valueOf(localDateTime);
+               }
+               
+               return timestamp;
+       }
+
+       @Override
+       public LocalDateTime convertToEntityAttribute(Timestamp sqlTimestamp) {
+               LocalDateTime localDateTime = null;
+               
+               if (sqlTimestamp != null) {
+                       localDateTime = sqlTimestamp.toLocalDateTime();
+               }
+               
+               return localDateTime;
+       }
+
+}
index acd46aa..fbdc708 100644 (file)
@@ -1,15 +1,10 @@
 package de.spring.example.persistence.domain.audit;
 
-import javax.inject.Inject;
-
 import org.hibernate.envers.RevisionListener;
 
 import de.spring.example.context.UsernameThreadContext;
 
-public class MyCustomRevisionListener implements RevisionListener {
-       @Inject
-       private UsernameThreadContext userNameThreadContext;
-       
+public class MyCustomRevisionListener implements RevisionListener {    
        
        protected MyCustomRevisionListener() {
                
@@ -25,7 +20,7 @@ public class MyCustomRevisionListener implements RevisionListener {
        }
        
        private String getSafeUsername() {
-               String userName = userNameThreadContext.getUsername();
+               String userName = UsernameThreadContext.getUsername();
                
                if (userName == null) {
                        userName = "NO_USER";
index 5609f29..5e4c00d 100644 (file)
@@ -8,6 +8,8 @@
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd">
         
+    <context:annotation-config />
+    
        <context:component-scan base-package="de.spring.example.context"/>
        
        <context:property-placeholder location="classpath:jpa.properties" />
index c903458..e2f5eb9 100644 (file)
     </changeSet>
     <changeSet author="gustavo (generated)" id="1469119656864-5" objectQuotingStrategy="QUOTE_ALL_OBJECTS">
         <createTable tableName="CUSTOM_REVISION">
-            <column name="ID" type="BIGINT">
-                <constraints nullable="false"/>
+            <column autoIncrement="true" name="ID" type="BIGINT">
+                <constraints primaryKey="true"/>
             </column>
-            <column name="REVISION_DATE" type="BIGINT">
+            <column name="REVISION_DATE" type="TIMESTAMP">
                 <constraints nullable="false"/>
             </column>
             <column name="USERNAME" type="VARCHAR(255)"/>
     <changeSet author="gustavo (generated)" id="1469119656864-8" objectQuotingStrategy="QUOTE_ALL_OBJECTS">
         <addPrimaryKey columnNames="ID, REVISION" constraintName="PRIMARY" tableName="AD_DESCRIPTION_AUDITED"/>
     </changeSet>
-    <changeSet author="gustavo (generated)" id="1469119656864-9" objectQuotingStrategy="QUOTE_ALL_OBJECTS">
-        <addPrimaryKey columnNames="ID" constraintName="PRIMARY" tableName="CUSTOM_REVISION"/>
-    </changeSet>
     <changeSet author="gustavo (generated)" id="1469119656864-10" objectQuotingStrategy="QUOTE_ALL_OBJECTS">
         <createIndex indexName="FK5kfkyp713lqaiybgbyjpjibgh" tableName="AD_DESCRIPTION">
             <column name="AD_ID"/>
index 05346ba..82009fb 100644 (file)
@@ -8,6 +8,8 @@
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd">
        
+       <context:annotation-config />
+       
        <context:component-scan base-package="de.spring.example.services, org.resthub.common.service"/>
        
 </beans>
\ No newline at end of file