2

Is it legitimate to create a one to one relationship between two entities when one is set to be abstract ?

ADude
  • 345
  • 5
  • 14

2 Answers2

3

An abstract entity is not meant to be instantiated. That's why you cannot create such a relationship. What you could do though is to create a relationship where the entity(s) are inheriting from an abstract entity.

From Apple's docs:

A relationship specifies the entity, or the parent entity, of the objects at the destination. This can be the same as the entity at the source (a reflexive relationship). Relationships do not have to be homogeneous. If the Employee entity has two sub-entities, say Manager and Flunky, then a given department's employees may be made up of Employees (assuming Employee is not an abstract entity), Managers, Flunkies, or any combination thereof.

EDIT: Apparently you could create such a relationship (so that child entities would inherit the relationship as well)...

If you define an entity inheritance hierarchy (see “Entity Inheritance”), when you specify a super-entity as the entity for a fetch request, the request returns all matching instances of the super-entity and of sub-entities. In some applications, you might specify a super-entity as being abstract (see “Abstract Entities”). To fetch matching instances of all concrete sub-entities of the abstract entity, you set the entity for fetch specification to be the abstract entity. In the case of the domain described in “Abstract Entities,” if you specify a fetch request with the Graphic entity, the fetch returns matching instances of Circle, TextArea, and Line.

See also this answer: Core Data: Abstract Entity in Fetch Request

Community
  • 1
  • 1
Alladinian
  • 34,483
  • 6
  • 89
  • 91
  • That is misquoted. "assuming Employee is not an abstract entity" is written in parenthesis because if Employee was abstract, it can not appear in the Employees relationship. In fact this paragraph confirms it is possible because it says the Relationships do not have to be homogeneous. I have many situations where I have used an abstract entity as the target in a relationship. – Paul de Lange Jun 26 '12 at 06:11
  • My initial understanding was the same as Alladinian's, though I have also spoken to developers with the same view as Paul de Lange. So if we have an Person (Abstract) with a oneToMany relationship with Employee. This implies an Employee can ask for its Person which would require a Person to be created. This will work as Objective-C won't enforce the class as abstract like Java but is it the correct thing to do ? Why then bother to define Person as abstract? – ADude Jun 26 '12 at 08:17
  • 1
    An Employee has many Person(s)? What a confusing example... Anyway, you need to think about entities and not classes. It is an abstract entity not an abstract class. Your class is just the programmatic representation of the entity. There are no abstract classes in Obj-C unlike Java as you mentioned. It explains in the documents why you would use this feature. – Paul de Lange Jun 26 '12 at 10:05
1

yes. you can have a person who owns a "thing"...

Paul de Lange
  • 10,613
  • 10
  • 41
  • 56