3

I have been using Hibernate-Tools and JPA tools in Eclipse to generate JPA entity classes from an existing database.

The problem is that this process is really complex to set up and has many small "bugs" especially if you want a specific schema from the database and basically all the steps need to be followed exactly to generate those classes, so I'm exploring other alternatives.

No restriction if they are IDE's, online apps, standalone apps, just to be free of charge.

0

I've been using OpenJPA via Gradle build script like this and have been pretty happy with it:

build.gradle.kts

dependencies {
    ...
    val jpa = configurations.create("jpa")
    jpa("org.apache.openjpa:openjpa-all:2.3.0")
    jpa("org.postgresql:postgresql")
}

tasks {
    register<JavaExec>("genSchema") {
        classpath = configurations["jpa"]
        main = "org.apache.openjpa.jdbc.schema.SchemaTool"
        args = mutableListOf(
                "-properties", "config/openjpa.xml",
                "-action", "reflect",
                "-file", "build/schema.xml"
        )
    }

    register<JavaExec>("genEntity") {
        dependsOn("genSchema")
        classpath = configurations["jpa"]
        main = "org.apache.openjpa.jdbc.meta.ReverseMappingTool"
        args = mutableListOf(
                "-properties", "config/openjpa.xml",
                "-metadata", "none",
                "-annotations", "true",
                "-nullableAsObject", "true",
                "-useGenericCollections", "true",
                "-directory", "src/main/java/${project.group}/${project.name}/entity",
                "-pkg", "${project.group}.${project.name}.entity",
                "build/schema.xml"
        )
    }
}

Then you can just run ./gradlew genEntity anytime you need to generate the entity classes.

Some notes on using this:

  • Here I'm generating the classes directly into src. A better practice would be to generate somewhere else and manually copy the files over.

  • This requires a config/openjpa.xml file to tell the script what are the parameters to connect to the database. You can use this one as example: https://gist.github.com/rafaeltuelho/39c98965954859b43733

  • Process is almost automated, so once you have the config/openjpa.xml there is no manual configuration involved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.