2

I plan to move form PHP to Java writing data-driven web apps. I obviously want to have a layer handling persistent data. In PHP with Doctrine (1.x) the following things can be done thru a single interface (PHP's ArrayAccess):

  • Representing data structures in code
  • Getting structured data from the database thru Doctrine
  • Representing structured data in an HTML form

So it is essential that I can have a layer for forms like:

$properties = array (
    "minlength" => 2,
    "maxlength" => 30,
);
new TextInput ("name", $properties);

... which is oblivious about the underlaying mechanics. It can load and save (possibly structured) data from all the sources above thru a single interface.

When saving data to a record it can not call setName($value). It can only call set("name", $value). (Of course it could be done thru reflection, but I hope I don't have to elaborate on why it's a bad idea).

So is there any ORM in Java which:

  • Implements the native collection interfaces. java.util.Map for example.
  • Maps DB relations as collections like author.get("books").put(newBook)
  • Has the right triggers to implement complex logic (like permissions or external files attached to fields).

3 Answers 3

2

Map access for POJO classess can be achieved thru a superclass implementing Map thru Hibernate's ClassMetadata interface like:

abstract class MappedRecord implements java.util.Map<String, Object> {

    private ClassMetadata classMeta;

    public MappedRecord() {
        classMeta = mySessionFactory.getClassMetadata(this.getClass());
    }

    public Object put(String s, Object o) {
        classMeta.setPropertyValue(this, s, o, EntityMode.POJO);
    }
}

Then when you extend MappedRecord in your persistent classes, you can call:

User u = new User();
u.put("name", "John");

Safely getting mySessionFactory is a tricky question though;

Sign up to request clarification or add additional context in comments.

Comments

1

You may want to have a look into Hibernate and JPA

3 Comments

Thanks, I checked Hibernate, but it seems to me that you can not access records as java.util.Map where fields are the keys. It would be essential to write a generalized form layer. I don't want to hardcode anything like setName() at this point.
Can't you use Hibernate (or any JPA implementation) to get Maps as results? I thought, such a mapping is possible, but I don't remember how. In any case, your use case is interesting. I will add it to my own persistence abstraction library jooq.org. Unfortunately, that library will not suit your needs...
After giving it another try I have found a way to dynamically set and get field values. There are still some question marks but I will post a rudamentary solution. - Yep I've already checked your jooq.org from your profile page :)
1

I think NHibernate is the choice, but I'm not sure I got your requirement about triggers. I think, it's a bit application layer, not ORM layer.

4 Comments

I want prmissions to be at the very heart of it, with no way of accessing anything without the correct credentials. I think it is more logical and less error-prone than the using application layer.
@vbence I don't see how permissions and triggers collide. Triggers are low-level at the database and don't really think anyone will implement security level using triggers. It's really low level and working with roles and permissions system on this level will quicky get you out of possibilities.
When I use "triggers" in the context of a Java active record, I don't mean triggers of the DBMS, I just use them as a synonim for "hooks" or "callbacks" or "event handlers". A piece of code where I can check permissions and throw an exception if something is not right. Like Doctrine's preUpdate().
Well, sure Nhibernate has these just like Doctrine does.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.