Simple no-nonsense persistence library

Hi all,

Based on a discussion here I thought I would post my ORM library for Java. Have a look at Persism. http://persism.sourceforge.net

Persism is a simple no-nonsense, auto-configuration, auto-discovery, convention over configuration Object Relational Mapping library. Persism uses POJOs for data objects and does not need any XML for configuration. Annotations are only used as a fall-back in cases where the library can’t discover the mapping on it’s own. Persism is pretty smart so you usually don’t need to bother with annotations at all.

Persism has no external dependencies however it will use logging if available and it’s only about 40k.

Persism has only 2 classes: Query for reading data and Command for updates/inserts and deletes.

The library works around the Connection object. Example of a query:


Query query = new Query(connection);
List<Player> list = query.readList(Player.class,"select * from PLAYERS where name = ?", "Fred");

// or a single object
Player fred = query.read(Player.class,"select * from PLAYERS where name = ?", "Fred");

// or even simpler
Player fred = new Player();
fred.setPlayerId(123); // set the primary key
query.read(fred); // finds Fred in the database by primary key

Notice that the class is Player and the table is PLAYERS. Persism understands that and maps this for you without annotations. Heck, the library will also map things like Category -> CATEGORIES and PlayerAttributes - > [Player Attributes]. Plural table names and tables with spaces in the names would all map fine without annotating anything.

Command is similar:


Command command = new Command(connection);

Player player = new Player();
player.setPlayerId(123);
player.setPlayerName("Fred");
player.setEmailAddress("x@y.com");

command.insert(player );

player.setPlayerName("Barney");
command.update(player );

command.delete(player);

I’ve been using this library myself in various production applications. I marked it beta for now because I need some feedback on it before I finalize the API and also I added support for several different databases.

Current databases supported:
MSSQL (JTDS and MSSQL driver)
Oracle
MySQL
Derby
H2
SQLite

Thanks everyone! Let me know what you think!

how do you handle the n+1 select problem?

Hi, good question. :slight_smile:

Simple. Persism doesn’t directly support the idea of having hierarchical objects. It’s up to you if want to do that. Persism uses SQL directly so you can do it yourself. The library doesn’t impose the methodology.

Say you have Player


class Player {
  int id;
  // etc...

  @NotMapped
  List<Attribute> attributes;
 
  // getters/setters

}

Then you could do:


Player player = query.read(Player.class,"select * from players where id=?",123);
player.setAttributes( query.readList(Attribute.class,"select * from Attributes where Player_id=?",123) );

This is effectively what Hibernate does. The difference here is you’re in control on how you want to do it.

Often if you want to join 2 sets of data together you might want to write a class with the specific values you want and then use joins instead of querying 2 separate times.


@QueryResult
class PlayerAttributes {
   int id;
   int strength;
   int dexterity;

  // etc...
   
}

then the query would be


sql = "select player.id, attributes.strength, attributes.dexterity from players p join attributes a on p.id = a.player_id where player.id = ?";
List<PlayerAttributes> list = query.readList(PlayerAttributes.class, sql, 123);

There actually is no n+1 select “problem”. It’s a problem only in as much as most ORMs have query languages that can’t really optimize the set oriented nature of relational databases. Persism uses SQL directly.

Cool that there is H2 support in there. If PostgreSQL can be added then it is pretty much solid for me :slight_smile: I guess you have to deal with primary key generation yourself?

the pluralization mapping support reminds me a bit of Ruby On Rails’ ActiveRecord.

Sorry for the delay.

Yeah I could test PostgreSQL. If you download it and try it - let me know how it goes. I’ll add PostgreSQL to my list.

If you create a table like this:

CREATE TABLE PLAYERS ( 
 ID IDENTITY, 
 NAME VARCHAR(30) NULL )  -- etc...

Then you have a class

public class Player {

    private int id;
    private String name;
 // setters and getters
}

You don’t have to specify anything in the class. In most cases Persism detects that ID is autoinc (and therefore primary) even if you don’t specify that it’s a primary in the table create. The exception so far is Oracle which needs the annotation. I can dig into Oracle more deeply and see if I can prevent that in the future. The way Persism works is that it uses annotations only as a fall-back. You Normally don’t need to specify anything special - simple POJOs are usually sufficient.

That’s the goal I’d like to achieve.

I just installed PostgreSQL and my basic tests all pass fine. So thank’s I’ll add PostgreSQL to my supported databases and include new tests for it in my next build. :slight_smile: