reverse engineering postgresql using hibernate tool in eclipse - hibernate-tools

I am trying to reverse engineer postgresql using hibernate tools in eclipse. It is not generating #Generated annotation. postgres tables have primary key defined with serial keyword. what can be the problem?

You should modify the freemarker templates, and add someting like :
#${pojo.importType("javax.annotation.Generated")}(value = "Generated by Hibernate Tools ${version}", date = "${.now?iso_local}")
For JPA entities, you could add it in pojo/PojoTypeDeclaration.ftl
It works with Hibernate Tools 5.2.3 and Freemarker 2.3.23.
You could also add block comments like :
// Generated ${date} by Hibernate Tools ${version}
// and FreeMarker ${.version}

Related

I need to generate Liquibase Change-set for my JPA entities. How can I do that?

I have tried the following
liquibase:generateChangeLog - It generated the change log from my db.I need to generate the change-log from my JPA entities.
liquibase:diff - It generates the change log for the difference between my db and JPA entities. I cannot say that my db is always empty and I want to generate the create scripts which can be applied on fresh db.
How can I use Liquibase to generate the scripts based on my JPA entities only ?
Note : I am ok in providing the details about my db such as url,driver etc
If your IDE of choice is IntelliJ IDEA, I'd recommend using the JPA Buddy plugin to do this. It can generate Liquibase changelogs by comparing your Java model to the target DB.
So if your DB is empty, you'll get a changelog that describes your whole model. But it is also useful to keep your evolving model and your changelogs in sync.
Once you have it installed and have Liquibase as your Maven/Gradle dependency, you can generate a changelog like this:
Try to use liquibase-hibernate-plugin
You have to create a schema with persistence properties according to Database Schema Creation and then use the Liquibase generateChangeLog command.

How can I re generate a new JPA entity(and a database table in a different database schema) for an existing jhipster project?

How can I generate an entity(and a database table) in a different schema other than the default public schema for an existing Jhipster(4.6.2) project generated with spring boot and AngularJS as the technology stack. [Database - Postgresql]
I am very interested if it is possible. Otherwise one simple way to reach that: you could try to generate the entity using the jhipster command jhipster entity <entityName> --[options] see for more details.
And customize your application to use multiple databases by following this excellent article: https://www.baeldung.com/spring-data-jpa-multiple-databases

Using Liquibase and Spring Boot

I have a Spring Boot application and want to use Liquibase to generate the changelogs for my JPA entities. However, I encounter different issues, depending on my approach.
My first approach is to use the diff goal of the maven plugin. The url is my H2 development database with the H2 driver and the reference URL is something like "hibernate:spring:myBasePackage.myEntityPackage?dialect=org.hibernate.dialect.H2Dialect" with the driver "liquibase.ext.hibernate.database.connection.HibernateDriver". In that case Liquibase seems to recognize my entities, but prints the differences to the console. Also the differences do not have the form of a changelog file.
My second approach would be to use the generateChangeLog goal of the maven plugin. In this case my url is "hibernate:spring:myBasePackage.myEntityPackage?dialect=org.hibernate.dialect.H2Dialect" with the driver "liquibase.ext.hibernate.database.connection.HibernateDriver. In this case I am getting an error "Unable to resolve persistence unit root URL: class path resource [] cannot be resolved to URL because it does not exist". This error can be found in both the Spring an the Liquibase issue trackers, but it seems like it is always said that this error is already fixed.
My third approach is basically like the second, but in this case I am using a "hibernate:classic" url with an implementation of "CustomClassicConfigurationFactory", which registers my annotated classes explicitly. This does work. However, in this case I have to do this in my application-jar. I have to add my application-jar as a dependency for the maven-plugin. Thus I have to build my application-jar (and install it to the local Maven repository), before I can generate the changelogs. This seems to be cumbersome.
My questions are:
Is there an easier way to generate the changelogs for JPA entities in a Spring boot based application?
Why are the first two approaches not working?
Is there a way to simplify the third approach?
I am using:
Spring Boot 1.5.4.RELEASE
Liquibase-Hibernate4 3.6
Liquibase 3.5.3
Many thanks in advance.
In the first approach using liquibase:diff , the change set for the entities (create table change set) will not be generated since liquibase do not assume new jpa entity as change.
In the second approach generateChangeLog, it generates the change log from the given data base.It won't look into your jpa entities.
In order to generate the ddl scripts for your jpa entities , just submit the following to your jpa properties
<property key="javax.persistence.schema-generation.scripts.action">drop-and-create</property>
<property key="javax.persistence.schema-generation.scripts.create-target">./ddl/create.sql</property>
<property key="javax.persistence.schema-generation.scripts.drop-target">./ddl/drop.sql</property>
The above will generate the scripts the ddl folder under the root folder.
You can check the other properties here https://thoughts-on-java.org/standardized-schema-generation-data-loading-jpa-2-1/

configure database schema in spring boot application using jdbc template

I have a spring boot application using spring jdbc template for DAO layer connecting to Oracle DB. The DB username is different than the schema on which the queries will be run. Hence when the queries are run it needs to run using a different schema and I do not want to prefix the hardcoded value for the schema(For ex select * from user1.table.....)
I researched a bit and couldn't find a simple and straight way to do that.
For ex if I were using JPA I could have simply configured the property spring.jpa.properties.hibernate.default_schema=<schema name> but couldn't find an equivalent way of configuring the same when using spring jdbc
I ran into a similar problem and didn't find an ideal way to do it. I ended up setting the schema in SQL when the app loaded. At least I was able to reuse spring.jpa.properties.hibernate.default_schema, which I already had set for the JPA default schema.
final String schemaName = jpaProperties.getProperties().get("hibernate.default_schema");
jdbcTemplate.execute("SET SCHEMA '" + schemaName + "'");
This clearly isn't ideal, but it is better than defining your schema in multiple places.
(Note: I autowired both the JpaProperties and JdbcTemplate.)
You need to use the Oracle JDBC Driver.
A good example can be found in this mykong article:
# Oracle settings
spring.datasource.url=jdbc:oracle:thin:#localhost:1521:xe
spring.datasource.username=system
spring.datasource.password=password
spring.datasource.driver-class-oracle.jdbc.driver.OracleDriver

how to retrieve values from existing table using hibernate

I am using Hibernate & Spring In my project. I need to access already existing table values from DB using hibernate.
I just have an entity class & I use Constructor injection here.
I am Using Hibernate & spring in same file.
Hibernate allows to generate beans and mapping configuration out of existing databases. This is called `Reverse Engineering'.
You can find the documentation here:
http://docs.jboss.org/tools/latest/en/hibernatetools/html/reverseengineering.html

Resources