How to properly implements Hibernate from an already existing database? - java

I'm trying to create a new Play application using JPA / Hibernate from an existing Mysql database.
hibernate.hbm2ddl.auto in persistence file is set to "update".
My table posts has 6 entries.
Problem is when I execute an HQL statement like "SELECT p FROM Posts p", I got an empty result, while if I execute a native query like "SELECT * FROM posts" I got my existing entries.
I think I'm missing something huge here, like hibernate's internal caching system or something, is there anything like this that can explain this empty result ?
Does Hibernate has to build some specific cache when used from an existing database so that it returns correct results with HQL statements ? Why would it need such a thing ?

My table posts has 6 entries. Problem is when I execute an HQL statement like "SELECT p FROM Posts p", I got an empty result, while if I execute a native query like "SELECT * FROM posts" I got my existing entries.
This sounds like you have to define mappings from mysql tables to jpa entities. You can generate them automatically using hibernate tools (like see an example here on how to do that using an Eclipse plugin).
You should get some results from your query after that, Hibernate will populate its caches along the way.

Related

JPA Hibernate query vs Native query

I use Spring Data JPA (hibernate) generated queries for fetching data from my Sqlserver. Now i am getting performance related issues in my system.
Load findByLoadId(Integer loadId);
This is the query i am using to get data. This query returns 25 cell data but i only use 5 data from that.
can i use direct native query like
select id,date,createdBy,createdOn,loadName from Load where
loadId=:loadId
but if native query is suggestable then I am having question like Does ORM frameWork reduce performence by getting unneeded data from Database?
By "data cell" I assume that you are referring to database table columns, and not to records. The answer to your question is that yes, ORM frameworks might tend to just do a SELECT * under the hood, which can result in unwanted information being sent across the network to your application. If the JPA repository interface is behaving this way, you may switch to either an explicit JPA query (e.g. using the #Query annotation), or even a native query. Then, just select the columns you want. The issue here is that ORM frameworks map object templates (e.g. classes) to entire database tables. So, the concept of entity implicitly includes every database column. If you go with the option of selecting only certain columns, you may need to do some juggling on the Java side. Note that if the use a JPA query, your code would still, in theory, be database independent.

Insert into Select using JPA

I am using JPA (with Hibernate) to talk to the database. I have a task of selecting all records from Table1 and insert into Table2. Can I achieve this using JPA in one step?
I tried using #Query annotation and provided a HQL query (INSERT INTO TABLE1.. SELECT * FROM TABLE2) to it but got a DML error.
Note: As a last resort I am doing it in two steps now, which seems to be inefficient:
step1: getAll from table1
step2: save to table2
JPA tries to do some O/R-Mapping for you.
Your request is just moving data between two relational tables (or a table and pseudo-table). So why do you need to use JPA to do that? Just go straight and use JDBC, or native query. And never forget that nothing is a silver bullet especially an O/RM.

Copy all records from Master table to a Clone table using hibernate

With SQL I can copy data from one table to another mirror table. (e.g. insert into TABLE_EXAMPLE_COPY select * from TABLE_EXAMPLE; .
How can I do the same thing using Hibernate org.hibernate.Criteria or org.hibernate.Query or org.hibernate.SQLQuery?
If you want to perform that action from within the boundaries of JPA or Hibernate, the best way to accomplish that is to use a Native SQL statement.
session
.createNativeQuery( "INSERT INTO table_copy SELECT * FROM table" ).
.executeUpdate();
The other options involve reading the source table into a POJO and then transforming that into the POJO representation for the copy table and saving those rows. The problem with these is that you also introduce network latency and JVM overhead just to create an in-memory object, transform it, and then push it back over the network to the database.
The presented above solution avoids all those drawbacks and allows the database to handle all that in the best performing way it knows how.

Spring JPA - Export to csv based on table records

I'm quite new here, but as I could not find the answer myself. I was hoping someone might.
I am using springboot with jpa to connect to a PostGreSQL db. From this i am retrieving a table e.g exportconfig containing a list of tables that need to be exported to CSV, and it also specifies which columns to export. I decided to use OpenCSV-BeanToCSV For this.
As I am using the spring boot jpa, i am not sure how to generate the query based on the ExportConfig table: e.g i would like to create a query like this:
"Select ... FROM :Tablename" where tablename is based on the value i retrieve from the exportconfig table.
I would appreciate if anyone has any recommendations on how to approach this, any alternative methods are always welcome.

How to copy selective data from one database to another (ORACLE)

We have a need to find a way to copy certain data from production into our dev regions so that we can debug/fix any issue.
Sometimes single user related data gets impacted. We have to replicate the same scenario in dev and find a solution.
Presently we follow two approaches:-
1. Check the audit history and try to recreate the similar scenario
in dev. <50% sucess rate in recreating the exact same scenario.
2. Restore+Encrypt the "whole" production into dev and then continue
on the work. It is an overkill if issue impacts only a single user.
So I am trying to find a way to just select a single user data from production and insert it into dev region.
We just have Java and Oracle. Can't use any external tools. Because we
dont have license and cannot download freeware due to security issues.
I tried the follwing:-
Write a java code which will query the informaition schema tables to find the relationships between the tables and create select statements like below:-
select 'insert into TABLE1(C1,C2,C3,C4) values ('||''''||C1||''''||','||coalesce(to_char(C2),'null')||','||''''||C3||''''||','||coalesce(to_char(C4),'null'));'
from TABLE1 where ID='1006' union all
select 'insert into TABLE2(C1,C2,C3,C4) values ('||''''||C1||''''||','||coalesce(to_char(C2),'null')||','||''''||C3||''''||','||coalesce(to_char(C4),'null'));'
from TABLE2 WHERE TABLE1ID in ( select ID FROM TABLE1 where ID='1006') union all
select 'insert into TABLE3(C1,C2,C3,C4) values ('||''''||C1||''''||','||coalesce(to_char(C2),'null')||','||''''||C3||''''||','||coalesce(to_char(C4),'null'));'
from TABLE3 WHERE TABLE2ID in ( select ID FROM TABLE2 WHERE TABLE1ID in ( select ID FROM TABLE1 where ID='1006'));
2. Use this set of selects in production, so that you get a set of insert statements as output.
3. Use the insert statements in dev.
Problem:-
The select queries are becoming huge. Around 25 MB in total :(
We cannot even execute that big query in production.
Could you suggest any better approach for this usecase?
Does oracle itself allow selective data exports? Or any other way I should write my java code?
We use something like this to move records from one database to another:
copy from username/password#database1 to username/password#database2 insert target_table using select * from source_table where where_clause_goes_here;
Use datapump to move data for the tables you need and with the whereclause you want. Straight forward and standard functionality of the database.
If both the DBs are Oracle, you can create a DBLINK in your local
database for the remote DB and Create a job in your local DB that queries
all the data from remote DB using the DBLINK, and update the tables in your
local database.
Or there are plenty of data migration API are availabe you can give a try to one of them.
Below are the some link,have a look in to them,may be it will solve your problem
http://code.google.com/p/c5-db-migration/
http://flywaydb.org/documentation/migration/java.html
http://migrate4j.sourceforge.net/
http://flywaydb.org/ --- its better to use
http://www.operatornew.com/2012/11/automatic-db-migration-for-java-web.html

Categories