Hibernate Pojo Generator for IBM DB2 - java

I used Hibernate Pojo Generator to generate hibernate stuff. I done it successfully with MySql, but when I tried to generate classes with DB2 it fails. I check hbnpojogen-core-1.4.4-jar-with-dependencies.jar it did not contain DB2 drivers. I added them but it again fails.
I want to know whether Hibernate Pojo Generator works with DB2?

From this tool's website, in the Known Issues section
Although JDBC is meant to hide database differences, each connector driver has its quirks; therefore at the moment, the generator is only known to work correctly on MySQL databases though it has been reported that MS-SQL support looks pretty good too. Support for other databases, notably PostgreSQL, is coming soon. Other databases are as yet untested (submit reports!)
So it seems that it may not work with DB2.

Doing a quick test using:
java -cp db2jcc.jar:db2jcc_license_cu.jar -jar hbnpojogen-core-1.4.4-jar-with-dependencies.jar config.xml
... just produced a ClassNotFoundException. However, using:
java -cp hbnpojogen-core-1.4.4-jar-with-dependencies.jar:db2jcc.jar:db2jcc_license_cu.jar com.felees.hbnpojogen.HbnPojoGen config.xml
... produced the following:
Reading from config: config.xml
Stage 1: Copying skeletons
Stage 2: Getting commit order in 'PARTIAL' strategy
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
With a little more effort, you may be able to get it working.

Related

How can I change the currently used Database just by changing the configurations in Struts2

I have developed an application in Struts2 using MySQL at backend. But now as per interest I want to use Oracle at the back end.
For that I changed the db configurations to Oracle but I found that some of the MySQL queries are unable to run on Oracle directly.
I have searched too much and tried a few thing but still have no solution. Can anyone help here?
You can use Hibernate, it has dialects for both MySQL and Oracle. But if you use native queries then you should use separate code for each dialect.
In the BaseHibernateDAO you can create a method
protected Dialect getDialect(){
return ((SessionFactoryImplementor) sessionFactory).getDialect();
}
Then you can use this dialect for any query you create.
In my opinion you've answered your own question.
MySQL queries are not ALL compatible with ORACLE (example : LIMIT in MySQL would be ROWNUM in ORACLE).
So if you have 'raw SQL' statements that involve specific terms for MYSQL you should change these to ORACLE.
However, if you are using Objects to generate the queries, this would/should be handled 'automagically' by the (for example) persistence solution you are using; just by changing the database driver in the settings.
For more details, we/I would need some examples of code that works on MYSQL and doesn't on ORACLE.
Let me know if that helps.

Alternatives to DDLUtils from apache

I would like to know which alternatives exist to replace DDL utils from Apache.
I ask this because ddlutils project seams to be Dead, and also it does not support H2 Databases. I've searched for it, and I found suggestions like liquidbase or flyway.
My problem is: These frameworks run when project starts and change DB structure based on some XML files. They are really designed for Database Migration.
What I want is a framework to CREATE/ALTER Tables in Runtime, in a high abstraction level., i.e. supportting at least Mysql, Sqlserver, oracle, and H2.
For example I could tell to the engine that I want to create a table with a Field AGE with Type Number, and the framework would rephrase to:
create table MY( id bigint(20))
create table MY(id bigint)
create table MY (id, number)
depending on the underlying db engine.
Any suggestions?
I could see there is a patch for ddlutils, for it to support H2. However I wasn't able to patch my svn checkout...
Any help will be appreciated.
thanks in advance
rui
I know this is an old thread, but wanted to give a definitive answer.
Yes, DdlUtils is dead, hasn't seen an update in 2 years now.
However, it looks like the guys might have switched over to https://www.symmetricds.org. Their repo is https://github.com/JumpMind/symmetric-ds.
As soon as you scratch away at the surface, you'll find that the core of DdlUtils is still in there (even has some of the old Apache copyright notices).
Class names have changed, APIs have changed so there is not a 1-to-1 mapping, but it is getting regular updates and includes H2 and other database support. Honestly I'd rather be getting those things instead of keeping the old APIs.
You're not going to find a guide on using Symmetric DS in the same way as the old DdlUtils doco, but there is enough in the code that you should be able to piece it together.
Take a look on jOOQ it is very useful in generating DDL (and DML too)
create.createTable("table")
.column("column1", INTEGER)
.column("column2", VARCHAR(10).nullable(false))
.constraints(
constraint("pk").primaryKey("column1"),
constraint("uk").unique("column2"),
constraint("fk").foreignKey("column2").references("some_other_table"),
constraint("ck").check(field(name("column2")).like("A%"))
)
.execute();
This looks promising: https://bitbucket.org/aragot/play-sql-dialects/src
At least as a start.
Mogwai ERD designer might help though they do not formally support H2 but you could put H2 into compatibility mode with one of the supported DB systems: https://sourceforge.net/p/mogwai

migrating java app that accesses mysql to code that uses SQL Server

I have with me a java app that uses mysql-- now I have converted the database to SQL Server- but I need to convert the code of the java app so that it now uses SQL Server.
This java app uses Struts and Hibernate
What all things do I have to keep in mind to make this conversion?
hibernate should make the transition seamless for you.
Make sure to choose the correct driver as some drivers might act differently.
I personally like jtds. It has vast documentation.
Look for native queries in your code because they might need some modifications.
Other than that, I don't think you should experience any problem.
I had an application that supported oracle, SQL-Server and mysql and we didn't have specific code for each one.
EDIT : as maba suggests in the comments, you will need to modify the hibernate configuration a bit (driver, dialect, url ).
For more hibernate specific configuration, I found a great stackoverflow answer

How to generate orm mapping classes from sql schema in Java

I have an existing sql schema file for db. Is it possible to generate and re-generate when needed DAO's entities and all other required helper/client classes to access it? I don't mind what will it be -- hibernate, other jpa or something else.
Asuming you/others are still looking for a solution:
I just got the same problem and got it working in Eclipse (slightly different) as follows:
created JPA Project and downloaded & added user library in the wizard
Also wanted to give a schema-sql-file as input but instead found a way to take an actual db as input. (That was surely much easier for the developers of the tool to process than parsing proprietary sql-script-files)
To do that "rightclick" you jpa project an there "new/other/jpa/entities from tables"
In the following Wizard you have to create a db-connection to the db whose schema you want to get as jpa-annotated POJOs (IMHO It's very intuitive..but you may ask if there is a problem)
After finishing all jpa-classes are generated from the db...saved me from a lot of dummy work :)

How to run sql scripts in order to update a Derby schema from java code?

The derby database installations of our customer sides have different schema versions. E.g. Customer1 has db schema version 4.1.5.0240, Customer2 has version 4.0.1.0330.
The idea is to update theses schemas to actual version when a new software is installed. There exists several sql scripts in order update a version to next level. E.g. update_4.1.5.0240_to_4.3.1.0020.sql.
The tool/procedure I am looking for should read the actual installed version from a derby database table. According to that version the appropriate sql script shall be run in order to update the schema to next level. This procedure must be repeated until no matching sql script can be found for the actual read version.
The installation at customer site should be done headless without any administrators help.
1.) I would prefer a java program that reads the version using JDBC. But how to run the sql scripts from within java? Should I call the ij commandline tool from java?
2.) Are there better alternatives?
Thanks
Viktor
You might find the ij.runScript method helpful: http://db.apache.org/derby/javadoc/publishedapi/jdbc4/org/apache/derby/tools/ij.html
One thing to be aware of is that it's not easy to automate the error handling inside the script, as there are no control flow (if/then/else) constructs in the ij scripting. But if you just need to perform a series of DDL operations in a clean manner, ij.runScript is a pretty good technique.
You could store the SQL in a text file:
From your java code you read in the SQL for updating/inserting data into the various tables:
Your java code could also perform checks on which 'SQL update file to run'.
something along these lines (in very pseudo code)
Installed = getCurrentInstalledVersion();//info stored in an admin table for current version
if (Installed < V4.1){//This would probably work better as a switch
String sql = updateWith(URL file:\\PathTov4.1\sql.txt);
}
else{
if(Installed = OtherVersions){
String sql = UpdateWith(URL file:\\PathToOtherVersion\sql.txt);
}
}
Connect.sendSQL(sql);//send the sql to the DB
I leave you to figure out the fuller details. If you already found a solution, why not post it here.
If you use a switch you could run updates to a newer version via other versions if required by placing the versions in the correct order, and inserting code before any break;

Categories