How to use groovy domain classes in my Java Project? - grails

I have some domain classes in grails application. My middle ware is called as servic
The domain classes would be shared between grails-app and services.
If we use annotation – would services be able to detect them?
How do you share the classes with services ?. Can you do a jar on them?.
Can any one please help me out
Thanks Siva.

You may access your grails domain classes with hql query. See How to execute HQL query in Java for reference.

Related

How to share Domain Codebase between Grails 2 and Grails 3

At the moment, we have multiple grails applications built on grails 2.5.0
Our Model is in a grails plugin (2.5.0), published to a local maven repository. So far everything works great.
Now we want to create a new application with Grails 3, which relies on some of the basic Domains located in a grails2 plugin (User, Group, etc).
What would be the best way, to share those ? I dont want to maintain two code-bases for our model, one for v2 and one for v3 ...
The only thing which came into my mind is, building everything in plain groovy Classes / Interfaces, and then extending / implementing the Model in both grails2 and grails3 plugins
core-model (contains Interfaces, abstract classes)
grails-model-v2
grails-model-v3
You're on the right track. The domain classes will probably work unchanged, but the problem is the grails plugin projects are incompatible. So you can use a plain groovy project to house your domains and then have each plugin depend on the domain project. The tricky part is telling grails that those plain groovy classes are domains. In grails 3 you can probably just apply the groovy traits that grails automatic applies to domains using doWithSpring(). I think grails 2 uses metaClass to accomplish the same thing, so the approach might be similar.

Add behavior (relationships) to grails plugin domain classes from main application?

I have a Grails plugin I've created which is intended to support a number of applications. This plugin has an Employee domain object. The problem is that, when used in the main application, domain objects from that application need to refer back to the Employee object. So, my main application might have an Address which belongsTo the Employee class from the plugin.
How can one handle this properly in Grails 2.5.0?
Thanks in advance.
It looks like that your main and plugin depend on each other. In this case you should add the plugin location of one to another:
grails.plugin.location.<PLUGIN-NAME> = "<PATH TO YOUR PLUGIN>" Ex: "../myPlugin" assuming it is located on the same folder structure as the plugin
and in your plugin
grails.plugin.location.<APP-NAME>= "<PATH TO YOUR APP>" EX: "../myApp"

How to use Grails Domain objects in a Java project?

I have a Grails application with outsourced (in a separate Grails Plugin) domain objects and would like to use the domain objects in a Java application.
If this is possible, how to do that?
Thanks
In order to use them as domain classes, you need GORM-plugin to be installed somehow in your java code. Without the plugin the domain classes lose their "active record" aspect and can be used only as POJOs.
I can barely imagine how to do that w/o lot of pain
You need to define your custom hbm.xml hibernate mapping files. See hibernate doc

How to reuse Grails application's domain classes?

I have already create a Grails application. For some reasons I need to create another console application for someone to modify database data.
Is it possible to package Grails application as a JAR library, so that the console application can reuse those domain classes?
Or, I add/create some classes in Grails application and package it as a JAR and run as console application?
If no better answer, probably I will use the batch-launcher plugin to do that.
You can put the domain classes in a JAR and tell Grails that these are your domain classes by adding a Hibernate XML file (in grails-app/conf/hibernate) that refers to the classes in this JAR. You can use this JAR in any other Java/Groovy application, but obviously they'll only have the persistence methods (dynamic finders, save(), etc.) when used in a Grails app.

What is the best way to work with services in grails

I've just started a project on grails and didn't find how to work with services using dependency injection and interfaces.
As I've seen so far in the documentation when you create a service - it's just a groovy class which can be auto wired wherever I want.
But what if I want to have an interface for a service and to inject one of its implementation like I did in Java using spring?
eg I want to have a service interface. let it be MyService.groovy
it will have 1 method doSmth()
and I'll have 2 implementations - MyServiceImpl1.groovy and MyServiceImpl2.groovy
I have a quartz job doing something like this
def myService
myService.doSmth()
Where should I put groovy interface (folder)? Shall I create a package for that in src/groovy?
How to configure resources.groovy to wire "myService" with 1 of the service implementation?
Any thoughts are appreciated
Thanks in advance!
Running grails create-service [name] is a convenient way of get a service deployed, but it doesn't create an interface with implementation, as you're looking for.
I'd suggest putting your interface and implementations into src/groovy and using resources.groovy to wire them up (you can access the environment, if you want to deploy a different implementation by environment).
Take a look at the 'Using the Spring DSL' section in chapter 14.2 of the user guide for how to wire up your service in resources.groovy. You also have the option of using resources.xml if you want to wire with XML, but I'd definitely recommend the Groovy DSL.
Just run grails create-service [name]

Resources