I'm new to Grails and was wondering if there was a way to populate some of my domain classes from web services rather than the DB managed by Grails.
RxGORM for REST is designed for exactly this task:
http://gorm.grails.org/latest/rx/rest-client/manual/index.html
Related
I want to combine both Cassandra and Hibernate data sources in Grails domain classes; some domain classes have to be mapped with Hibernate and others have to be mapped with Cassandra.
I used (static mapWith = "cassandra") in the domain classes but still Cassandra maps all domain classes in the project.
That is currently a limitation with the Cassandra GORM implementation. Even if it won't be used by with Cassandra all domains needs to be mapped to table. I mean to add support for only using some of the domains in Cassandra it isn't yet done.
So currently you would need to create the whole schema in Cassandra as well as MySQL.
Although currently all domain classes will have a Cassandra table created, static mapWith = "cassandra" will only persist a domain to Cassandra only.
Not having any mapWith will result in the domain class being persisted using Hibernate to a database.
See the docs for more information.
If you don't want a class to be persisted at all, move it to src/groovy as detailed in the answer for this question Grails Entity without persist.
I need to integrate a standalone Apache Solr server to my grails application. I know there is a solr plugin for grails, but it embeds the solr server and the plugin don't seem to actively maintained.
My main question is how to index the fields in my domain objects. I want to update the index when I create/update/delete my domain objects. I guess it can be done via a afterInsert/afterUpdate/afterDelete event. But there must be some smarter (=less code in the domain objects) way of doing this?
You can register an hibernate event listener, or use a event publishing/listeningn mechanism like this -> http://grailsrocks.github.io/grails-platform-core/guide/events.html
Then, when your domain objects are changed, just update them in the solr, using solr plugin
I have just implement some entities and some backing beans. However, I would like to know if there is someway to test the domain model, or entities, on the application server which in this case is Glassfish.
For example when I have added a new entity, I would like to test that the persistence is correct by writing and reading the entity and maybe do some operations.
I have used JUnit for standard applications, but now on a web application which is deployed on the application server makes me confused.
What is the standard way to deal with this. I have heard something about JSFUnit but I didn't see any example for Glassfish (maybe it doesnt matter?)
PS. My project involve EJB's, which I assume require either testing under an embedded application server or hosting server?
Can you please help me to understand what is the best practice to deal with this kind of stuff?
Best regards
Preferably you want out of container testing, which is very possible since JPA works outside of container. Just set up a new persistence.xml designed for testing, configured as if you were using Java SE only, and you can test your entities. You will have to set the entitymanager instance yourself thought, since you are not inside your container. Either let your test classes inherit from the EJB you are testing and set the protected EntityManager instance to an instance from your EntityManagerFactory, or add a setter to your EntityManager in each of your DAO EJBs. Then you should be ready to go. You will have to handle your transactions manually though, which should be possible to in the same way for most calls since you will probably want to rollback changes from each test.
Imagine two Grails applications which share a domain class. Maybe a Book domain class.
One application is identified as the owner of the data, one will have to access the domain data. Something like amazon and the amazon web services.
I guess it is trivial that the owning application will use a normal domain class and will expose the data through web services - no problem in grails.
But what would be best practice to implement the domain in the other application?
use a service to access the remote domain and not implement a local domain class at all?
implement a local domain class, overwrite the get()-method in order to fetch the remote data and use the local database as cache?
what other solution comes to your mind?
Ryan Geyer has a very interesting article Modularizing your Grails application domain classes which lists 3 solutions to this problem:
As a RESTful JSON Service - easy to get this setup in Grails but then you lose the automatic GORM functionality.
Separate out the domain classes into a library JAR file and reference that library in both of my other applications. This is not as easy as it first sounds
Create a Grails plugin. Put the domain object in the plugin. Each of your applications can then import this plugin. You can then create different controllers with different functionality as required. The sample code for this is available at:
git clone git://ec2.nslms.com/grails/blog_example_modular
Ted Naleid gives a great tip later in the post and recommends...
"create a grails-app/conf/BuildConfig.groovy file and put the plugin name and path to the source in it. ... If you do this, your applications will see the live changes to your domain/controller/service/etc classes as if they were actually in current app and there isn't any need to repackage and reinstall the plugin when you make changes."
Using memcache should enable both applications to have a consistent view of the data and would avoid each individual application having it's own inconsistent cache.
I think you can make JAR file of your domain classes and add reference to other grails application .
Found another interesting solution:
Riak is a key/value database with a first class REST API. There is a grails plugin for riak which maps most of the GORM functionality (relations, dynamic finders etc) to the REST API of riak: http://grails.org/plugin/riak
Now comes the part which I haven't tested yet: if you make use of the DataSources feature of grails 2.0, it should be possible to only connect those "remote" domains to a riak database.
As a result, there would be a domain stored in a riak database and several applications would be able to access it via a clean REST API without effort.
OK. This also show how silly my question is - it would be the same if you connect several apps through the same SQL-database. But sometimes people want to have something more funky like webservices.
I would like to use neo4j's embedded db on my web app.
Unfortunately I cannot use Spring.
Instead of instantiating a new db on each call, I'd rather inject a singleton.
How do I achieve this on Jetty ?
many thanks,
Not sure what exactly you are looking for, but I think you could inject a Neo4j instance into your servlet as a singleton via the servlets context?