In Grails I am creating "domains", i.e. classes with some fields, which are automatically mapped onto database.
Is it possible to achieve the same (or similar) functionality in separate Java or Groovy project?
I wish it to be compatible with separate Grails-controlled database, i.e. for example, to write desktop application to populate database.
Grails mappings are based on Hibernate ORM. hibernate is independent ORM rom map objects to DB. I think it will help you. See Hibernate docs
Related
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 am new to hibernate. Learning from internet source.
As we know, with the help to Hibernate Dialect, we can easily migrate one database to another (for ex. DB2 To MySQL) i.e. only Java part we can migrate.
But how can migrate Stored Procedure and Indexing that are database specific? And can hibernate provide such feature to migrate?
Hibernate is used to store a java class object into database and retrieve it as it is. That is a ORM framework. What hibernate can help you in your DB migration as below:
1. By changing some properties like dialect, connection url, driver class etc will allow you to store/fetch the java class objects into the new DB that you are migrating to.
2. Reverse engineering: If you already have table structures (containing all the relationships between the tables) in your old DB then using reverse engineering you can create the POJO classes for hibernate.
Netbeans has built-in support for reverse engineering.
Hibernate Tool/Plugin can be used for Eclipse IDE to achieve reverse engineering.
Now, the stored procedure, triggers etc. created in your db are not from hibernate. It means hibernate have nothing to do with it. Because, the extra things (procedure, cursor, triggers etc) that are working on your database have some special purpose than a java application to store/manipulate/fetch data.
Hence, you can migrate from old DB to a new one but you won't need hibernate to migrate those extra things. Instead you can use some techniques or procedures like this Migrating Oracle Databases to SQL Server .
This things (stored procedures, triggers migration) can be done at database level and the java application has nothing to do with it.
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 need to add a new datasource to my grails project wich uses the musicbrainz postgresql database. http://musicbrainz.org/doc/MusicBrainz_Database
I found a project on github where the data bindings are ready to use for a spring project:
https://github.com/lastfm/musicbrainz-data
Am i able to use these data bindings in a grails 2.2.3 project? If yes, how can i do this? (because there is no hibernate xml as needed by grails (regarding to the grails documentation: Hibernate Mapped Domain Classes))
I don't think it will be. Just setup the additional datasource and model out the tables or objects you need.
How do you access two databases in Grails
Once you do that, you can use all of the GORM methods and dynamic finders to get your data. Plus validation criteria, transactions, etc. Unless there is some very specialized criteria that make it necessary to bypass GORM, I would suggest leveraging it.
I need to write a new app but using some data acces logic from other two app. One uses Hibernate and the other uses iBATIS. Can I use in the same app both, Hibernate and iBATIS?. How?.
Update: Let me reformule my question. Let's forget I will rehuse some DAOs or domain clases. I need to use in the same app, Hibernate and iBATIS. How can I do that? Thanks for your time...
check appFuse project. it has skeleton implementations for both orms. just combine both of them into the same app. database usually doesn't care what's behind the jdbc driver that accesses it