How to create entity classes from database using spring boot? - java

I'm working on a spring boot application using spring data jpa at Spring Tool Suite IDE. I need to create entity classes from database but I couldn't find any answer to how to do it.
I saw an answer about adding JPA Facet adding and using it. But I couldn't add JPA Facet because my application is maven web , not jee application.

Mapping from db table -> entity:
Eclipse:
Make an empty JPA Project, set data sources and use JPA Tools > Generate entities from Tables. Then copy-paste model classes to your project.
IntelliJ:
Install plugin JPA Buddy and make a reverse engineering:
Reverse engineering video
Netbeans:
How to
If you know other ways to generate entities, please tell us.

You don't create entity classes from database. You create models to map your table and its relationships with other tables.
Application Properties to configure JPA mapping.
https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
Map your classes to a table.
https://www.vogella.com/tutorials/JavaPersistenceAPI/article.html#simple

Related

IntelliJ Hibernate facet without hibernate.cfg.xml?

I am using Spring Boot Data JPA for my project and would like to add the Hibernate facet. However, this seems to require the use of hibernate.cfg.xml to use for the path property in the facet to work. This completely defeats the purpose of Spring Boot and the auto configury shenanigans it provides. My workaround is to use the JPA facet with Hibernate as the default provider, but I would like to use the Hibernate facet instead. Any help is appreciated.

Reading data from database that was inserted by another Spring Boot JPA project

I have two projects
ProjectA --> 'Spring Boot JPA based Java` project
ProjectB --> General 'Java' project
My Database is MySQL Server
My ProjectA is a web project so from a webpage I was able to get a lot of data inserted into multiple tables without any insert queries. This is because it's a JPA project and it was super easy.
Now I want to access the same data from the same database in a few Java classes in my ProjectB. I don't want to convert this into a Spring Boot project and make it more complex just to read data from two tables.
On the other hand I really don't want to use JDBC Connectors and process ResultSet etc. I like the way how annotations were used in Spring Boot JPA project to write and read data from the database.
What options do I have here?
You can use spring's JDBCTemplate or HibernateTemplate to minimize your code.

How to generate Spring WebMVC CRUD API

What I want to do is to create a full Spring WebMVC CRUD API from database tables/Hibernate JPA entities, into an existing Maven Web Project.
What I want generate, to be precise:
Spring WebMVC controller (mapping&implementation)
The JPA entity (if not given) - ofc. using the standard generation built in to most IDEs
Spring Data JPA repository
Optionally modifying some other files (f.e. adding the entity to existing persistence.xml)
Is there a project for this?
What would be the best way to create something like this?
What I had thought about:
Standalone Java APP
Maven subgenerators
Eclipse plugin
Standard code generation methods (f.e. Acceleo plugin)
What would you suggest?
Partial Answer :
If you can generate JPA entities and Spring Data JPA Repositories, you can use Spring Data REST to expose the repositories as a full CRUD REST API.
Although Spring Roo will generate a CRUD application for you, however the code it generates is outdated. If I where starting a new project, I would definitely stay away from it (at least until a new version is released).
I suggest you take a look at JHipster which is a Yeoman that will generate a CRUD application using some of pretty hot tech (like Spring Boot, AngularJS etc.)
Besides the novelty factor of the generated code, another great feature of the project is that it is extremely active as is evident from the releases
The response is probably in this tutorial :
https://sites.google.com/site/telosystutorial/springmvc-jpa-springdatajpa
You can take a look at crud-rest-gen project which explains how to use the crud-maven-plugin to generate :
the CRUD Rest API
the documentation of the API
the HTML5/AngularJS CRUD Administration UI
the Rest API for retrieving audit information and associated unit tests if you use Hibernate Envers to audit your entities
All you have to provide is the data model containing the JPA entities.
Spring-roo (link) have these features, and if you want you can remove spring-roo from your project and remains a java-spring project.
Sample from link :
roo> hint
roo> project --topLevelPackage com.foo
roo> jpa setup --provider HIBERNATE --database HYPERSONIC_IN_MEMORY
roo> entity jpa --class ~.Timer --testAutomatically
roo> field string --fieldName message --notNull
roo> hint web mvc
roo> web mvc setup
roo> web mvc all --package ~.web
roo> selenium test --controller ~.web.TimerController
roo> web gwt setup
roo> web gwt all --proxyPackage ~.client.proxy --requestPackage ~.client.request
roo> perform tests
roo> quit
As you can see : create the project, setup jpa provider, create Entity, create MVC Controller, the Tests and some GWT setup.
A Yeoman generator for generating Microservices with SpringBoot in Hexagonal Architecture
https://www.npmjs.com/package/generator-springboot-hexagonal

How to access multiple database schemas from single persistence unit?

I am working on a project in which currently I have single persistence unit file as I have only one database schema there in my db. Now I need to separate that schema into two different schema. So I made two different ORM files and mapped it into the PU. Now when i build my EJB project its working fine but as soon as I build my WEB project it starts giving me compilation error.
So, is there any other way so that I can manage two different schema together??
Note that both the schema are related with foreign keys.
Please help me out.
If you are using Oracle and you have SCHEMA_1 and SCHEMA_2 and you can define synonyms:
As SCHEMA_2, grant the appropriate privileges to SCHEMA_1
Define synonyms in SCHEMA_1 for the tables in SCHEMA_2
Now in SCHEMA_1 you should be able to use SCHEMA_2 tables as if they were there

Loading selected Hibernate entities with JPA & Spring

Is there a way to load only selected entities with Hibernate?
I would like to only load a selected handful for integration testing.
I create an AnnotationConfiguration programatically for this kind of tests and use methods such as addAnnotatedClass(Class) to "enlist" entities.
I ended up using a custom persistence.xml and gave it to the EntityManagerFactory in the spring config.

Categories