JPA default entity listener

The problem Sometimes we want to register a default Entity Listeners which are called as soon an entity is persisted, without adding the @EntityListeners annotation to our classes, as in most cases we anyway provide interfaces so we can access data on the entity itself. The problem is, that we cannot annotate our entity listener with…

Android Fragments and Transactions

Problem Sometimes it is confusing how fragments work in Android and interesting side effects like: Why have Fragments empty constructors? Why shouldn’t I pass state variables like they are? Why do I need this Fragment transactions? What does addToBackStack mean and do I need it? Why is sometimes the Activity null in my fragment? Overview…

Java SSLContext and the SSLSocketFactory self-signed certificate

Problem Often we want to connect to create a secure SSL connection to an HTTPs endpoint which is secured by a self-signed Certificate. If we do so just with a simple call we usually face an nice exception like: Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested…

Spring Data MongoDB and JSR Validation using Lazy DBRef

The Problem Recently using MongoDB and Spring Data I encountered an interesting issue. Saving an Entity  which was loaded by @DBRef(lazy = true) wasn’t possible together with the activated ValidatingMongoEventListener. Each time the following error occurred: javax.validation.ConstraintViolationException at org.springframework.data.mongodb.core.mapping.event.ValidatingMongoEventListener.onBeforeSave(ValidatingMongoEventListener.java:65) at org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener.onBeforeSave(AbstractMongoEventListener.java:144) at org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener.onApplicationEvent(AbstractMongoEventListener.java:90) at org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener.onApplicationEvent(AbstractMongoEventListener.java:33) at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:163) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:136) at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:381) at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:335) at org.springframework.data.mongodb.core.MongoTemplate.maybeEmitEvent(MongoTemplate.java:1633) at org.springframework.data.mongodb.core.MongoTemplate.doSave(MongoTemplate.java:986) at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:933)…

Serialization Jackson vs Google Protocol Buffers

Where are many articles around comparing the serialization between different frameworks and of course different encodings. I came around recently the problem that I needed a very fast serialization for pretty simple objects but between different platforms (embedded device with C, Android Java, iOS Objective-C and Java Backend). Well first reaction, let us just take…

Use Session and Request Scope Beans in Tests with Spring

Sometimes it is useful haven the Web Scopes in Unit Tests. Usually Spring just show this very useful error in the Unit Test: Caused by: java.lang.IllegalStateException: No Scope registered for scope ’session‘ at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:334) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1120) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1044) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533) … 28 common frames omitted You can easily have all needed…

Spring Boot Configuration – Properties not loaded

I had recently a very interesting problem using Spring Boot and the automagic concerning the configuration. The Problem org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‚demo.MyApplicationTests‘: Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String demo.MyApplicationTests.url; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder ‚my.url‘ in string value „${my.url}“ at…

Strong IDs

In some projects you may encounter the so called „strong id pattern“. What is it? Strong IDs are simply wrapped primitive or simple types into classes. Just remember what we did in Java before the Enum come around. We had just Strings or Numbers in the code which we used to assign well known values…

JSR 303 Validation for Dates

A common case is to validate date inputs in Java e.g. to avoid the nice and friendly typos like 06/01/1500. Nevertheless by default where is not to much support to handle this very common case. So let’s fix this together: Annotation first The first we need is a JSR annotation which we use to annotate…