This is more of a design question so I know I'll receive a lot of flak for this but I am a little confused.
In Dropwizard we use DAO objects to declare the SQL queries we want to run and Mappers for mapping them to the objects.
Existing structure :
- StudentPOJO
- StudentMapper for all StudentPOJO fields
- StudentDAO which has all the "select * from student where ..." calls.
Change -
- StudentDAO now has to have a query that says "Selct a.*, b.name from student a, subjects b where ...."
- Now to get this information I need to
- make changes to both Mapper and POJO
OR
- create new Mapper and POJO (or extend the existing POJO and add the new params to the child POJO)
What approach is the usual norm for such cases and what are the pros and cons for the same?
Related
I know union is a SQL construct, but it's the best analogue for what I'm trying to do.
I have multiple groups of data that I'm receiving from an external source. I'm maintaining them as separate entities in Core Data (they only have some attributes in common (e.g. name)), but I want to present them in the same tableView.
Say I have an entity Food that has relationships with FruitGroup and VegetableGroup. The FruitGroup has a relationship with Fruit which has a relationship with FruitType. The VegetableGroup is similar.
How can I use FruitGroup.Fruit.name and VegetableGroup.Vegetable.name as sectionTitles? And FruitGroup.Fruit.FruitType.name and VegetableGroup.Vegetable.VegetableType.name for row data. (I tried coming up with a predicate that walks down from Food, but that doesn't appear to be workable)
Example modeled data (my groups are far more disparate than fruits and veggies, so re-doing my data model is not an option):
Food
FruitGroup
Apple
Macintosh
Granny Smith
Pear
Bartlett
Asian
Anjou
VegetableGroup
Asparagus
white
wild
Peas
baby
split
Which I would like to appear as:
Apple [section]
Macintosh [row]
Granny Smith
Pear
Bartlett
Asian
Anjou
Asparagus
white
wild
Peas
baby
split
I could use multiple NSFetchedResultsControllers in the UITableViewController and conditionally select the FRC within each of the UITableViewDataSource methods, but that doesn't feel clean.
I'm thinking about subclassing NSFetchedResultsController and, internal to my subclass, merging the results of multiple private NSFetchedResultsControllers that each represent one of the entities. (e.g. sections returns a concatenation of the returns from the sections calls of the internal FRCs)
Does this make sense - or is there a better way? (I saw Core Data Union Query Equivalent but since there are relationships among my entities, I wanted to seek alternatives)
While you can do this as described in the other answers (via creating an abstract Parent entity), I would not recommend it. The performance when it comes to dealing with abstract parents gets bad very quickly. The reason for this is that Core Data will put all of the children into a single table in the underlying SQLite file.
I would suggest going a different route. Have a single entity called Food with attributes describing if it is a vegetable or fruit. Then you have one NSFetchedResultsController which has the type of the food item as the sectionPath and you will get your display the way that you want it.
I recommend creating entities in Core Data based on what the objects are as a very loose level. I would not create entities for Honda, Ford and Dodge, but create an entity for Car and perhaps type or a relationship to a manufacturer.
While Core Data can be backed by a database, at the end of the day it is not a database but an object graph and should be treated as such. Trying to normalize the database will result in poor performance of the object graph.
You should probably look into abstract entities. For example, you could create an abstract entity called Food. Then you're able to create Fruit and Vegetables, which inherits the abstract entity. You'll have to set Food as the "Parent Entity".
Then you could fetch all the items with the entity Food, which includes both Fruit and Vegetables. Based on your post, you'll probably will have a relation from Food to FoodGroup.
To answer your question:
You cannot unify different entity types (if they are not subclasses of the same entity) under a single fetch request. You can define an entity (B) to inherit from another entity (A) and then fetch by the parent entity (A) and get both kind of entities (As and Bs)
You can try and think of it this way:
Item ("Macintosh","White Asparagus",...) has a relationship to Group ("Apple","Asparagus",...), and Group has a relationship to Area (or simply to another parent group).
In this manner you could use a single FRC with sectionNameKeyPath of "group.name" and entity Item (you can filter by "group.area" to only select food items).
I'm starting to write a project and its my first time using ASP.NET MVC and Entity Framework (since now I've used PHP for around 5 years).
I got categories and posts, each category has to have its own unique filters that can be strings or Booleans (I'll get them via textboxes or checkboxes). I'm starting to get confused when designing the entities.
I'm using code-first approach but don't know how to set-up custom fields in Entity Framework. If I'd design that in PHP and pure SQL, I think that in order to keep it perform good, I'd create extra columns on the fly (e.g. "filter_1", "filter_2") and then create mapping table that contains the description of the field, the type, etc. and I didn't figure out if this kind of implementation is possible in EF.
I've thought about some options:
- I can create the filter using many-to-many relationships when creating a filters and filters-values tables and when creating a post add the filter values into that table. The main con about it is he performance - I'll most likely have 40k+ rows and more than 20 custom filters for sure... so searching and data fetching will be too slow...
- I thought about serializing the filtered values into some form of content, for example into bytes array and then only desterilize it - but the problem is that I won't be able to search within that...
- I can use the traditional ADO.NET way in order to create my initial idea (that I've described when talking about PHP - using the "filter_N" columns) but that'd create too much mess with EF...
There's any other way to achieve my goal (to create custom filters)? any way to create custom fields using EF?
Thanks for your help!
I don't know of a good way offhand to implement this in the Entity Framework. If you really wanted to use the Entity Framework I believe you could design your database tables in such a way that you wouldn't have to create the extra columns on the fly. You could have separate tables to hold the filters and relate them back to categories.
In Grails we define domain classes in such a way that clearly indicates the relationship between domain classes such as one to many or belongsTo (if any). Since Grails is based on DRY, does that mean we do not need to use the join keyword when performing complex HQL queries in Grails DomainClass.ExecuteQuery method?
Grails doesn't change the way you write your HQL it is the same whether your using Grails domain classes or POJO's. If you need to write queries that navigate the object graph then you need to use the same syntax as you would with HQL like FROM parent p JOIN p.child c WHERE c.age = 10. Criteria queries work in the same way but you just get to use closures.
Take a look at the documentation (section 5.4.3) for more information.
In general no, you don't need explicit joins because Hibernate knows about the relationships of the tables based on the relationships of the domain classes. One exception is collections, and it is possible to use joins to customize the default behavior. The best resource on HQL is the Hibernate documentation itself: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html
I have a MySQL database and would like to have a similar structure in Core Data. I am very new with using Core Data with Xcode. I have a few fundamental questions if I am doing the right thing.
My Mysql DB looks similar to this:
table.caveconditions
visibilityID
percolationID
xxxx
table.visibility
visibilityID
visibilityValue
...and so on. I would then connect the tables using JOINS
Now, I have done the Core Data modeling like this but I am not quite sure if this is the right approach.
Would be great if someone of you could tell me if this is the right way to do it. In the end I would like to use JSON strings to dump the mysql table into core data.
Thanks a lot
Chris
I have created the new schema. Is this right?
It looks good except for all the "xxxID" attributes e.g. caveID. You also need to follow the naming conventions.
You have the same attribute names with (presumably) the same values in two or more entities. This is necessary in SQL for joins but in Core Data, this is handled by objects and relationships.
Each object in Core Data is automatically universally unique. This means when you create a relationship from one object to another, that relationship concrete identifies on specific unique object.
This means you only need an attribute like caveID in the actual entity that caveID designates which in this case is (presumably) the Caves entity. You don't need the attribute in the CavesConditions entity or any other entity that has a relationship to the "Caves" entity.
(If the xxxID were just artifacts of SQL, you don't actually need them at in Core Data unless some external database your app interacts with requires them.)
A good rule of thumb to use is that any particular value should show up on only one side of a relationship and, ideally, only once in the entire data model.
Naming conventions are a little different than SQL. A Core Data entity isn't a table. An entity is more akin to a class. Each entity is supposed to describe a single instance of a managed object. How many of those instances end up in the object graph is irrelevant. Therefore, entity names are singular.
In this case, Caves should be Cave, Countries should be Country and so on.
Relationships are named after the entity they target. It is not immediate obvious but each reciprocal relationship (the default) on the visual data model editor is actually two relationships because there is one relationship description for each side. Each side has the name of the entity targeted. By convention to-one relationships have a singular name and a to-many relationship has a plural name.
So:
Caves.relConditions<-->>CaveConditons.getCave
...would become
Cave.conditons<-->>CaveConditon.cave
The naming conventions are important because Objective-C uses conventions names to generate and search for accessor methods.
CoreData is NOT a database. Remodel your data as simply as you can and in a way that suits how it will be used in your application and do not think about joins or structure based optimization. You do not have control over the backing schema of a CoreData object model. This is the hardest concept you must get over when starting to use CoreData, but once you do, you will be better off.
I want to create a hierarchical object model in ASP.NET MVC, but I'm not sure what would be the best way to design database for this. I have a Product base class with certain properties like Title, Price, OnHandQty etc. I have several inherited classes like Book, which has extra properties like ISBN number, Author etc. Many of my products will fall under generic (base) Product class, but some products will fall under these derived classes (e.g. Book). I am not sure what is the best methodology to map this to database. Should I create separate tables for each product type (including one for generic product)? Or is there any better way?
Please note that I'm not really asking about OR mapping. I know how to create classes from DB tables using Entity Framework. But in this case I am confused about the database design itself.
If you are going to use Entity Framework then you should check out Inheritance with EF Code First by mortezam. He explains three strategies that can be used for representing an inheritance hierarchy:
Table per Hierarchy (TPH): Enable
polymorphism by denormalizing the
SQL schema, and utilize a type
discriminator column that holds type
information.
Table per Type (TPT): Represent "is
a" (inheritance) relationships as
"has a" (foreign key) relationships.
Table per Concrete class (TPC):
Discard polymorphism and inheritance
relationships completely from the
SQL schema.
The idea (with Code First) is that you define your classes and inheritance and let the framework create the database for you. That way you don't need to worry so much about the database design.
You might also want to think about using an Object Database or one of the NoSQL storage strategies like Mongo DB which work better than relational databases when you have these kind of 'jagged' classes.