JPA 2 Tutorial – Many To Many with Self

selfWe have just covered the subject of Many-To-Many relationships, which is a fun topic for the hole family! Now there are some particular cases on the Many-to-many front which are not particularly hard, but can lead to a bit of confusion. One of those particular cases is the M2M with same table.

A reader asked : “How would one create manyToMany relationship with the same entity. Entity user can have friends (user), and can be friend to other users. With note that junction table has to have some additional columns besides user keys. How can that be achieved?”  (Thanks Felix for the feedback and question!)

Read more of this post

Advertisements

JPA 2 Tutorial – Relationships – Many To Many

So, we have already dabbled into Relationships in JPA, in particular One-To-Many and Many-To-One. It is now time to give a look into Many-To-Many, which is very similar to One-To-Many Relationships. So at this point you should have read the previous two tutorials on JPA 2, but if for some reason you don’t know what I’m talking about, give a look at these two links, they will help quite a bit:

Read more of this post

Apache POI, translating Excel column number to letter

Recently I had to write some code to generate excel files using Apache POI. One of the files was fairly complex and used formulas in a few cells.

I found myself spending a few hours searching for a way to translate the indexed column numbers that POI uses to the column letters I needed in those formulas.

It turns out the solution is quite simple, you just have to use org.apache.poi.hssf.util.CellReference:

String columnLetter = CellReference.convertNumToColString(columnNumber);

Enabling remote JMX on Glassfish 2.1.1

The default glassfish app. server installation comes with local JMX connections enabled by default.

To enable JMX connections from remote hosts all that’s needed is to add -Djava.rmi.server.hostname=<hostname> in the JVM Options of the instance of GlassFish, where <hostname> is the external hostname of the server where glassfish is running.

Java Threading and Concurrency Introduction

Multi Threading is one of the most interesting subjects when developing applications, be them in Java or any other Language: The ability to have ones software performing multiple tasks at the same time, maybe handling multiple requests from users, parallel processing things… wicked stuff really!

On the other hand… when things go wrong with multi threading, they go wrong really fast, they go wrong all over the place, it is hard to debug (specially if threads “share” stuff) and you get the usual comment: “I just don’t get it why this is doing this”. Multi Threading is not without challenges!

In this Tutorial we’ll be going through a bit of theory regarding threads and the Java memory model, but I’ll try to make it as interesting as I can with examples and demonstration of concepts. We’ll see how stuff can go wrong, how to avoid those situations, and we’ll play with threads just to see them working! A more advanced tutorial will follow(at some point) in which I’ll discuss more advanced parts of Threads and Concurrency.

Read more of this post

JPA 2 Tutorial – Relationships – One To Many

Following from the previous tutorial we now dive into move interesting and complex parts of JPA 2: Relationships! In this tutorial we’ll be focusing on One-to-Many relationships (mainly Bidirectional), looking into their particularities (specially related to the Cascading of operations), we’ll create a JUnit Test class for testing the Relationship, look at what actual SQL queries that are performed, learn by example and also some more interesting theory that is related to all of this. Read more of this post

Getting Started with JPA 2 Tutorial

So, one of the most common comments I get to hear while at work almost every week regarding JPA is : “Oh I Just hate JPA!” . I never really had much of a problem with it, obviously sometimes with JPA something just doesn’t want to work for some strange reason and the Logs don’t really help finding the problem but most of the days everything works fine.

At some point I decided that I would write a few tutorials on JPA. Some time has passed since and now I’m actually doing it. Use the comments section for corrections if you find any problems or errors, or if you would just like to leave a kind word!

Read more of this post