I need to create tables based on existing classes form the library using jOOQ. It's possible to use DB migration scripts (something like V1__first_script.sql) manually.
The question: is it possible to generate migration scripts or create tables by jOOQ exactly from the java class? Avoiding to write SQL scripts manually.
Thanks!
You can use one of the DSLContext.ddl() methods to generate a set of DDL queries from jOOQ generated code. Beware that this will not reproduce all of your schema definition, only the parts that the jOOQ API can represent. E.g. CHECK constraints are not yet included.
Related
I would like to find a reliable way to test my Maria DB schema with Jooq.
This is what I have now:
let Jooq with Gradle to extract an XML schema from the real DB (i.e. Maria instance) via org.jooq.codegen.XMLGenerator, this step will generate a maria_information_schema.xml;
use this schema to generate all the Java Classes.
To test all the classes I will write I have develop a technique:
fire a H2 embedded in ram database;
build a java InitDatabase.java class which manually set a DB schema as similar as possible with the MariaDB one;
preform all the test on the in ram DB.
This procedure works perfectly as far as I don't change something on the real DB and I forgot to do the same on the InitDatabase.java class.
My question is: is there a way to use the XML schema or the generated java classes to create an H2 Database with the same schema as the MariaDb one without writing manually all the create table statement?
Thanks
This is an open ended question with no obvious, "correct" answer. jOOQ's official take here is that you may want to re-consider using alternative RDBMS for testing what you could be doing with your target RDBMS directly, specifically using testcontainers.
You could combine this approach with jOOQ's code generation for a more streamlined development process.
In short, jOOQ's recommendation is to use:
Flyway or Liquibase for database change management
Testcontainers for code generation
Testcontainers for integration testing
Other, equivalent products are obviously also possible.
I’m trying to build an administration portal accessible via web starting from the informations inside a database using Spring Boot with MyBatis for accessing the database. I wanted to find a way to reuse the code that I’m building for the portal with any other type of database, so I was wondering if there was a way to automatically generate classes for my project starting from the informations in the database, for example table names and fields...
Thanks in advance!
Telosys code generator (http://www.telosys.org) does this kind of job.
It uses the database schema to create a lightweight model that is used to generate the code (Java or any other language).
For more information see : https://modeling-languages.com/telosys-tools-the-concept-of-lightweight-model-for-code-generation/
Everything is Open Source (tool and templates)
An option could be Jassd, which generates POJOs, Dao interfaces, and Dao implementation classes from a database. Use what you want from the output and discard the rest, the aim of this project was to not have to manually write getters and setters and have something to start from.
See the documentation and sample output at https://github.com/aforslund/jassd
(Note: I am the author, implemented this library to be able to get plain old Java objects generated and bootstrap som Dao/DaoImplementation classes using plain SQL and JDBCTemplate with Spring Boot).
If it is to switch db and reuse the code, simply changing these values in application.properties should help. If you have MySQL in development environment and postgres in production, that can also be handled. Check the following link for more details
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html
You are lucky! I just release cgV19 at https://github.com/carstenSpraener/cgV19
With it you could implement a model loader which connects to your database and reads the table information. It than provides this information to cgV19 as a meta model.
the next step is to implement a "cartridge" for cgV19 where you can generate java classes from that meta model.
if you try it out please give me feedback on you progress.
If you are going to use mybatis then MyBatis Generator is your best bet:
MyBatis Generator (MBG) is a code generator for MyBatis MyBatis and iBATIS. It will generate code for all versions of MyBatis, and versions of iBATIS after version 2.2.0. It will introspect a database table (or many tables) and will generate artifacts that can be used to access the table(s).
MyBatis Generator will generate:
Java POJOs that match the table structure.
MyBatis/iBATIS Compatible SQL Map XML Files. MBG generates SQL for simple CRUD functions on each table in a configuration.
Java client classes that make appropriate use of the above objects.
I want to use Hibernate 4 to work with the database. I use this configuration to map tables in database with Java class:
<mapping class="test.table1"></mapping>
In my Java class I define compatible fields with the table. But now I want to generate the compatible fields automatically. Is there any tool to do it?
I'm using Java 1.7 and MySQL.
If you just want to generate some Java classes based on the schema defined in your database why don’t you use a code generator designed for this kind of job?
For example you could use Telosys, it’s a very handy tool that can create a “database model” in a few minutes by connecting to an relational DB and then use it to generate any kind of code and especially Java classes.
The advantage is that you can adapt the templates if necessary (and if you want you can generate more than the domain classes).
For more information see the web site http://www.telosys.org/ and read this article https://modeling-languages.com/telosys-tools-the-concept-of-lightweight-model-for-code-generation/
You can use Hibernate Tools Reverse Engineer, which inspects the database and generates the domain model classes from the database table definition.
I am using Hibernate for ORM in my Java application. I want to write custom queries combining multiple tables and using DB functions like sum(salary).
I also want to support multiple databases without writing the SQLs again and again for each database. The approach currently followed
is having Stored Procedures specific to each DB (Oracle, MySQL etc) and whichever we want to support, we change the configuration file in the application.
What I am looking for is a solution very generic so that I need not write Stored Procedures or SQLs for every new functionality.
If you really want to keep it portable, you'll need to do it all with HQL.
There's no reason that you couldn't do multi-table joins and aggregate functions in HQL, you just need to limit yourself to the ones it supports.
Once you start doing database-vendor specific things, you are no longer database independent, by definition.
A perfect suite is HIbernate Criterias
Hibernate provides alternate ways of manipulating objects and in turn data available in RDBMS tables. One of the methods is Criteria API which allows you to build up a criteria query object programmatically where you can apply filtration rules and logical conditions.
http://www.tutorialspoint.com/hibernate/hibernate_criteria_queries.htm
Is there a way to generate domain classes based on Hibernate mapping? I anticipate answers about hbm2java tool, but it is designed to be used in build time. Is it possible to perform such task at runtime?
Short answer: this may be possible, but generally not a good idea.
Hibernate and other ORM libraries are meant to be a bridge between a database and a object oriented domain in your application. If you want to generate your domain during runtime, you would negate the whole reason for having a domain or using Hibernate. Honestly, Hibernate was not designed to do this.
However, if you want a tool to investigate a schema and report back on what tables, columns, etc structure exists I would suggest using regular sql.
As you mentioned, if you want to generate a domain off of a set of .hbm.xml files, use the hbm2java tool and leverage the generated code.