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.
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.
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
I am working on a Java project for the university and I am trying to learn about framework, technologies and best practices for creating a well structured software that follows software engineering principles. I decided to use Spring (Spring-data-jpa), and Hibernate for the standalone software and now I am having some trouble in understanding how to use the Java Persistence Api to create an abstraction layer above the Hibernate implementation provider. What I am trying to figure out is in which part (configuration file or java class) I can switch from an ORM tool to another one. I saw that I have to use the persistence.xml file to specify the persistence unit and persistence context and also the DB parameters, but it's not clear how the EntityManager bind itself with the underlying ORM tool and in which properties this bind is set. Is the "provider" properties inside persistence.xml file that create this binding? Any link/references/examples or guide will be appreciated, thanks in advance and excuse me for my english ;)
Each persistence unit in he persistence.xml file is associated to a provider. For example, with Hibernate:
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
If you want to use another provider, you'll specify another value for this element.
Note that this is usually unnecessary, because you typically use only one provider in an application. If a single provider is available in the classpath, JPA will use that one. So switching from Hibernate to EclipseLink for example just consists in having the EclipseLink jars in the classpath rather than the Hibernate jars.
Hibernate works on the JPA API so you don't have to explicitly configure JPA if you are using hibernate. The ORM integration happens in the DAO "Data Access Object" layer of your application.
there exists a requirement which sounds quite simple: support a couple of RDBMS (which i intend to do by using JPA) and MongoDB (spring-data-mongodb is preferred) for persistence. More precisely either the one or the other has to be configured and used, i'm not talking about a cross store.
The procedure shall be the following: code the application, deliver the .war to the customer, in a config file the customer puts the persistence information like the databaseurl (i.e. either mongodb:localhost/test or jdbc:oracle:thin:1521#foo).
Additionally it would be nice to extend the implemenation for further datastores like couchdb.
Is there a best practice or at least any of a non-too-much-overhead-solution which is not that dirty?
Is Eclipselink an option? The latest supports JPA for both RDBMS and NOSQL (including Mongo)
https://blogs.oracle.com/theaquarium/entry/jpa_and_nosql_using_eclipselink
I am currently developing a project with similar needs. I can advise you according to my experience.
I believe that the major concern here is not regarding the technology but more regarding how you will structure data. For that I advise you to use the AbstractFactory and FactoryMethod design patterns. Regaring technology I am using Morphia for MongoDB and JPA for MySQL (as an example) and it's working like a charm.
So the easiest way is to create interfaces for all the objects you want to persist, and then do an implementation for MongoDB with Morphia tags and another with JPA tags. Create one factory for MongoDB that will deal with all the CRUD operations in the MongoDB objects and do the same with a JPA factory.
When the application is starting, you only have to verify the user choice for persistence and then initialize the corresponding factory.
DataNucleus JPA allows you to persist to RDBMS, MongoDB and a host of other datastores (LDAP, HBase, AppEngine, Neo4j, etc), with a simple change to the connection URL, and has done so for quite some time
I would like to know if there are any tools to automatically generate EJB3 Entity Beans(for JPA) from a database schema.
Thanks.
Dali supports top-down, bottom-up (this is what you're looking for), and meet-in-the-middle development approaches.
Some of IDs have such feature, for example NetBeans
OpenJPA has a tool which will generate your Entity definitions.
From the OpenJPA user manual:
OpenJPA includes a reverse mapping tool for generating persistent class definitions, complete with metadata, from an existing database schema. You do not have to use the reverse mapping tool to access an existing schema; you are free to write your classes and mappings yourself, as described in Section 3, “ Meet-in-the-Middle Mapping ”. The reverse mapping tool, however, can give you an excellent starting point from which to grow your persistent classes.
No IDE required!
-Rick
I have a solution for you i.e to create auto generate domain objects with all table relationship properly mapped in class ...Try Dal4j yes you can find it in sourceforge.net/p/dal4j/wiki/ DAL4j is a Command Line and Framework tool that can be used to reverse engineer a MySQL or SQLServer database schema into a set of JPA Entity Beans.
DAL4j can be useful for scenarios where there is an existing database schema but a technology other that JPA is used by applications to interact with the database. DAL4j can provide an easy way to migrate your code base from other technologies such as JDBC or Hibernate to JPA.
The beans generated can be 1 or two types: Simple or Framework. Simple beans are standard pojo classes managed by your application using JPA semantics. Framework generated pojos use the DAL4j framework DAO generic to simplify CRUD operations.
DAL4j provides optional hooks to allow you integrate encryption/decryption of data fields that must be encrypted in the database.
Last, DAL4j provides a set of Generic classes that can be used to simplify creation of Session Beans which perform CRUD operations using generated Entities.
I think you will find this article feasible....