I have an object MainObject which is related to around 20 other objects. These 20 other objects - RelatedObject1, RelatedObject2, etc. The relationship with the MainObject is defined as follows.
1 instance MainObject - > N instances of RelatedObject_1
1 instance MainObject - > N instances of RelatedObject_2
1 instance MainObject - > N instances of RelatedObject_3
.
.
.
1 instance MainObject -> N instances of RelatedObject_20.
Now all the relationship here is "HAS - A" relationship and not a "IS-A" relationship. It is not a parent-child relationship.
The RelatedObjects have their independent transactional functional flow in the system.
Now should I create Bags/Sets/Lists/ for these 20 related Objects in my MainObject.hbm.xml.
for accessing their data from the MainObject.
If I create a bag/set/list - I want to ensure that data is not Saved/Persisted in Database when I Save or Update the MainObject. Using which property makes sense in this scenario.
use
bag when unordered and possibly duplicates
set when unordered and no duplicates
list when ordered and possibly duplicates
and set cascade="none" to prevent cascading any operation (save, update, ...)
Related
I have two sets of entities, entities1 and entities2 and I want to identify all relations that link the two together, from a specified set of relations.
This can be done by declaring the relation as a variable:
?entities1 ?relations ?entities2
However, this iterates over all possible relations, therefore making the query incredibly slow and inefficient.
This could also be done by declaring the relations needed using a OR operator:
?entities1 (relation1 | relation2 | ... | relation_n) ?entities2
This finds all of the cases where entities 1 and 2 are linked by one of our relations but the relation is not returned, therefore it's not known which relation was correct.
I need a method of finding the relations between each entity from a specified set of relations that can then be returned as a table with the headers (entity1, entity2, relation).
Any ideas?
#UninformedUser kindly pointed out that a set of relations can be declared by using VALUES meaning that the desired effect can be achieved as follows:
VALUES ?relation {:relation1 :relation2 ... :relation_n} ?entities1 ?relation ?entities2 .
Thanks for your help!
Is it bad DB design to have an associative entity with multiple foreign keys?
In a scenario such as;
1 Truck is managed by 1 Team which goes to 1 particular route and has 1 Manager..etc
an associatve entity would be ideal in the above, is this bad design principle to do so?
First off an associative entity is needed for many-to-many relationships, so you need to use 0,1 or many in your description as opposed to 1 truck managed by 1 manager.
Secondly, associative entities have multiple foreign keys (one for every entity they have are connected with). An associative entity is essentially a RELATIONSHIP where both sides have a many carnality:
e.g. 0 or many trucks are managed by zero or many teams.
TEAM (TEAM_ID)
TRUCK (TRUCK_ID)
TRICK_MANAGEMENT_TEAMS (TEAM_ID, TRUCK_ID)
I have 3 entities like Member, List and ListMember. Member and List have many-to-many relationship, as a Member can be added to multiple Lists and naturally a list have multiple Members added to itself. I'm trying to keep the related records of Members and List in the ListMember entity. That is, when a Member is added to a List, then there must be a record created in ListMember entity with the Member and List.
My first question is, is there any automated way to do this, that is, can I define ListMember entity as a many-to-many relationship keeper or something like that?
Second question is, if there isn't such a way, how can I trigger a process which creates records with the Member and List in the ListMember entity each time a Member is added into a List, and how can I reach data from both List and the Member in the process?
For more info about the problem, here is my previous question which reduced the situation into this triggering thing:
CRM Dynamics How to set short list - long list relationship
You have 2 basic options when it comes to creating a M:M relationship in CRM:
Add the relationship and defining it as M:M will create your ListMemeber Entity for you, and adding a List to a member, or a member to a list will be populated in the ListMember Entity.
Roll your own Entity that basically does the same thing. You can also auto-populate fields on the form by looking up the regardingojectid
regObj = Xrm.Page.getAttribute("regardingobjectid").getValue();
The regardingobjectid will return the referencing entity that you came from when creating a new entity. This will allow you to populate the side of the relationship that you came from, so the user just has to select the other entity.
It sounds like you want option 1, so I won't go anymore in-depth on Option 2.
I'm wondering if there's a general term used for objects that map exactly to data tables? E.g., a user and an article objects could map directly to user and article tables in a db, with each db field corresponding to a class variable...
They are referred to as Entities in JPA specification.
They are usually called entities, but entities in general don't need to map 1:1 to DB tables. However, what you describe is known as Active Record pattern.
Also, please note that there is very rarely an exact 1:1 mapping between object model and DB:
many-to-many relationships are usually implemented with third table in the DB but are usually mapped to only 2 classes with direct associations in the object model (if relation doesn't have additional attributes)
class inheritance can be modeled in 3 different ways in the DB with 1, N or N + 1 tables
ternary relationships use 3 tables in DB, but can be modeled with parameterized properties in the object model
I have an issue with single table inheritance and I'm not sure if I'm interpreting the documentation correctly.
First: I've not copied my code / entity mappings verbosely (or even using the correct syntax) here as I think the problem can be better communicated abstractly.
If this is not acceptable by all means say so and I'll add the full code - but it is LONG and I think my question can be answered without it.
If it helps I can draw
an ER diagram to try and communicate what I'm trying to do.
If you read the following and think 'hey that should work' - then tell me and I'll upload the real code
Second: I don't use lazy loading anywhere. Before accessing my entities I make sure that I load every related entity that I'm going to be accessing by writing DQL up front - so the following issue is fairly terminal to my application)
The Setup:
I have some entities - these make up the core of my application.
// #entity AnimalTrainingSchool
// oneToMany: trainingDepartment
fields: [name / location / year founded]
// #entity trainingDepartment
oneToMany: animalTrainer
oneToOne: building
fields: [capacity]
// #entity animalTrainer
fields: [name / age / qualification]
I access them frequently and in different contexts - but I commonly iterate though levels and access properties and relations for these entities.
foreach ($animalTrainingSchool as $school){
echo $school->getName() . ' at ' . $school->getLocation();
echo 'These are the training departments for this school:';
foreach ($school->getTrainingDepartments as $trainingDepartment){
echo $trainingDepartment->getAnimalTypeTrained() . ' are trained in this department';
}
}
I make sure that all of these are loaded up front by forming my DQL and executing it - to limit the number of SQL queries (to one).
This all works great (fairly standard stuff).
DQL Example : "SELECT ats, td, at FROM AnimalTrainingSchool ats JOIN AnimalTrainingSchool.trainingDepartments td JOIN td.animalTrainer at";
This sets up my collection and means that I can traverse it without having to issue additional queries
The Problem:
I have mapped other entites elsewhere in my application - very similarly to this (NOTE: My overall question is very similar to the below question with one MAJOR difference (see below)
Doctrine 2 Inheritance Mapping with Association
// NB: new awards are issued each time they are awarded - so it is meant to be a oneToOne relationships - the awards are unique
// #entity award
{id information / table information / discriminator map / inheritance type (SINGLE_TABLE)}
fields: [medalMaterial / award_serial_number]
//departmentAward extends award
oneToOne: trainingDepartment
//trainerAward extends award
oneToOne: animalTrainer
then I made the relationship bidirectional by modifying my initial entities
// #entity trainingDepartment
oneToMany: animalTrainer
oneToOne: building
oneToOne: departmentAward
fields: [capacity]
// #entity animalTrainer
fields: [name / age / qualification]
oneToOne: trainerAward
What Happens
Now when I access my original entities in exactly the same way as above - they automatically (eagerly) load the associated entity for their awards though I'm not telling them to.
This is especially bad when I'm iterating though a whole bunch of trainingDepartments / AnimalTrainers and Doctrine is executing an SQL statement for EVERY entity.
For 20 departments with 10 trainers in each - this is 200 additional queries.
//Given the exact same DQL and foreach loop as above (note that at no stage am I accessing awards) - I get a ton of extra queries that look like this
"SELECT award.property1, award.property2, award.property3 FROM awardTable LEFT JOIN trainingDepartmentTable ON award.awardee_id = trainingDepartmentTable.id and award.discriminatorColumn IN ('departmentAward')";
// or...
"SELECT award.property1, award.property2, award.property3 FROM awardTable LEFT JOIN animalTrainerTable ON award.awardee_id = animalTrainerTable.id and award.discriminatorColumn IN ('trainerAward')";
None of what is being generated is incorrect - it's just that having read the following question it seems to me like I have set this up as the documentation describes (and in the opposite way to #Matthieu; namely - that If I related my initial 3 entites to the LOWEST level entities
rather than the 'award' base class then they SHOULD be able to use proxies instead of attempting to eagerly load the entities.
Stackoverflow Question which is asking the opposite of what I am describing
Doctrine 2 Inheritance Mapping with Association
Relevant Doctrine Documentation
http://www.doctrine-project.org/docs/orm/2.0/en/reference/inheritance-mapping.html#performance-impact
There is a general performance
consideration with Single Table
Inheritance: If you use a STI entity
as a many-to-one or one-to-one entity
you should never use one of the
classes at the upper levels of the
inheritance hierachy as
“targetEntity”, only those that have
no subclasses. Otherwise Doctrine
CANNOT create proxy instances of this
entity and will ALWAYS load the entity
eagerly.
It seems to me that regardless of whether or not you are joining to the base level entity or a subclassed entity - Doctrine will eagerly load the associations and will not attempt to use proxies.
Again: I can post real code - but given the length of the question already I felt it was best not to. Any input greatly appreciated.
The inverse side of a oneToOne relationship can not be lazy loaded. In order to support lazy loading, Doctrine needs to create a proxy object, but proxy objects need to have an identifier associated with them. In the case of oneToOne relationships, the identifier is only available on the owning side. So the inverse relationship has to be loaded eagerly.
You should try to fetch join these associations if possible. Version 2.1 will automatically force fetch joins for inverse oneToOne relationships.