I have a model_A which I am using in various parent models (Parent_1 and Parent_2).
Each parent model has a configuration reference.
The problem I am having is that model_A get the configuration reference from the last parent model I configure.
Is there a way that each refence of model_A inherits the configuration refence from the Parent_X in which it is referenced?
In other words is there any option to set the Source location of the dictionary as "inherit" ?
The solution I did was to define a third dictionary for the reusable components with the common parameters and also linking this dictionary agains the dictionaries from the parent objects. To summarise
Common Dictionary: with common parameters. Reusable models link against this dictionary
Parent Dictionary 1: Has a link to Common + Specific Params
Parent Dictionary 2
Hope it helps to other people facing similar problems
Related
I am trying to write a SessionCustomizer that will translate camelCase field names to under_score. I have found solutions that basically follow the following strategy: loop over the ClassDescriptor objects in Session.getDescriptors().values(), and then loop over the DatabaseMapping objects in ClassDescriptor.getMappings().
The problem is that this only reaches direct attributes of a class, it does nothing to the attributes of an #embeddable class, a list of which is an attribute of the main class through #ElementCollection (and ends up in a separate table).
I believe that such an attribute is encoded as an AggregateCollectionMapping (a subsubsubclass of DatabaseMapping), but I cannot find the list of DatabaseMapping objects that this should in turn have, and which I would like to loop over again.
Eclipse uses AggregateCollectionMapping and AggregateObjectMapping for embedded attribute mappings.
Class descriptor of embedded attribute can be found at rootEntityClassDescriptor#mappings#embeddedMapping#referenceDescriptor. Reference descriptor contains attribute mappings of embedded entity.
I have an object model with a parent -> children relation:
class diagram
The relation is a composition, a parent can have multiple children, a child has always one parent and cannot live without its parent. To persist this data, we serialize it into JSON and use a NoSQL document database (MongoDB) to store it:
{
"id": 123456,
"Attr1": "I'm a parent",
"children": [
{
"Attr1": "I'm a child"
},
{
"Attr1": "I'm another child"
}
]
}
Now we are trying to map this model to an OData EDM. We map the parent objects as an entity type. Because the children cannot live on their own but are rather part of the parent we did not want them as entity type but more like a complex type.
When using OData 2, I am having a hard time, because OData 2 seems to not know about collections of primitive or complex types. As soon as you need to map a "to many" relation, it seems necessary to define the "many"-side of the relation as an entity type in its own right. But when defining the children in my model as an entity type, OData2 requires to be able to address these children on service root level.
So the canonical URL of the following object
httX://host/serviceroot/Parent(1)/Child(2)
really is something like this:
httX://host/serviceroot/Child(1,2)
See documentation of OData2:
"For OData services conformant with the addressing conventions in this section, the canonical form of an absolute URI identifying a single Entry is formed by adding a single path segment to the service root URI. The path segment is made up of the name of the Collection associated with the Entry followed by the key predicate identifying the Entry within the Collection. For example the URIs httX://services.odata.org/OData/OData.svc/Categories(1)/Products(1) and httX://services.odata.org/OData/OData.svc/Products(1) represent the same Entry, but the canonical URI for the Entry is httX://services.odata.org/OData/OData.svc/Products(1)."
http://www.odata.org/documentation/odata-version-2-0/uri-conventions/
For me, OData 2 seems to fit for relational databases, where when objects are connected with a "to many" relation, they are mapped to different tables and every object therefor has its own primary key. In this case, it is easy to address a child object on service root level (httX://host/serviceroot/Child(48346954) (where 48346954 is the DB primary key)) because you can fetch that object directly with SQL means.
However, OData2 seems not to fit for document databases which can store nested object graps as documents (e.g. JSON). Child objects do not need to be independent documents but are rather nested in the parent document. When mapping this to OData and then address a concrete child, you would need to first find the document in which this child is embedded and from there traverse the object graph to the child. A direct fetch of an embedded child is not possible.
What do you think, am I right when stating that OData 2 does not really fit a document oriented database?
When using OData 4, things change a bit, because OData 4 knows about collections of complex types. So may be I can model my children as complex types. Then I have the "to many" relation of parent to its children as a property of the parent with type "complex collection". Would this be the recommendet way to model dependent and nested child objects?
Is OData 4 better suited to map documents from a document oriented database to a REST interface? Has anyone tried it? Or are there more pitfalls I currently do not see and that make OData 4 not a good choice in my case?
I have two entities X and Y with the relation #ManyToMany. X has a list of Y's, let's call it yList. Both X and Y has other class members as well (they are not important).
I am using Hibernate as JPA provider, and jackson-databind / jackson-annotations for things like serialization and deserialization.
Now, the following json is received from the client. It has all the fields of X, but only a list of id's for Y. As a concrete example, X could be Person and Y could be Country. And the many-to-many relation captures which countries have been visited by whom.
{
name: 'Bob Dylan',
age: '74',
visitedCountryIds: ['45', '23', '85']
}
When deserializing this json, I want to populate all the fields of the entity X, including yList, such that the elements of yList are resolved by looking up these entities in the database.
My idea so far is to deserialize yList by writing a custom subclass of JsonDeserializer, and have it perform the lookup by id.
Is this a reasonable approach?
You could use #JsonCreator (as already suggested by Uri Shalit) or just a setter method for your property in which you would do necessary lookups from the database.
However, if you have many entities (and associations) for which you want to do this, then this could be a repeated boilerplate code. Also, if implemented in entity classes directly, it would pollute them with database lookup code (readability, SRP, etc).
If you want some generic approach to this, then I think you are on a good way; custom deserializer is the place to implement it.
If I were to implement the generic approach, I would probably introduce a custom annotation which I would place on the association definition together with standard JPA annotations. For example:
#MyCustomJsonIds("visitedCountryIds")
#ManyToMany(...)
private List<Country> countries;
Then, in the deserializer, I would query for the presence of those annotations to dynamically determine what needs to be looked up from the database.
Another option is to create a constructor that that accepts those parameters, annotate it with #JsonCreator and have the constructor perform the lookup from the database, this way you don't need to write a specific deserializer.
Is there a way to create a dictionary of namespaces from a pyxb binding class?
A dictionary mapping what to what?
The set of all namespaces known to PyXB (which is defined by the set of binding modules that have been imported) can be obtained from pyxb.namespace.Namespace.AvailableNamespaces(). The elements are Namespace instances.
I have two domain classes one is parent and other one is child and i have a hasMany relationship between them. Parent class has many childs and child class belongs to parent class.
And here is coding example.
class Parent{
String name
static hasMany = [childs:Child]
static constraints = {
}
}
class Child{
String name
static belongsTo = [parent:Parent]
static constraints={}
}
Problem is as soon as I get the parent object the child objects associated with parent class were also fetched. But when I convert the object to JSON I don't see the child object completely I can only able to see the ID's of child objects. I want to see all columns of child object instead of only Id.
Converted JSON response:
[{"class":"project.Parent","id":1,
"name":"name1","childs":[{"class":"Child","id":1},{"class":"Review","id":2}]}]
But I want the response which contains name of child object too, as follows
[{"class":"project.Parent","id":1,"name":"name1",
"childs":[{"class":"Child","id":1,"name":"childname1"},
{"class":"Review","id":2,"name":"childname2"}
]
}]
Any help greatly appreciated.
Thanks in advance.
The issue is with the use of default JSON converter. Here are your options:
1. Default - all fields, shallow associations
a. render blah as JSON
2. Global deep converter - change all JSON converters to use deep association traversal
a. grails.converters.json.default.deep = true
3. Named config marshaller using provided or custom converters
a. JSON.createNamedConfig('deep'){
it.registerObjectMarshaller( new DeepDomainClassMarshaller(...) )
}
b. JSON.use('deep'){
render blah as JSON
}
4. Custom Class specific closure marshaller
a. JSON.registerObjectMarshaller(MyClass){ return map of properties}
b. render myClassInstance as JSON
5. Custom controller based closure to generate a map of properties
a. convert(object){
return map of properties
}
b. render convert(blah) as JSON
You are currently using Option 1, which is default.
The simplest you can do is use Option 2 to set global deep converter, but be aware this effects ALL domain classes in your app. Which means that if you have a large tree of associations culminating in a top level object and you try to convert a list of those top level objects the deep converter will execute all of the queries to fetch all of the associated objects and their associated objects in turn. - You could load an entire database in one shot :) Be careful.
The latest grails automatically deep converts but you are probably a victim of lazy loading.
The children are not loaded at access and hence the JSON converter cannot convert them to JSON.
The workaround is to put this
static mapping = { childs lazy: false }
user dbrin is correct, but there's one more option. You could also use the Grails GSON Plugin:
https://github.com/robfletcher/grails-gson#readme
The Plugin adds some more features when dealing with json data.
The suggested solution is working, however I had some trouble referencing "grailsApplication". It turns out, that you can ingest it like any other service. I put the following code into the
BootStrap.groovy
file. Also, the class DeepDomainClassMarshaller handles quite well bidirectional circular references, but beware that the JSON Payload is not to big after all deep deferencation.
package aisnhwr
import grails.converters.JSON
import grails.core.GrailsApplication
import org.grails.web.converters.marshaller.json.DeepDomainClassMarshaller
class BootStrap {
GrailsApplication grailsApplication
def init = { servletContext ->
JSON.createNamedConfig('deep'){
it.registerObjectMarshaller( new DeepDomainClassMarshaller(false, grailsApplication) )
}
}
def destroy = {
}
}