Grails 4 how to test if domain class PersistentProperty is an assocation? - grails

In earlier versions of Grails 3 there was a property "GrailsDomainClassProperty.association". I could use this to test whether a property was part of an association. In Grails 4 how do I use PersistentProperty to find this out? Is a property a simple type or an association - one-to-one, one-to-many, etc?

It looks like the answer is to get the PersistentEntity.associations list and check there as to whether an arbitrary property is in fact an assocation. There may be a better answer but that is what I'm going with.

You can check to see if the property is an instance of
org.grails.datastore.mapping.model.types.Association

Related

Java8 and Spring Data Neo4j application queries

I have a bunch of questions regarding Java 8 and SDN4. I created a model in Neo4j v3.0, played a bit with Cypher queries, and now moved to creating a Spring Boot application. As i started coding classes in Java, I've begun to rethink some of my model as well. Here's some questions in my mind (and I haven't found an example explaining this):
Do you need to use Interfaces in Java, with SDN?For eg. I'd code a Product interface and then have my products implement it, but is that how it's done when working with labels?
This is somewhat tied to my question on inheritance - I'd usually have a ProductFamily that my Product would inherit from. At the database level its modeled as (:Product)-[PartOf]->(:ProductFamily), but in the code these would not be super/sub class.
Any examples of using Generics in a graph context?
Is there a way to define constraints on what relationships a node can have and their direction in Java?
I understand there is probably not one right answer, but there's precious little on the web, so hope to get enlightened here!
If you had a Product interface annotated with #NodeEntity, then the you'll have the Product label in addition to the label on your implementing class, which I assume is what you want. If your interface isn't annotated, then your implementing classes will not inherit a label from it.
Not sure what you mean- if you say you have a ProductFamily that Product inherits from, but in the code it would not be a super/sub class?
Based on your graph model, if you want (:Product)-[PartOf]->(:ProductFamily) then you will have a Product class that maintains a reference to a ProductFamily class, and that reference annotated with #Relationship. If the Product class inherits from ProductFamily then persisting the Product will result in two labels- Product and ProductFamily because a Product IS-A ProductFamily.
How do you see yourself using generics- the answer really depends on that. Some cases are supported, some are not (an example of something not supported right now is equivalent of template.createRelationBetween in SDN4)
Yes, via the #Relationship annotation which accepts a type and direction. Note that this annotation only constrains your domain model, but you could very well flout this by creating relationships in another direction via a custom query.

Rails Multi Table Inheritance, Polymorphic association or Single Table Inheritance?

I'm trying to implement the OpenEHR reference model in Rails (ActiveRecord), but I'm finding some problems, since it works with a lot of different of different classess,
Here is the diagram of a Composition:
As you can see, a lot of classes "inherit" a couple of attributes from Locatable or Pathable* (the whole reference is huge, and almost every class inherit from it).
Also, it establish data_types as other classes, for example in the same composition class, language is class CODE_PHRASE, that have two attributes (link).
Therefore I encounter two problems 1) how can I inherit attributes from abstract classes, and 2) how is that I can "include" the needed "classes".
For the first problem I thought in using Polymorphic Associations.
For the second one, I thought using STI, but I'm quickly finding a lot of almost similar models (they are exactly the same actually): CompositionLanguage, CompositionTerritory, EntrySetting, EntryEncoding that I only use in the type attribute to "link back", for example: The composition class, can have up to three attributes with CODE_PHRASE, since all three references a different attribute (language, territory and category), I thought that I needed to know for the associations (there's no point in knowing that Composition has 3 code_phrases, but I didn't know which one is the corresponding attribute). On the other hand, the Entry class, have the setting and encoding attribute (link).
I realize that there could be different approaches, but I would really like to know if that maybe Rails (or ActiveRecord), wasn't made for this. Or, maybe I'm missing conceptual info.
The openEHR RM specification has deeply nested inheritance and composite patterns with tree hierarchy.
I could not implement this nested inheritance by ActiveRecord. The following implementation is an example to simulate openEHR RM.
I would be very happy if this example could help you.
https://github.com/skoba/openehr_rm_rails
Have you looked at this project ..
https://github.com/skoba/openehr-rails
I think Shinji uses Active Record.
Personally, given the complex structure of the openEHR RM, if I was starting out I might look to use something like MongoDB with an ORM.
I have pointed the openehr technical community to your question via the openehr technical list to see if others can help.
Ian

Setting attributes with homemade classes in Rails

I'm quite new at rails and ruby so I'm not even sure if this is necessary, but I'm trying to add attributes to my model that are of one of my homemade classes.
In the tutorials I've read, the attributes are just listed after attr_accessible and the types of those attributes are not defined. My question is: how do I define the type/class of an attribute or do I even have to?
Those types (String, Date, for example) are defined in your database...activerecord will read the attributes from your database.
You can read more here, and can always use the guide

MVC 4 ComplexType properties are not scaffolded

I have a an address complex type as one of the properties of a user. When I scaffold out the domain model, the complex type is skipped in the view. Is there a fix for this?
Thanks!
It turns out that MVC 4 is not capable of scaffolding out complex types yet. We will have to add the fileds in the form manually.
You should be able to scaffold complex types using the Package Manager console.

Grails: how can I query domain class' hasMany-relationship against intersecting set of objects?

I have about the following:
class Object_1 {
static hasMany = [tags:Tag]
Set tags;
...
}
Now I have a set of tags and want to find all Object_1-instances with intersecting (!= matching) tags. I was thinking of something like
Object_1.findAllByTagsInList(tags);
But that does not work at all - I get a "nested exception is org.hibernate.exception.SQLGrammarException: could not execute query". I have the feeling I am missing something important. Help highly appreciated.
I actually found an elegant way to solve the problem. I redesigned the relationship to be many-to-many which allows for simply iterating over the tags list finding all the relevant objects.
... of course now I have to take care of that relationship a couple of times - but I am happy to have this working with few locs.
"in list" is not one of the operators recognized in dynamic finder methods - that won't work.
Instead, you'll have to use HQL or the criteria builder to formulate your query (and perhaps put it in a static finder method).

Resources