From what I learned in school many years ago, an entity is an actual object in a database. A recordset or a dataset.
This is how I remember it but I may be wrong.
But in many books I read an entity is not an object but the data model, like a class, for the object. When I am in the Core Data - Data Model Editor in Xcode and I click on "Add Entity" I don't add an object to the database but another data model.
So I am confused!
An entity, is it like an object, or like a class I can create objects from?
If you want to become proficient in core data you should learn the associated vocabulary which admittedly might be counter-intuitive at first.
Let me stress that core data is not just a database wrapper but an object graph. Therefore, please take the equivalencies I give here with a grain of salt.
An entity would correspond to a table in a database.
An attribute would correspond to the particular fields in a table.
A relationship (to-one or to-many) would be the presence of a foreign key.
A many-to-many relationship would be a join table with two foreign keys.
One "record" in a database would be an instance of a certain entity.
Note that it is common practice to model entities with corresponding classes which are subclasses of NSManagedObject. Thus instantiation works pretty much like with any other object, only that they are persisted in the database store.
Definitely spend some time on the Core Data Programming Guide.
Related
I am at beginner stage of OOP. Got an academic project, need to make a UML Class Diagram of C# Code.
I am developing a project which will use database, I am confused about 1 thing. In UML we use inheritance like department and student, we create 2 classes and put department ID in student class.
Coming to my confusion, I will make some classes like department, student and teacher, and also a database with same tables. How can I use classes, because I know that on user request I can process data on database (on runtime), Like adding teacher or student to department, getting all students, etc. I am supposed to get all data from database when program loads? and save data in variables and put it in database when required? Simplifying my confusion, How to use classes when we use database to get data dynamically?
What you are talking about is a transition, which is not inheritance. At least not really. Once you have developed a class model you will think about persistence. The persistence can be designed in many ways (in most cases you will derive just one persistence scheme, though). Now what you do is to "copy" your classes from your design model to a new package for the persistence. Thereby <<trace>> from the persistence back to the design class. Now you almost independently optimize your persistence towards your desired schema. You will introduce primary and foreign keys, redundancy and things like that. Anyhow, your persistence model only remembers where it came from (via the <<trace>>) but it now has a living on his own.
Note that some UML tools offer automated transition from design models to various derivates.
How do you solve a situation when you have multiple representations of same object, depending on a view?
For example, lets say you have a book store. Within a book store, you have 2 main representations of Books:
In Lists (search results, browse by category, author, etc...): This is a compact representation that might have some aggregates like for example NumberOfAuthors and NumberOfRwviews. Each Author and Review are entities themselves saved in db.
DetailsView: here you wouldn't have aggregates but real values for each Author, as Book has a property AuthorsList.
Case 2 is clear, you get all from DB and show it. But how to solve case 1. if you want to reduce number of connections and payload to/from DB? So, if you don't want to get all actual Authors and Reviews from DB but just 2 ints for count for each of them.
Full normalized solution would be 2, but 1 seems to require either some denormalization or create 2 different entities: BookDetails and BookCompact within Business Layer.
Important: I am not talking about View DTOs, but actually getting data from DB which doesn't fit into Business Layer Book class.
For me it sounds like multiple Query Models (QM).
I used DDD with CQRS/ES style, so aggregate roots are producing events based on commands being passed in. To those events multiple QMs are subscribed. So I create multiple "views" based on requirements.
The ES (event-sourcing) has huge power - I can introduce another QMs later by replaying stored events.
Sounds like managing a lot of similar, or even duplicate data, but it has sense for me.
QMs can and are optimized to contain just enough data/structure/indexes for given purpose. This is the way out of "shared data model". I see the huge evil in "RDMS" one for all approach. You will always get lost in complexity of managing shared model - like you do.
I had a very good result with the following design:
domain package contains #Entity classes which contain all necessary data which are stored in database
dto package which contains view/views of entity which will be returned from service
Dto should have constructor which takes entity as parameter. To copy data easier you can use BeanUtils.copyProperties(domainClass, dtoClass);
By doing this you are sharing only minimal amount of information and it is returned in object which does not have any functionality.
I'm very new to the core data programming. I understand that the entities are tables, and I'd like to do the following:
One table, "Record" has an attribute "recordID" in another table ("Event") I have a series of events associated with this record, they all also have an attribute "recordID". There's a one entry in the "Record" table to many "Events" relationship between these tables, linked by "recordID".
I would like to know how to use the GUI "Fetched Properties" in the Data Model Editor to retrieve an array of "event" for the record's current recordID.
I checked the predicate programming guide, and it mentions that I can do something like this:
[NSPredicate predicateWithFormat:#"anAttribute == %#", [NSNumber numberWithBool:aBool]];
So in the GUI, I was thinking of using
recordID == recordID
How do I differentiate between different tables ? do I say self.recordID = recordID or something?
Edit:
Unless I completely misunderstand what the relationships are for. Do relationships automatically link tables for me?
First, Core Data is not a database. Core Data is an object graph that can persist to a sqlite file.
Second, Core Data handles the relationships for you. Just create a relationship between the two entities, set the Record instances as the "parent" to the Event instance and the relationship will be created for you.
You do not need to create foreign keys yourself. Core Data will manage them and keep the referential integrity for you.
Core data is not a database and so I am getting confused as to how to create, manage or even implement Lookup tables in core data.
Here is a specific example that relates to my project.
Staff (1) -> (Many) Talents (1)
The talents table consists of:
TalentSkillName (String)
TalentSkillLevel (int)
But I do not want to keep entering the TalentSkillName, so I want to put this information into another, separate table/entity.
But as Core Data is not really a database, I'm getting confused as to what the relationships should look like, or even if Lookup tables should even be stored in core data.
One solution I'm thinking of is to use a PLIST of all the TalentSkillNames and then in the Talents entity simply have a numeric value which points to the PLIST version.
Thanks.
I've added a diagram which I believe is what you're meant to do, but I am unsure if this is correct.
I'd suggest that you have a third entity, Skill. This can have a one to many relationship with Talent, which then just has the level as an attribute.
Effectively, this means you are modelling a many-to-many relationship between Staff and Talent through the Skill entity. Logically, that seems to fit with the situation you're describing.
I've got a data model where there is a Person entity, which has a transformable attribute which is an array of dictionaries containing information. The model is much bigger than that, this is just the part I'm having trouble with. It was designed this way by an old developer, and in taking over the project I need to migrate this to be 100% core data.
So what I need to do is create a new entity, then step through each dictionary in the Person's array and create new instances of that entity with the information from that dictionary. I thought I could use an NSEntityMigrationPolicy to set up a custom migration for this new Entity, but it seems the Core Data migration is expecting X number of source entities to translate to X number of destination entities. Because I technically have 0 source entities right now (because they're in an array that Core Data doesn't really know anything about), I'm not sure how I can make the migration create new entities during the process.
What, or rather where in the migration procedure, is the best way to do what I'm trying to accomplish? I've always used lightweight migration in the past, so this is my first adventure in custom migration.
It would help to have a sense of your data model (schema) - but let's assume that your Person entity now holds home address and list of favorite restaurants. And let's further assume that you will be creating new entities Address and Restaurant along with the following relationships:
Person has one Address, so there's a to-one relationship from Person to Address called "homeAddress". There's an inverse to-many relationship from Address to Person, because many people could live at the same address.
Person has a to-many relationship (called restaurants) to Restaurants. Restaurant could also has a to-many relationship to Person (though this might be one of those cases where bidirectionality doesn't really make sense).
Anyway, the point is that now - in addition to your PersonToPerson NSEntityMigrationPolicy subclass, you will also have PersonToAddress and PersonToRestaurant. These will be the places that you unpack the old data and use it to instantiate and initialize new Address and Restaurant objects.
Of course, there are lots of other complicating issues. For example, you won't want to be creating a new instance of the same Restaurant for every Person who likes it. You will need to keep track of newly created Restaurants.
You will want to order your mappings strategically - probably with PersonToPerson first.
You might want to look at Marcus Zarra's Core Data sample code and maybe even buy his book.