<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>
--- /dev/null
+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;
+ }
+
+}
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();
}
}
--- /dev/null
+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;
+ }
+
+}
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() {
}
private String getSafeUsername() {
- String userName = userNameThreadContext.getUsername();
+ String userName = UsernameThreadContext.getUsername();
if (userName == null) {
userName = "NO_USER";
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" />
</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"/>
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