How to use the eclipse Hibernate Tools with grails and MySQL - mysql

I've managed to connect from eclipse Hibernate Tools to my MySql Database used with grails, classes mapped by GORM.
Now I'd like to perform HQL queries on the DB using the Hibernate Tools. However Hibernate Tools tells me for every table that it is not mapped.
My question: Do I really need to write all the class mappings manually into a hibernate.cfg.xml file or is there a way to get it from grails? I mean grails / GORM needs to have an idea about the mappings, right? Or am I going for this the wrong way?
P.S. I know there is a script grails-create-hibernate-cfg-xml, but this only creates a dummy file for some Books class...

Grails has convetions based config so there is no hibernate.cfg.xml but you can easily execute HQL queries int groovy console - just call
grails console
It will open Groovy console connected to your app so you can write groovy scripts interacting with your DB.

Related

Creating the MySQL table from the Java Spring boot #entities / classes, is it even possible?

I want to create my Database (MySQL) using my Java Spring boot #entities & classes defined already with all the required fields and associations. Is it possible to do automatically (By a make migration or migrate command mechanism) ? or do i have to explicitly write the SQL queries e.g the Create Table ones.
PS : tools such as flyway arent of a big help as they require to write SQL code despite defined JAVA Code.
Thank you,
Spring Boot Uses Hibernate under the hood as an ORM mapping tool.
Hibernate indeed has such a feature:
In plain hibernate:
hibernate.hbm2ddl.auto is the parameter that you're looking for: use the create as a value
Other values also can come handy, read this SO thread for more information
Spring boot names this parameter slightly different but the logic behind stays the same
Update 1
*Based on Op's comment:
Unfortunately I don't have any access to code that works with the relational databases. But given Spring boot driven application you probably already have src/main/resources/application.properties (or yaml).
So you should: Read the relevant chapter of spring boot configuration
Then,
In the application.properties (or yml) put
spring.jpa.hibernate.ddl-auto=create
And start the application. It should create the tables during the hibernate startup if everything else is configured correctly.
If

Domain classes and database are out of sync in Grails

I've this weird problem. I have a Grails app in which some database changelog files are missing. Therefore, the database has gone out of sync with the domain classes. I've done some changes in the domain classes. When I try to run the database migration plugin, it is creating a diff betweeb the current domain classes and the database and try to execute all the sql commands that has already been run which is causing error in executing the commands that I want to execute.
Is there is a solution for this problem?
If I understand your problem correctly, you can re-create all of the missing changelogs using dbm-generate-changelog. This will create changelogs based on the current data model. Then you can use dbm-changelog-sync to mark those changelogs as EXECUTED (which will populate the DATABASECHANGELOG table). Once the DATABASECHANGELOG table is in sync with the current data model, you can use dbm-gorm-diff to make sure you're not missing any other data model changes.
https://grails-plugins.github.io/grails-database-migration/1.4.0/ref/Maintenance%20Scripts/dbm-changelog-sync.html
NOTE: My answer assumes you're using Grails 2.x and Database Migration plugin 1.4.x, but I believe the process is similar in Grails 3.x with Database Migration Plugin 2.x or 3.x.

Is it possible to have common entities for both Cassandra(NoSQL) and mysql(RDBMS)

We would like to support two databases MySQL and Cassandra to our application. Our application will be built using Spring. I would to like to have common entities which has to be shared by both MySQL and Cassandra I would like to change the DB dynamically based on customer preference. How can I achieve this
If your requirements to have the system runnable on both data stores, with only one at a time, this I think it is achievable.
The following advices i believe will let you achieve this but i didn't try'em: (supposing that you uses spring-boot)
In maven put the dependency for spring data jpa and spring data mongo.
Enable both jpa and mongo repositories (see EnableJpaRepositoies)
On entities put both JPA and Mongo annotations.
Use PagingAndSortingRepository as base interface for you repositories
In application.properties either configure mysql or mongo db through spring boot well-defined properties
This might work in regular spring apps but will need modifications; also include corresponding driver dependencies in pom file.
Edit: you can use 'spring.data.jpa.repositories.enabled=false' ( and its mongo counterpart) source: https://github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jpa/JpaRepositoriesAutoConfiguration.java

Read existing mysql tables using Grails by generating the domains

I'm trying Grails 3.0.8 for the first time. I'll use it to create web-services for mobile development.
I already have a mysql database with a lot of tables. I found that I can use "db-reverse-engineer:0.5.1" to generate the different domains from the tables. For some reason, I cannot install the plugin and it doesn't work. I think it has something to do with the new version of Grails which is 3.0.8.
As there are not a lot of documentation on this version, I was wondering if there was a way to generate domains from an existing MySQL database.
If not, is it possible to use the database without having to create domains for the tables?
The db-reverse-engineer plugin is for Grails 2. It's not compatible with Grails 3. See Grails 3 reverse engineer database to domain objects
You can run database queries if you get a Hibernate session. You can read about how to get one here.
With a Hibernate session, you can use the Session.createQuery(String) method to create a SQLQuery instance. Then just execute the SQLQuery.list() method to run the query. Here's an example of running an arbitrary query in an H2 database.
def q = session.createSQLQuery 'select * from INFORMATION_SCHEMA.COLUMNS'
q.list() // Runs the query.

Struts- Best way to connect to Mysql in struts 2?

Unable to determine what is the best way to connect to mysql database in struts..
We can always use DriverManger and Class.forName() to connect.
DataSource interface - but this has problems I am getting compliation error for
DataSource dataSource = (DataSource)context.getAttribute(Globals.DATA_SOURCE_KEY);
or when Action.Data_SOURCE_KEY is used. when searched I found that these variables are depricated.
How can I use connection pooling in struts?What is best place to place url,username,pass for database?DO i still have to use datasource configuration in same way in struts-config? Then why was this facility depricated?
Too many queastions but I cannot find a definite source to learn struts.
Struts doc can be but then revisions and backword compatibility are the issues which a learner cannot get easily... Pls suggest a good source to learn struts2.
Struts is an MVC framework, not a database access framework. You should use some sort tool for your Data Access Layer. Spring makes it really easy to manage connections, transactions, and the sort, and integrates well with ORM tools like Hibernate or the JPA implementation.
Where Struts fits in in this is that it will manage the request, delegate to an action, which in turn will invoke a service that uses your data access layer. You could put your DAL in your actions, but I wouldn't -- I would put them in a service.
Struts is a framework having the MVC approach. It makes you to create application in an efficient way. Connection between database is somewhat risk compare to someother connection.