Use #Relate and unique - dart

How can I use Relate and unique using aqueduct ORM?
In my code I want that the userapp be unique?
If I try to put #Column(unique: true) I receive a error like this:
*** Relationship 'userapp' on '_Professional' cannot both have 'Column' and 'Relate' metadata. To add flags for indexing or
nullability to a relationship, see the constructor for 'Relate'.
My code below:
class Professional extends ManagedObject<_Professional> implements _Professional {}
class _Professional {
#primaryKey
int id;
#Relate(#professionals)
Userapp userapp;
#Column(defaultValue: 'true')
bool active;
ManagedSet<ProfessionalSpecialty> professionalSpecialties;
}

Whether the foreign key column underlying userapp is unique or not is determined by the inverse relationship property. In this case, the inverse is Userapp.professionals.
If Userapp.professionals is of type Professional, then a unique constraint is added to userapp; this is a 'has-one' relationship.
If Userapp.professionals is of type ManagedSet<Professional>, no unique constraint is applied; this is a 'has-many' relationship.
I'd guess that because you are using the plural form (professionals) that you are declaring a ManagedSet<Professional>. Change the declaration in the _Userapp table definition and make sure your inverse matches in _Professional:
class _Userapp {
...
Professional professional;
}
class _Professional {
...
#Relate(#professional)
Userapp userapp;
}

Related

hasMany mapping table not created

I am having a GORM issue.
I try to map one domain Object with another with hasMany.
class PrototypePriceModifierCode {
...
static hasMany = [activitys:Activity]
...
}
Since I don't need a back reference in Class Activity I don't have any reference to PrototypePriceModifierCode.
Having only this creates my mapping table as expected (1).
prototype_price_Modifier_code_id activity_id
In the Activity, I need a reference to a PrototypePriceModifier, which has nothing to do with the above mapping table.
The problem is that the mapping table is not generated anymore as soon as I define
class Activity{
...
PrototypePriceModifierCode prototypePriceModifierCodeAttached
How can I get the mapping table created and having a reference to PrototypePriceModifierCode in my Activity domain class?
Try like this:
class Activity {
static belongsTo = [PrototypePriceModifierCode]
}
This way, there will be a column in the activity table for PrototypePriceModifierCode instead of creating a separate table for hasMany.
When Activity does not have the prototypePriceModifierCodeAttached property, the hasMany in PrototypePriceModifierCode results in a uni-directional one-to-many association. In the database, this is implemented with a mapping table.
However, when Activity has the prototypePriceModifierCodeAttached property the association changes to a bi-directional one-to-many. In the database this means the activity table has a foreign key pointing to it's prototype_price_modifierCode, so the mapping table is not used. You can read more about these differences here.
"prototypePriceModifierCodeAttached" property
If you want a uni-directional one-to-many and the property Activity.prototypePriceModifierCodeAttached, you can create a getter method which looks up the PrototypePriceModifierCode:
class Activity {
PrototypePriceModifierCode getPrototypePriceModifierCodeAttached() {
PrototypePriceModifierCode.where {
activitys.id == this.id
}.get()
}
}
The downside here is that the property is inaccessible to GORM; can't query on it.
"price_modifier_code_id" column
On the other hand, if what you want is a price_modifier_code_id column in the activity table, you can add it as a long:
class Activity {
long prototypePriceModifierCodeAttached
static mapping = {
prototypePriceModifierCodeAttached column: 'price_modifier_code_id'
}
}
This makes it possible to use GORM queries on the property, but only on the PrototypePriceModifiedCode ID, not the domain class instance itself.
A combo
You can combine both approaches, as long as you're willing to do a bit of maintenance:
class Activity {
long priceModifierCodeId // <--- You gotta maintain this property manually.
PrototypePriceModifierCode getPrototypePriceModifierCodeAttached() {
PrototypePriceModifierCode.get(priceModifierCodeId)
}
}
Note: activitys is misspelled. It should be activities.
I ended up using String saving comma separated ids.
String activitys
this.activitys.split(',')each{
p.activitys.add(Activity.get(Long.parseLong(it)))
}
As I don't need referencial integrity here this works fine for me.

Polymorphic Expand Breeze Navigation Properties

I have something like this:
public class Person {
string Name;
}
public class Customer : Person {
List<Order> orders;
}
public class MyReference {
Person aPerson;
}
public class Me {
MyReference myRef;
}
Now in my Metamodel I have specified a baseType for Customer. And I think my metamodel is right. The only problem is, that when I want to execute a query like the following:
breeze.EntityQuery.from('Me').expand('myRef, myRef.aPerson, myRef.aPerson.orders')
I get an error, that "orders" is not allowed on the EntityType "Person". Of course as it's the base-class. I would like it to be polymorphic and if the Person is really of Type "Customer" it should expand orders and if not, well then it can be empty or not defined or not even existent on the object.
Is this somehow possible? Would I need some kind of "toType" to cast inside the Query?
The base class, Person, does not have an Orders property. Therefore, .NET (EF) on the server won't let you ask for Person.Orders. That's not how polymorphism works in EF and there isn't anything Breeze can do to change that.
You'll need a different approach I'm afraid.
FWIW, that isn't how polymorphism works in Breeze either.

Putting entries from a list property into a statically mapped joinTable

I have a domain class called FoapRequest. I want one of the properties called "approver" to be a list of integers. Order matters, so I've defined the class as described by http://grails.org/doc/latest/guide/GORM.html#sets,ListsAndMaps as a list:
class FoapRequest {
Integer requester
Integer subject
List approver
static hasMany = [foap:FOAP, newFoap:NewFoap, approver:Integer]
...
Just for clarification, FOAP and NewFoap are two other domain objects.
I need to map this class to a particular table in the Oracle database, so I also specify a static mapping with a join table:
static mapping = {
table 'OUR_SCHEMA.FOAP_REQUEST_TABLE
id column : 'ID', generator:'sequence', params: [sequence:'OUR_SCHEMA.FOAP_REQUEST_SEQ']
requester column : 'REQUESTER'
subject column : 'SUBJECT'
approver indexColumn: [name: "APPROVER_IDX"], generator:'sequence', params: [sequence:'OUR_SCHEMA.APPROVER_SEQ'],
joinTable: [name:'OUR_SCHEMA.APPROVER_TABLE',
key: 'ASSOCIATED_REQUEST',
column: 'APPROVER_PIDM',
type: "integer"
]
However, when I try to create a new instance of the FoapRequest object, I get the following error:
Invalid column type
The console displays the following:
Error 2012-08-01 12:29:31,619 [http-bio-8080-exec-9] ERROR errors.GrailsExceptionResolver - SQLException occurred when processing request: [POST] /FOAPauth/foapRequest/saveFoapRequests - parameters:
I am certain that the issue lies with the jointable. The domain model didn't include the joinTable originally- approver was just an Integer type (I realized too late that I was going to need to track multiple approvers).
Here's the SQL for creating the APPROVERS table:
CREATE TABLE "OUR_SCHEMA"."APPROVER_TABLE"
(
"APPROVER_IDX" NUMBER(*,0) NOT NULL ENABLE,
"ASSOCIATED_REQUEST" NUMBER(*,0) NOT NULL ENABLE,
"APPROVER_PIDM" NUMBER(8),
);
I'd prefer to avoid creating an Approver domain class if at all possible, since all I really need to keep track of are the integer identifiers.
So, after much janking with join tables, I determined that the best way to deal with my needs was to simply create an Approver object in my domain model.
class Approver {
Integer pidm
String approvalDecision
Date lastUpdated
Date dateCreated
static belongsTo = [foap: FOAP]
}
To be honest, I'm not really sure why I was trying so hard to avoid this. Possibly because my DBAs use a version control system for table definitions that I find a hair annoying. :)
Regardless, a simple one-to-many relationship between domain classes met all my needs, no join table required.
For those who are still burning to know, I did manage to get a statically mapped join table working using a Map, which was more appropriate for my needs (though not as appropriate for them as a new domain class, and not nearly as simple).
I ended up doing it in a different domain object- FOAP instead of FoapRequest:
import java.util.Map
class FOAP {
...
Map approvalData
...
static mapping = {
table 'OURSCHEMA.FOAP_TABLE'
id column : 'ID', generator:'jpl.hibernate.util.TriggerAssignedIdentityGenerator'
fund column : 'FUND'
org column : 'ORG'
chartOfAccounts column : 'CHART_OF_ACCOUNTS'
permissionType column: 'PERMISSION_TYPE'
foapRequest column: 'REQUEST_ID'
version column : 'VERSION'
approvalData joinTable: [name:'OURSCHEMA.FOAP_APPROVERS',
key: 'FOAP'
]
}
For the table definition, I used the column names similar to those in my original question.
CREATE TABLE "OUR_SCHEMA"."APPROVER_TABLE"
(
"FOAP" NUMBER(*,0) NOT NULL ENABLE,
"APPROVER_IDX" VARCHAR2(255),
"APPROVER_DLT" NUMBER(8),
);
The IDX column was the map object's key, the DLT column its value. I'd recommend against this approach, for anyone who can avoid it. Creating a new domain object is much simpler.

Spring Data Neo4j - #RelationshipType issues

I'm having difficulties retrieving relationships when the relationship type is annotated with a #RelationshipType field.
The relationships look correct in Neoclipse, but I'm retrieving no results in my application.
The code that doesn't work is (simplified):
#NodeEntity
public abstract class Entity {
#RelatedToVia
private Collection<Relationship> relationships;
public Relationship relatedTo(Entity entity, String type) {
Relationship relationship = new Relationship(type, this, entity);
relationships.add(relationship);
return relationship;
}
...
}
and:
#RelationshipEntity
public class Relationship {
#RelationshipType
private String type;
...
}
The code that does work is:
#RelationshipEntity(type = "something")
public class Relationship {
...
}
However, this doesn't suit my use case (I have a bunch of different Relationship types between arbitrary combinations of Entity instances.
The full test code is below. Agency and Item are both subclasses of Entity.
// Create first entity
Agency arnz = agencyRepository.save(new Agency());
arnz.setCode("ARNZ");
agencyRepository.save(arnz);
// Create second entity
Item r123 = itemRepository.save(new Item());
r123.setCode("R123");
// Create parent/child relationship between entities
r123.relatedTo(arnz, EntityRelationshipType.PARENT);
itemRepository.save(r123);
// Retrieve entity from database
Entity entity = itemRepository.findByCode("R123");
// Verify that relationship is present
assertThat(entity.getRelationships().iterator().hasNext(), is(true));
The final line is where the test is failing. Any clues?
M
PS. I'm a rank amateur with Neo4j and just happened to find #RelationshipType, so I may well be doing something laughably wrong. I hope so!
Sorry to disappoint you, but during the retrieval the code right now doesn't look for the type class but rather for the type from #RelatedToVia or #RelationshipEntity or the field name relationships as relationship-type. But you're making a valid point, can you please raise in issue in JIRA?
Did you look into template.getRelationshipsBetween ?
Why don't you create individual classes for your relationships? What is the use-case for this approach?

Composite foreign key columns in GORM

I need to customize the column names for composite foreign keys in GORM, and I didn't find any document that shows how to do it. I know how to customize PK columns, and how to customize a single-column FK, but not multi-column FK. Is it possible at all?
Thanks.
A domain class with composite id must implement the Serializable interface.
class Person implements Serializable {
...
}
You need the "id: composite" construct in your object mapping closure.
I have to leave for work, so here it is in brief:
class Person {
String firstName
String lastName
static mapping = {
id composite:['firstName', 'lastName']
}
}
Grails: Object Relational Mapping

Resources