开发者

Doctrine-like array access for Java

开发者 https://www.devze.com 2023-02-25 08:24 出处:网络
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 inter

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 sou开发者_StackOverflow中文版rces 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).


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;


You may want to have a look into Hibernate and JPA


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消