tag:blogger.com,1999:blog-64747775151498718072024-03-10T09:50:08.067+01:00Warlock's Thoughtsvarð-lokkur (warlock)http://www.blogger.com/profile/08196109352954198217noreply@blogger.comBlogger46125tag:blogger.com,1999:blog-6474777515149871807.post-6376080326469750502019-10-15T18:17:00.000+02:002019-11-01T13:42:00.000+01:00Spring MVC - HTTP message converterQuite often you need to provide users with the same data, but in different forms, like JSON, PDF, XLS, etc. If your application is Spring Framework based, this task can be achieved using HTTP message converters.
HTTP message converters are applied when HTTP request (or its parts) needs to be converted into type required for handler method argument (see: Handler methods - method arguments), or varð-lokkur (warlock)http://www.blogger.com/profile/08196109352954198217noreply@blogger.com1tag:blogger.com,1999:blog-6474777515149871807.post-37739996943228311722019-08-03T23:25:00.000+02:002019-10-12T07:54:04.412+02:00JDBC - Emulating a sequence Probably each of us encountered this problem at least once in the programmer's life - how to emulate a database sequence? Below you may find my variation of this problem's solution.
Suppose that we have an interface defining the desired API for returning a sequence of integer numbers:
public interface Sequences {
int nextValue(String sequenceName) throws SQLException;
}
and the varð-lokkur (warlock)http://www.blogger.com/profile/08196109352954198217noreply@blogger.com3tag:blogger.com,1999:blog-6474777515149871807.post-84732752133624934152014-06-15T18:00:00.000+02:002014-06-15T18:09:45.762+02:00Serialization Proxy Pattern exampleThere are books, which change your life immensely. One of such books is "Effective Java" by Joshua Bloch. Below you may find small experiment, which was inspired by Chapter 11 of this book - "Serialization".
Suppose that we have a class designed for inheritance, which is not Serializable itself, and has no parameterless constructor, like in this example:
public class CumbersomePoint {
varð-lokkur (warlock)http://www.blogger.com/profile/08196109352954198217noreply@blogger.com0tag:blogger.com,1999:blog-6474777515149871807.post-37057865983298517302014-05-04T17:30:00.000+02:002014-05-04T17:46:02.718+02:00@OneToOne with shared primary key, revisited :)Long time ago, I wrote a post @OneToOne with shared primary key. Today I would like to return to this problem, with solution based on @MapsId annotation introduced in JPA 2.0
Again we have two entities: Primus and Secundus. Both entities have primary key using Long Java type. They are related 1-1, and Secundus should use the same primary key as Primus.
3 Years after my initial post they will varð-lokkur (warlock)http://www.blogger.com/profile/08196109352954198217noreply@blogger.com13tag:blogger.com,1999:blog-6474777515149871807.post-51508558023367290282013-05-11T07:00:00.000+02:002013-05-19T07:58:18.303+02:00JPA - Querydsl ProjectionsIn my last post: JPA - Basic Projections - I've mentioned about two basic possibilities of building JPA Projections. This post brings you more examples, this time based on Querydsl framework. Note, that I'm referring Querydsl version 3.1.1 here.
Reinvented constructor expressions
Take a look at the following code:
The above Querydsl construction means: create new JPQL query [1] [2], using varð-lokkur (warlock)http://www.blogger.com/profile/08196109352954198217noreply@blogger.com0tag:blogger.com,1999:blog-6474777515149871807.post-87053851779529872832013-05-04T08:00:00.000+02:002013-05-19T08:03:43.811+02:00JPA - Basic ProjectionsIn my last post: JPA - Should I become a laziness extremist? - I mentioned about the possibilities of improving JPA usage - one of them is using Projections.
Projection is a subset of entities' properties. It can be represented as dedicated class (or classes), and mapped either as the database view based entity, or using constructor expressions [1][2]. The clue of this solution is having very varð-lokkur (warlock)http://www.blogger.com/profile/08196109352954198217noreply@blogger.com0tag:blogger.com,1999:blog-6474777515149871807.post-38672778416613008662013-04-27T08:00:00.000+02:002013-05-04T08:09:37.919+02:00JPA - Should I become a laziness extremist?When you spoke with the Developers about mapping objects to relational databases, they very often complain about poor JPA performance, unpredictable behavior of JPA Providers, etc. Usually at some point of the conversation you will hear: "Let's drop this technology at all, we've seen something much better on the conference last month. We will use it in our projects instead of JPA and develop themvarð-lokkur (warlock)http://www.blogger.com/profile/08196109352954198217noreply@blogger.com0tag:blogger.com,1999:blog-6474777515149871807.post-2780281061945980292013-04-06T14:30:00.000+02:002013-04-27T07:57:33.739+02:00JPA - Hibernate - Type mapping on package levelWhen we are finally mature enough to use some custom types mapping in JPA, we usually stuck with some provider specific solution, because JPA itself doesn't define any mechanism for doing it. Let me show you an example of custom type mapping definition for one of the JPA providers - Hibernate.
Suppose that we use Joda Money in our project, and have an entity with property having type Money. varð-lokkur (warlock)http://www.blogger.com/profile/08196109352954198217noreply@blogger.com0tag:blogger.com,1999:blog-6474777515149871807.post-24673096539579504972012-12-02T15:00:00.000+01:002013-05-19T08:09:08.249+02:00Spring's Web MVC - Redirect to the Memory LeakThey say that one rock can cause an avalanche. Lately, one of my Colleagues, Marcin Radoszewski, gave me such a rock. You'll probably never guess what it is, but there is a chance, that you use it in many of your Web Applications. Allow me to introduce this rock to you :)
You probably well know redirect after post pattern. Using Spring Framework you have few ways to implement it, let's focus on varð-lokkur (warlock)http://www.blogger.com/profile/08196109352954198217noreply@blogger.com3tag:blogger.com,1999:blog-6474777515149871807.post-89491923336036537022012-08-13T23:00:00.000+02:002012-10-28T11:02:07.840+01:00JPQL - pagination on Oracle Database with HibernateIn your daily work, you rely on many different libraries, trusting they will serve you well, being perfect piece of code ... do you? ... really?! Then it's time to realize that you are perfectly wrong :) Increasing complexity of code leads to new possibilities of making errors :) Many of them are lurking in the libraries used by you, even if they are used for years by thousands of developers.
varð-lokkur (warlock)http://www.blogger.com/profile/08196109352954198217noreply@blogger.com1tag:blogger.com,1999:blog-6474777515149871807.post-38630323921274964352012-03-19T21:00:00.008+01:002012-10-28T11:06:12.627+01:00Suppressing FindBugs warningsFew days ago I spoke with my Friend, Tomek, about suppressing FindBugs warnings. Here is brief summary of our conversation, which may be interesting for you.
There is one simple method for suppressing the FindBugs warnings - usage of edu.umd.cs.findbugs.annotations.SuppressWarnings annotation. Just add it in place where FindBugs reported problem, and use varð-lokkur (warlock)http://www.blogger.com/profile/08196109352954198217noreply@blogger.com2tag:blogger.com,1999:blog-6474777515149871807.post-16205125917682436772012-03-07T22:30:00.005+01:002012-10-28T11:16:20.389+01:00FindBugs and JSR-305Suppose that group of developers work in parallel on parts of big project - some developers are working on service implementation, while others are working on code using this service. Both groups agreed on service API, and started working separately, having in mind the API assumptions...
Do you think this story will have happy end? Well, ... - maybe :) - there are tools which can help achieve itvarð-lokkur (warlock)http://www.blogger.com/profile/08196109352954198217noreply@blogger.com3tag:blogger.com,1999:blog-6474777515149871807.post-7362949330071874822012-02-19T12:30:00.000+01:002012-02-19T12:48:47.945+01:00Spring MVC - Flash AttributesLatest Spring Framework incarnation (3.1) brought interesting feature called Flash Attributes. It is remedy for the problem mentioned a long time ago, in one of my posts: Spring MVC - Session Attributes handling. This problem can be described in few words: if we want to pass the attributes via redirect between two controllers, we cannot use request attributes (they will not survivevarð-lokkur (warlock)http://www.blogger.com/profile/08196109352954198217noreply@blogger.com9tag:blogger.com,1999:blog-6474777515149871807.post-21178598388896268602012-01-07T07:30:00.012+01:002012-01-07T07:40:18.655+01:00EasyB, Thucydides and Selenium 2 - another BDD exampleToday I'll present you quick example of usage for EasyB - BDD framework for Java :), accompanied by Thucydides - amazing tool for ATDD build on top of Selenium 2.
Suppose that you want to leave something for the humanity, after many sleepless nights you finally discover that it will be a movie database, even more: Internet Movie Database. Now, my Friend, you have two ways: you can varð-lokkur (warlock)http://www.blogger.com/profile/08196109352954198217noreply@blogger.com3tag:blogger.com,1999:blog-6474777515149871807.post-68879771329569644752011-09-13T22:30:00.004+02:002011-09-13T23:11:04.964+02:00Modular Web Application based on JBoss ModulesRecently I read Why there is no standard for developing real modular web applications? by Patroklos Papapetrou. Inspired by this article I decided to check JBoss Modules in action. This post describes my experiment step by step.
I've started with following goal in mind - create web application using some service defined by my own JBoss module. Service prepared by me was pretty simple, varð-lokkur (warlock)http://www.blogger.com/profile/08196109352954198217noreply@blogger.com5tag:blogger.com,1999:blog-6474777515149871807.post-67566195376027032502011-08-16T23:00:00.002+02:002011-08-16T23:23:39.157+02:00JBehave - quick BDD exampleAfter few last months of work on some projects with Spring MVC, Spring WebFlow, and JPA I had to do something else ;) - even for a while ;) - below you may find a results of my small experiments with Behavior Driven Development and JBehave :)
I started with Introducing BDD and What's in a Story? articles by Dan North, and Alex Soto blog entry about Spring Framework,varð-lokkur (warlock)http://www.blogger.com/profile/08196109352954198217noreply@blogger.com4tag:blogger.com,1999:blog-6474777515149871807.post-85252130731675764502011-06-25T23:00:00.003+02:002011-06-25T23:18:15.463+02:00JPQL and joinsHave you ever asked yourself if JPQL (Java Persistence Query Language) queries written by you REALLY do what you want, or you just prepare them, commit to the code repository, and forget about them with a little help from pizza and beer ;) ...
Let's take a look at very simple example, we have Person entity:
@Entity
@Table(name = "PERSONS")
public class Person implements Serializable {
varð-lokkur (warlock)http://www.blogger.com/profile/08196109352954198217noreply@blogger.com0tag:blogger.com,1999:blog-6474777515149871807.post-34329914504188479352011-06-13T11:45:00.004+02:002011-06-13T11:45:00.561+02:00Customize PMD in Eclipse with your own rulesPMD is very nice Java code scanner which helps you avoid potential programming problems. It can be easily extended to your needs, and this post will bring you simple example of custom PMD rules related to JPA's @Enumerated annotation usage.
Before you'll continue the reading, you should check one of my previous posts - JPA - @Enumerated default attribute. When you work with the group of varð-lokkur (warlock)http://www.blogger.com/profile/08196109352954198217noreply@blogger.com1tag:blogger.com,1999:blog-6474777515149871807.post-24282784307180590402011-05-29T11:00:00.016+02:002011-05-29T11:00:06.446+02:00EclipseLink - JPA Queries optimization - @JoinFetch, @BatchFetch and Query Hints@JoinFetch is very helpful when you try to minimize the number of SQL queries standing behind JPA query. I wrote about it in one of my previous posts: JPA Demystified (episode 1) - @OneToMany and @ManyToOne mappings, but it brings also some threats (see JPA - insert instead of update). This post will describe one of them, related to limiting number of results returned by JPA query.
varð-lokkur (warlock)http://www.blogger.com/profile/08196109352954198217noreply@blogger.com0tag:blogger.com,1999:blog-6474777515149871807.post-52655539764317876082011-05-04T17:49:00.000+02:002014-05-21T07:38:49.794+02:00@OneToOne with shared primary keyA Friend of mine asked me lately how would I define @OneToOne mapping in JPA with shared primary key. Well, we will definitely need an example ;) - Suppose that we have two entities: Primus and Secundus. Both entities have primary key using Long Java type. They are related 1-1, and Secundus should use the same primary key as Primus. Let's start with following state of Primus:
@Entity
@Table(name varð-lokkur (warlock)http://www.blogger.com/profile/08196109352954198217noreply@blogger.com4tag:blogger.com,1999:blog-6474777515149871807.post-23473959506558169872011-04-10T13:00:00.005+02:002011-04-10T13:50:04.152+02:00JPA and unmodifiable collectionsPatrycja Węgrzynowicz (see Yon Labs / Yon Consulting) had very interesting talk on 33rd Degree Conference this year called "Patterns and Anti-Patterns in Hibernate". Inspired by this talk, I decided to verify JPA providers behavior regarding unmodifiable Collections.
I will use the same Employer - Employee - Benefit model as in my previous JPA posts (ex. JPA varð-lokkur (warlock)http://www.blogger.com/profile/08196109352954198217noreply@blogger.com4tag:blogger.com,1999:blog-6474777515149871807.post-83527390284595924622011-04-02T08:00:00.010+02:002011-04-02T08:13:34.454+02:00NSObject - description methodI believe that all Java Developers, using debugger from time to time, are appreciating toString method in Object class :) - probably the same feelings share all Objective-C Developers who know NSObject's description method.
In short, this method returns a string that represents the content of the receiving class, by default it is something like this:
<Card: 0x4b64c90>Sounds mysterious for varð-lokkur (warlock)http://www.blogger.com/profile/08196109352954198217noreply@blogger.com1tag:blogger.com,1999:blog-6474777515149871807.post-40643912845355826202011-03-21T23:00:00.001+01:002011-03-21T23:02:49.569+01:00Useless Flow Managed PersistenceSpring Webflow has some interesting feature called Flow Managed Persistence. In short: it allows you to change some persistent entity during the flow, and merge the state of entity into persistence context at the end of the flow.
Sounds interesting, and is worth trying for sure :) - but you have to be aware, that Spring Webflow guys have overlooked something very important. The&varð-lokkur (warlock)http://www.blogger.com/profile/08196109352954198217noreply@blogger.com1tag:blogger.com,1999:blog-6474777515149871807.post-60369117525062464972011-03-05T09:00:00.003+01:002011-03-05T09:16:04.870+01:00Spring @Autowired, JUnit and MockitoFew days ago I've encountered interesting problem with autowiring Mockito based Spring Framework beans, let me share it with you :)
Everything started when I've made JUnit test for some business logic code.
...
import static junit.framework.Assert.assertNotNull;
...
@ContextConfiguration({ "classpath:.../business-logic.xml", "classpath:.../orm-jpa.xml",
"classpath:.../test/mockito.xml", ...varð-lokkur (warlock)http://www.blogger.com/profile/08196109352954198217noreply@blogger.com3tag:blogger.com,1999:blog-6474777515149871807.post-36855844931946942602011-02-20T09:00:00.011+01:002011-02-20T10:25:16.335+01:00Test me to the end of codeThose who know me may think I'm Javaholic - well, maybe a little ;) - but I do like other programming languages too :) - really :) - below you may find results of my small experiment with XCode 4, Objective-C categories and Unit Tests.
I've started with the idea of extending NSArray class with method returning an array with shuffled elements, which can be achieved using Objective-C categories [1varð-lokkur (warlock)http://www.blogger.com/profile/08196109352954198217noreply@blogger.com0