domaframework / doma

DAO oriented database mapping framework for Java
https://doma.readthedocs.io/
Apache License 2.0
447 stars 70 forks source link

Use JPA annotations or maybe generate POJOs from database #315

Closed agentgt closed 5 years ago

agentgt commented 5 years ago

I really like the idea of Doma. Its like almost the perfect ORM that I have always wanted particularly the testable bi-directional SQL templates. In fact I had wanted this so bad I had partly implemented my own very incomplete parser here a long time ago: https://github.com/agentgt/jirm/tree/master/jirm-core

However there are couple of reasons why we don't use it:

  1. The library doesn't generate POJOs from the database like jOOQ does
  2. We use JPA annotations and so does the jOOQ generator
  3. The annotation processor is not separated from the implementation

I know doma does not want a dependency on JPA annotations but it doesn't have to be a required dependency. There are a couple of tricks to make this possible but mainly it involves the fact that annotation processing doesn't actually need the annotation libraries.

Speaking of which I noticed you do a lot of manual annotation parsing here: https://github.com/domaframework/doma/tree/master/src/main/java/org/seasar/doma/internal/apt/annot

There is actually a preprocessing library that will generate all that for you called hickory. Unfortunately Hickory's documentation is very hard to find because while it is very mature library it has been semi abandoned. The documentation is here: https://www.mvndoc.com/c/com.jolira/hickory/index.html

In your pom you would put:

    <dependency>
      <groupId>com.jolira</groupId>
      <artifactId>hickory</artifactId>
      <version>1.0.0</version>
      <scope>provided</scope>
      <optional>true</optional>
    </dependency>

    <dependency>
      <groupId>org.hibernate.javax.persistence</groupId>
      <artifactId>hibernate-jpa-2.1-api</artifactId>
      <version>1.0.0.Final</version>
      <scope>provided</scope>
      <optional>true</optional>
    </dependency>

Then you just make a dummy class or use package-info and do something like:

package org.seasar.doma.internal.apt.annot.jpa;

import javax.persistence.Column;

import net.java.dev.hickory.prism.GeneratePrism;
import net.java.dev.hickory.prism.GeneratePrisms;

@GeneratePrisms({
    @GeneratePrism(value = Column.class, publicAccess = true),
        // Additional JPA annotations here besides Column
})
public class PrismGenerator {

}

Hickory will then generate a class like this: https://gist.github.com/agentgt/a3c8dc50ce992ee375d9e5a358172417

(Notice how there isn't a runtime processing dependency on the annotation. The JPA dependency is only for Hickory to generate the prism. Yes you can run annotation preprocessing on an annotation preprocessing library but there are some caveats (namely generating the service loader file). Alternatively you can just run the generator once and copy the generated code since JPA annotations never really change).

Anyway I realize this a lot of work to ask so I may fork the project to try it. The idea is we would continue to use the jOOQ Table -> POJO generator (a maven plugin does it before annotation preprocessing) and then have doma manage the CRUD of those beans leveraging the JPA annotations to build its internal model.

Thoughts? BTW let me know if you want me to translate this into Chinese if that helps as my business partner can probably do that and I'm sure I'm not being entirely clear or easy to follow.

nakamura-to commented 5 years ago

Thanks for your comment. I'm glad to know JIRM and Hickory.

If you can accept Doma annotations, I think there are other ways.

The first way is to use domaframework/doma-gen. Doma-Gen generates POJOs for Doma.

The second way is to customize the jOOQ generator to use Doma annotations. I think you can override the following methods: https://github.com/jOOQ/jOOQ/blob/version-3.11.0-branch/jOOQ-codegen/src/main/java/org/jooq/codegen/JavaGenerator.java#L4716 https://github.com/jOOQ/jOOQ/blob/version-3.11.0-branch/jOOQ-codegen/src/main/java/org/jooq/codegen/JavaGenerator.java#L4816

The annotation processor is not separated from the implementation

How important is this for you? let me know if you have any trouble.

agentgt commented 5 years ago

Oh I saw doma-gen and thought it was an old apt processor.

I didn’t see doma-gen mentioned in the Doma2 doc but this fixes most of the issues I have.

The JPA annotation stuff was more of a nice to have and not that important.