Can I combine querydsl-jpa and querydsl-sql to create incremental changes to the underlying database? - sql

We have an ongoing project in which we use querydsl-jpa to make the queries.
When we deploy a new version of the product, it auto-executes sql file scripts to update the tables before starting hibernate. This scripts are just sql strings manually crafted (and very error prone).
I would like to use querydsl-sql to write the migration scripts programmatically and once that's done, use querydsl-jpa for normal ORM querying.
So the question is, if anyone knows if they can live together in the same project? And if anyone has done that before?

Using Querydsl JPA and SQL in a project together is a very common use case. You can for example use the JPA API and Querydsl JPA for CRUD use cases and Querydsl SQL for queries.
Querydsl SQL doesn't support DDL scripts (CREATE, ALTER etc). So that part is not covered by Querydsl.

Related

Pros and Cons of Hibernate

In my Project iam about to use Hibernate but one create confusion is:
That I Read somewhere:
Hibernate has its own query language, i.e hibernate query language which is database independent
So if we change the database, then also our application will works as HQL is database independent
HQL contains database independent commands
Does it means that we dont have to write stored proceedure and views while using Hibernate in java?
Short answer: You dont have to write any query and/or stored procedure. (Also you can hibernate tell to create/update all required tables for you, during application start.)
Long answer: Hibernate can be used without any manual definition of a query. (Using the EntityManager, you can simple tell hibernate to get everything of user.class from the database.) However it does support HQL as well as SQL-Queries, also.
SQL Queries of course will stop to work, when you switch to another database later on. HQL will work for every Database, because hibernate is able to translate HQL Queries to any (of the supported) Database Languages.
But be aware: In my Opinion Hibernate is damn slow if you let hibernate do all the work. (Hibernate fires a LOT of single Select Queries, when loading entities with complex relations)

Schema generation using Hibernate Vs Manual schema generation

I have worked on projects that involved creation of schema by hand coding sql scripts. Then we have used hibernate to do DML related activites.
Now, I am starting a project that involves extensive database entities creation and I was wondering if it is a good idea to use Hibernate itself to generate the entities. In other words, is hibernate capable of handling all possible DDL related scenarios, especially the ones that are complex in nature. Or, is it advisable to hand code the DDL sql scripts and use Hibernate for DML related tasks.
Thanks for your inputs.
No, Hibernate isn't able to handle all possible situations (synonyms, tablespaces, and all sorts of various things can't be handled by Hibernate).
I would only consider using Hibernate (to handle the schema creation and updates) for a quick and dirty POC. Otherwise, SQL scripts or Liquibase are your friends. You'll need them once you have a database in production that you need to migrate anyway.

How to supply SQL functions and views required for testing Django app

I've created a file <APP>/<MODEL>.sql according to the Django docs in order to make use of a hook to pass arbitrary SQL after syncdb is run. Inside this file are two function declarations for PostgreSQL and a statement that creates a database view. This runs fine in production but -- just as the docs say -- the code is not run for the test database because the use of fixtures is suggested. Now my unittests are missing the crucial database views and functions and thus fail.
How do I test code that relies on raw sql functions / views?
UPDATE
I dug up this ticket which concerns this question directly and also presents a small workaround.
I found the best way to handle this is to put the custom SQL code into Django's migrations.
Django and South (which is the predecessor to Django's own migration framework) both provide commands to create custom (i.e. empty) migrations. The code for creating database views or functions can be put into an empty migration and will be run whenever a new installation of the project is migrated or the test suite is run.
A tutorial on how to use custom migrations for database views with South can be found here. The syntax is a bit different in Django's own migration framework but the documentation about RunSQL explains it all.
Just run them natively like the sql that they are.
or
Use sqlcustom
or
Don't bother with them; you might find yourself swimming upstream to try and make good use of these functions and view via the ORM.
or
Consider another python framework (dare i say it) which is more attuned to using native sql.

ORM tool to create SQL tables

I need to create a database in SQLite, but I do not want to create the tables manually.
I already have the model of the data I need in the database, and what kind of relationship is each one (many-many, one-many, ...)
I'm wondering if there is a tool that allows me to do that?
I just need the tool to generate the SQL code. Then I will take care of the queries manually using SQL
I was thinking about placing the model in Django, and see what it generates, but there should be a tool not linked to a particular language that allows me to do that. Am I wrong?
Hibernate have the ability to create a scheme from mapped classes. There is support for SQLite.
You can go for dia (see "Tools that generates something from Dia diagrams" at http://projects.gnome.org/dia/links.html).
Also there is SQL::Translator and DBIx::* that allows reading an schema from YAML, Excel, and other sources, but these are Perl specific.
Good luck
You can use Symfony + Doctrine framework. It can generate SQL queries.
Try this module on CPAN: Parse::Dia::SQL

NHibernate, ORM : how is refactoring handled? existing data?

When using an ORM (specifically NHibernate), how is refactoring of data handled? I mean, if data objects change such that the underlying relational DB schema changes, does the ORM offer any help in schema upgrades / downgrades in the DB? How about migration of existing data to the new schema?
I am in the process of making a decision on using an ORM and have very limited exposure. Please bear with me if the questions are naive.
In NHibernate, you can use the SchemaUpdate class to make additive changes to your schema. (Additive changes would include new tables, new columns, etc., but not deletes.) SchemaUpdate is intended for development purposes and is not intended to be run in production. I would strongly recommend checking out a SQL migration tool such as Tarantino, dbdeploy.net, RikMigrations, or similar.
Migration tools come in two flavours - SQL script-based (Tarantino and dbdeploy.net) and code-based (RikMigrations and Rails-style migrations). With a code-based migration tool, you write your migrations using code written in C#, VB, Ruby, ... SQL script-based tools take an ordered set of SQL scripts. In either case the migration tool runs any migrations against your database that haven't been run before. (Typically a migrations table lists the scripts that have been run and allows the tool to figure out which still need to be run.) The SQL scripts are generated via:
// SchemaUpdate.Execute(bool script, bool doUpdate)
new SchemaUpdate(cfg).Execute(true, false);
and then edited to taste. Or you could generate a new schema using NHibernate's SchemaExport and using a schema diff tool such as Microsoft Visual Studio for Database Professionals Ultimate Now With Extra Mayo Edition (aka DataDude) or RedGate SQL Compare. You would need to write transformation scripts by hand as there is no way in general for the SQL migration tool to know that that Foo char(1) column filled with T/F should be transformed into that Bar bit column.
Personally I prefer the SQL script-based migration tools as I can generate the schema diffs using a tool and then edit to taste rather than having to hand-roll the entire migration using C# or similar language.