Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Simple no-nonsense persistence library  (Read 1690 times)
0 Members and 1 Guest are viewing this topic.
Offline sproket

Junior Member


Medals: 1



« Posted 2012-06-17 02:43:45 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
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!

Offline OttoMeier

Senior Member


Medals: 4
Projects: 1



« Reply #1 - Posted 2012-06-17 22:49:54 »

how do you handle the n+1 select problem?
Offline sproket

Junior Member


Medals: 1



« Reply #2 - Posted 2012-06-18 02:29:21 »

Hi, good question.  Smiley

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
class Player {
  int id;
  // etc...

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

}


Then you could do:

1  
2  
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.


1  
2  
3  
4  
5  
6  
7  
8  
9  
@QueryResult
class PlayerAttributes {
   int id;
   int strength;
   int dexterity;

  // etc...
 
}


then the query would be
1  
2  
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.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline gimbal

JGO Knight


Medals: 25



« Reply #3 - Posted 2012-06-20 13:19:25 »

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

the pluralization mapping support reminds me a bit of Ruby On Rails' ActiveRecord.
Offline sproket

Junior Member


Medals: 1



« Reply #4 - Posted 2012-06-21 02:14:20 »

Sorry for the delay.

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

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

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:

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


Then you have a class
1  
2  
3  
4  
5  
6  
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.





Offline sproket

Junior Member


Medals: 1



« Reply #5 - Posted 2012-06-21 21:09:54 »

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.  Smiley

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (18 views)
2014-07-24 01:59:36

Riven (17 views)
2014-07-23 21:16:32

Riven (14 views)
2014-07-23 21:07:15

Riven (17 views)
2014-07-23 20:56:16

ctomni231 (45 views)
2014-07-18 06:55:21

Zero Volt (40 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (36 views)
2014-07-16 23:30:00

Cero (51 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!