Spring JPA + dynamically switch the datasource between schemas in postgresql - java

I have to modify design of the existing application according to following manner,
According to above design each project contains it's own schema and that mapping table (project_schema_table) is under main schema. All schemas are under one Postgresql database and in future users will create another new schemas (on demand) for their new projects.
Before displaying login window I have to list all existing projects and once user select his project I have to direct to that project with relevant schema. Other than main schema all other project base schemas are exactly the same.
This is a desktop application developed with latest versions of java technologies JavaFX, Spring and JPA with Postgresql database. Currently it has only one project related schema and I have to introduce main schema and all other subsequent project base schemas.
I have found similar stack overflow post but it didn't contains any solution. Whether I can accomplish this task with Spring AbstractRoutingDataSource ? or is there any other mechanism. Thanks.

The feature you are looking for is called Multi-Tenancy and Hibernate supports this out of the box from version 4.x onwards. Please see this post on how to use this feature.

Related

Spring boot. Automatically create Entity classes from MySql schema

I have been working on EER diagram model for my Mysql base for the last 7 days. It is a rather complicated model with lots of connections and attributes. Now i know spring boot automatically creates tables based on your entity classes(including foreign keys and other settings) if you use spring.jpa.hibernate.ddl-auto = update in your application.properties, but is it possible to create entity classes after providing good credentials to DataSource object based on tables withing schema?
Point is i would probably need another 3-4 days of back-end coding to create all the classes with all the attribute, relationships etc.
Given the fact it can only be done in one correct way, based on schema tables and it is not really rocket science. Why not do the thing just once?
There is this question Automatically create Entities from database
but 1. i am using spring boot not JPA project and second blog is no longer active.
Any hints?
Just figured it out. I used the hibernate perspective in eclipse to connect to a database, create a cfg.xml and run the project as hibernate configurations, create a new reveng.xml config and that will work. More detailed answer can be found in the article below.
http://o7planning.org/en/10125/using-hibernate-tools-generate-entity-classes-from-tables
Hope it will help someone!!
There is this question Automatically create Entities from database but
1. i am using spring boot not JPA project and second blog is no longer active.
Under the woods, Spring boot uses JPA and more specifically Hibernate since it has bad compatibility with other JPA implmentations such as EclipseLink.
Why don't you use Dali Eclipse plugin ?
https://www.eclipse.org/webtools/dali/docs/3.2/user_guide/tasks006.htm
It has a wizard with many options and it addresses well this kind of need. The real drawback when I use it and we cannot store our orm configuration. So, since you have many tables, I advise you to generate it in an incremental way your entities.

Use of Spring data JPA with maven in Eclipse

I'm new with Spring and I would like to use Spring Data with Jpa, Maven and Mysql in Eclipse environment. Then I'm going to use Tomcat as application server
I read a lot of tutorial but all of them are different from each other therefore don't understand what I have really to do. At the moment I'm confusing between these technology and DAO/DAL structure
Must I use Hibernate?
I have to use it with java JPA and then I have to add Spring data?
If anyone has an example or a tutorial I thank him.
Official spring data link
For spring-data-jpa,maven,mysql,hibernate,eclipse example tutorial
Hibernate is optional, it depends on your use case what you want to do. Its a ORM for mapping your db tables into java objects and have lot of other features like session caching, second level caching, criteria queries, validation, search/filters etc.
If you are just testing queries to load data you can use spring jpa queries directly, you will find examples in spring-data-jpa link

DB administration site for Spring + JPA project

I would like to know how if there is any way to automatically generate a database administration site for a Spring + JPA project. It should take the annotated JPA entities and dynamically generate the site to keep track of any change in the domain Java classes.
I am looking for something similar to the Django admin site (I think you can also do the same with Ruby on Rails), that allows the user:
to see and update the data in the different tables without developing any extra code
to handle relations between tables, different kind of data (numbers, dates, etc)
In the Java world I am only aware of Spring Roo, but it does not fit my needs because I want to integrate it into an already existing third-party project.
Try Play! Framework's CRUD Module

SQL Schema to Hibernate Class

I got an existing table in Oracle database how can I generate my Hibernate Java class etc from this table?
Using Eclipse 3.2, Java 5
Do you mean generate a POJO that maps to the table? You can use the Hibernate Tools plugin for Eclipse. Otherwise you can create the POJO manually and map it to the table using Hibernate Annotations.
The Hibernate Tools for Eclipse and Ant does support reverse engineering. From the website:
Reverse Engineering: The most powerful feature of Hibernate Tools is a database reverse engineering tool that can generate domain model classes and Hibernate mapping files, annotated EJB3 entity beans, HTML documentation or even an entire JBoss Seam application in seconds!
Refer to the provided link for more details.

generate java domain objects from database table

may i know in eclipse, is there any feature that will auto generate domain objects with all table relationship properly mapped in class?
can provide me with some reference articles on this?
You can use something like Hibernate to accomplish this
This plugin set for Eclipse called Hibernate Tools for Eclipse and ANT will do most of the work for you.
In particular it will do Reverse Engineering: The most powerful feature of Hibernate Tools is a database reverse engineering tool that can generate domain model classes and Hibernate mapping files, annotated EJB3 entity beans, HTML documentation or even an entire JBoss Seam application in seconds!
Telosys code generator does this kind of job.
It's an Eclipse plugin, it uses the database schema to create a light model
that is used to generate the Java code.
There are some predefined templates available on GitHub (for JPA, POJO, Documentation, Spring MVC, etc )
See http://www.telosys.org
and http://marketplace.eclipse.org/content/telosys-tools
Templates : https://github.com/telosys-templates-v3
Articles about code generation with Telosys :
https://modeling-languages.com/telosys-tools-the-concept-of-lightweight-model-for-code-generation/
https://dzone.com/articles/telosys-a-code-generation-tool-by-laurent-guerin
You can use Hibernate Tools 3.0.0.GA either via Eclipse or ANT to auto-generate your hibernate domain entities directly from your database tables.
See tutorial here :
http://docs.jboss.org/tools/3.0.0.GA/en/hibernatetools/html_single/index.html
I have a solution for you i.e to create auto generate domain objects with all table relationship properly mapped in class ...Try Dal4j yes you can find it in sourceforge.net/p/dal4j/wiki/ DAL4j is a Command Line and Framework tool that can be used to reverse engineer a MySQL or SQLServer database schema into a set of JPA Entity Beans.
DAL4j can be useful for scenarios where there is an existing database schema but a technology other that JPA is used by applications to interact with the database. DAL4j can provide an easy way to migrate your code base from other technologies such as JDBC or Hibernate to JPA.
The beans generated can be 1 or two types: Simple or Framework. Simple beans are standard pojo classes managed by your application using JPA semantics. Framework generated pojos use the DAL4j framework DAO generic to simplify CRUD operations.
DAL4j provides optional hooks to allow you integrate encryption/decryption of data fields that must be encrypted in the database.
Last, DAL4j provides a set of Generic classes that can be used to simplify creation of Session Beans which perform CRUD operations using generated Entities.
I think you will find this article feasible....
You want an object relational mapping of which Hibernate is the most popular for Java. The hibernate tools are typically better for taking annotated classes and using them to generate a schema, as opposed to vice versa, which is what you sound like you're doing. I suspect you'll be doing a lot of hand-annotating if you're working with a legacy DB schema.
if you use grails, you can generate domain objects with GRAG http://sourceforge.net/projects/grag
I use eclipse for java development, but when it comes to generating domain entities I use Net beans.
Create an EJB module, and then right click and generate entities.
You need to set up the database also you can select the tables you want visually.
Regards
Lyju
It feels like another rather common question that people always run into.
The link below links to a blog detailed enough for me to learn how to generate entities from database schema the first time.
http://shengwangi.blogspot.com/2014/12/how-to-create-java-classes-from-tables.html
Just in case, the following link refers to eclipse help page. This link should never expire:
http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.jpt.doc.user%2Ftasks021.htm
I downloaded JBoss and failed to understand how it works. I think the plugin that I used is Hibernate Tools but I am not sure as I did not install any new plugin for this purpose. I am using Eclipse Luna for EE.
Hope this helps.
I got so tired of manually coding this kind of stuff so I made a tool to generate models, dao, and dao implementation from a schema. It's oriented towards spring boot and only tested on MySQL, but for those that don't want to use Hibernate and just want to work with jdbc/sql and JdbcTemplate, or just want POJOs with getter/setters generated for tables, then this could perhaps be something to kick off the coding.
Called Jassd (Java Automated Spring Source-code for Databases generator), I'm "jazzed" to introduce this tool: https://github.com/aforslund/jassd

Categories