Using criteria-api to dynamic tables - java

I use criteria-api to made sql query to the database and it work very fine.
But now I need to use dynamic tables , where some tables can be create or destroyed and some column can be add or removed.
I want to manage this entityties in dynamicBean of apache, then I can create bean and edit the column.
How can I made a sql sentece using criteria-api if the bean is not in the file persistence.xml and if it is a dynamic bean?

Not sure on dynamic beans, but you may wish to investigate EclipseLink's dynamic entity support,
http://www.eclipse.org/eclipselink/documentation/2.4/solutions/softwareasaservice002.htm#BABFJDCF

Related

Spring Boot + JPA + Hibernate different table name prefix

I am working on a multi-tenanted application (with old database structure) where I have a common user table and set of tables based on the access permission.
For example if the user can work with invoice of different companies C1 and C2, the database contains a tables with name C1_invoice and C2_invoice.
I am able to achieve adding prefix with one company using org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
So I can access C1_invoice table. But how can I choose the prefix C1 or C2 dynamically?
You could use a variation of this approach.
It is basically using hibernate's multitenancy features in Spring Data by providing a custom MultiTenantConnectionProvider. The connection provider reads connection details from a map of data sources. You could provide a different value for the hibernate.physical_naming_strategy in each of the data sources. I'm not sure if there's a way to specify the prefix for each data source as a property, though. You could end up with a separate subclass of the PhysicalNamingStrategy for each tenant. Could be gruesome.
What DB are you using? Alternatively, you could resolve the issue by providing a schema per each tenant, and aliasing their tables from the default schema using unprefixed names, something along the lines of:
CREATE SYNONYM C1.INVOICE FOR DEFAULT.C1_INVOICE;
This way, you could use Hibernate's standard MultitenancyStrategy.SCHEMA strategy.

Hibernate forward Engineering

I want to implement a simple application Hibernate forward engineering concept. Where If I make any changes to Hibernate Entities then Hibernate should automatically make specific changes to the database columns.
For example:
If I add a new field in Hibernate Entity (POJO), then a new column should be added to the DB table. (This should happen when I restart tomcat).
Is this possible?
You can do this using hibernate configuration property hibernate.hbm2ddl.auto. set this property value to update to reflect changes from POJO to database. Refer this link for more information.

Can MyBatis create the database schema?

Has MyBatis any feature to allow create the SQL schema from de Class Model like Hibernate does?
I'm looking for that in Google and I only found information about MyBatis Generator (http://mybatis.github.io/generator/).
This tool seems to be useful for generate the Java model from the SQL Schema, which is just the opposite I want.
Can MyBatis create the database schema?
I'm afraid not. In order to do that you need an ORM and MyBatis is not an ORM.
With an ORM solution (like Hibernate for example) you map tables to entities. The entity is the (object) representation of the (relational) table and contains all the metadata (getters/setters, annotations etc) necessary to create the table.
MyBatis does not map entities to tables, it maps methods to SQL statements. There is no way to determine from the methods or the SQL statements what the structure of the database should be. When you use MyBatis the tables must already exist.
I tried create a new database schema and new table via the mybatis, it works well with my mysql db. So it seems mybatis totally support the execution almost all of the free sql statement.
I think you could refer another answer.
Creating a table programmatically using MyBatis and MySql

JPA2/Hibernate - Creating a schema on the fly (ie without pre-create the schema manually)?

I use JPA 2 with Hibernate Entity Manager 3.6.4. Once I have marked my entities with various annotations (#Entity, #MappedSuperClass etc), I put in my persistence.xml file the default schema to use (hibernate.default_schema property).
I know it's possible to create automatically the objects contained in the schema.
But is it possible to create the schema itself automatically and then create the objects it contains ?
EDIT :
I use this parameter too : hibernate.hbm2ddl.auto, to tell Hibernate to create the schema if it doesn't exists yet. No luck, Hibernate doesn't create it !
I have googled a little bit and find this post : Hibernate hbm2ddl won't create schema before creating tables.
The fact that Hibernate does not create a schema before creating table is a bug. Other database suffer from this situation : H2, Postgresql etc.
This bug is planned to be fixed with 5.0.0 release of Hibernate.
So, for now, the only workaround is to create the schema by yourself, either manually or by a mean offered by your database vendor, since Hibernate can't do it itself :\
I managed to build a workaround that uses the hbm2ddl default flow.
Since it always calls the "database-object" drop statements BEFORE creating schema, you can do something like this:
<database-object>
<create></create>
<drop>DROP SCHEMA IF EXISTS myschema cascade; CREATE SCHEMA myschema</drop>
</database-object>
unfortunately the create clause is mandatory and sadly it's only executed AFTER schema creation, no matter what order you put it on cfg.xml, so I made it empty, that way you don't have errors trying to creating schema again (it was already created together with drop)

How to map dynamically created table in Hibernate?

I am working on a web application. We are using Hibernate as ORM in our project. Actually, our application creates some tables dynamically based on user selection. The user can select table name, column name and then s/he can import data from a csv file. So my question is: how to map this dynamically created table with Hibernate and Java objects?
It can be done dynamically, but it's somewhat messy:
You'll need to dynamically alter Hibernate's Configuration object before SessionFactory is built. If you're using Spring, this can be done by overriding postProcessAnnotationConfiguration() method of AnnotationSessionFactoryBean; otherwise you'll just need to do it using your Configuration object prior to invoking buildSessionFactory() on it.
If you need to do this without application restart, you're looking at either rebuilding your SessionFactory (which means your users will have to wait until that's done) or using a separate SessionFactory instance specifically dedicated to your custom classes (which is next to impossible if your custom classes need to reference your built-in classes).
You can get access to class / table mappings via configuration.getMappings(). You will then need to create a new table mapping via Table API and add it to configuration via addTable(). Same thing will have to be done with PersistentClass which represents a class mapping. If you're using the same class to represent multiple entities (e.g. map multiple tables) make sure to use unique entity names for each. You'll have to do this (alter the configuration) on every app restart.
How much of the tables are dynamically created? Are the tables similar and you just change the database name?
Here is a discussion of changing the table name:
http://www.experts-exchange.com/Programming/Languages/Java/J2EE/Frameworks/Spring/Q_24237099.html
If you are completely building a new table, can you use a view, and just direct people to a view?
Why are you using Hibernate for this, rather than just dynamically creating queries in JDBC?
The database solution - you can create a view and point it to one table or another (assuming the structure is identical).
CREATE VIEW HIBERNATE_NAME
as
SELECT * FROM TABLE_A
or
CREATE VIEW HIBERNATE_NAME
as
SELECT * FROM TABLE_B
You would need you application to execute native SQL (DDL),
however this should be easier than Hibernate hacks

Categories