Coming from a Java/Hibernate environment, I can map some columns within a table to be a "component" that is belongs to a parent class. For example an User object contains a Preference object, which actually mapped all the attributes into one table in the database.
Is there anything similar in DataMapper? or other database interfacing library?
DataMapper currently doesn't support this feature, but it's on the roadmap and is referred to as "embedded value". ActiveRecord supports defining embedded values already.
As long as DataMapper has no builtin support for embedded values, they can be "faked" by following the pattern outlined in https://gist.github.com/873428
Related
Disclaimer: Please do not suggest database-model redesigns / database model concept flaws. This question is about the case described, there is no way to change that case apprently.
Assuming i hava a table fruits, with name, color and type as fields
This means, all apples have the type=apple, all oranges have type=orange and so on.
Now creating a JPA entity/model called Apple with those 3 fields mapped and the Table=fruits, i want to create a AppleRepository where the argument type is statically set to apple and not visible to the outer consumers.
So i do not want to offer
AppleRepository.findByTypeAndNameContains with requiring the caller to now to set type to apple
but rather just
AppleRepository.findByNameContains while type is statically set to apple.
(With the same method i would then chose to Orange and the OrangeRepository using type=orange)
I understand i could use #Query to write a custom query where type is just statically included and only mapping the dynamic parameters... but
Question:
Is there a elegant way to implement that without using #Query?
What you have is the principle behind the "singe table" inheritance strategy. It consists in storing all the entities in a hierarchy inside a single table, and distinguishing them using a discriminator column (i.e. your type column).
See the documentation for details
Getting my feet wet with Grails, so please bear with me...
Just wondering if this is a good use of transient, or if there's a better way.
say I have
class Author {
String name
String favoriteBook
static transients = ["favoriteBook"]
etc.
"Favorite Book" is the title from the Book table of the a book published by the author. I have a database stored procedure (function) that I want to use to do this lookup. I don't want it persisted to the database, just evaluated on the fly when the Author list (and show) is executed. For the list, ideally it is sortable as well.
Note: using Oracle datbase
This obviously is not my real world example, I am actually working on extending a legacy database, and cannot touch that structure....I have a lot of stored procedures that can be utilized. So the question remains...I want my domain class to pull down a value from the database which is the result of a stored procedure.
I find examples on the web of using transients, but not in this way...am I misusing it? How do I utilize stored procedures within g&g and GORM?
I also find example of using stored procedures, like this one
http://www.craigburke.com/post/44771719252/oracle-stored-procs-in-grails
but missing how to implement this in the views...
tia...
K.
Instead of declaring it as a transient I would map it as a formula
class Author {
String name
String favoriteBook
static mapping = {
favoriteBook formula:'find_favorite_book(id)'
}
(or whatever the required SQL is to call your custom function).
Quoting from the linked documentation
Note that the formula expressed in the ORM DSL is SQL so references to other properties should relate to the persistence model not the object model.
i.e. if you need to refer to other properties in the formula then you have to use the database column names (some_property) rather than the property names (someProperty). If you don't want to have to guess the naming convention then you should consider making the property-to-column mapping explicit for any columns you want to use in the formula
static mapping = {
someProperty column:'my_unusual_column'
}
I'm having a few tables on SQL Server, which have similar structure - int Id and string Value.
This tables linked to main table via foreign key, so I'm wrote a bit of logic for mapping a string values to id's in models in MVC Razor. This feature requires that models used as dictionary implement simple IKeyValue interface with Id and Value, but after updating model from database I can loose interface implementation from models and must write it again.
Any way to automate this?
Are you modifying the auto-generated file? If so, you should not do this, for the exact reason you describe in your question -- it will get overwritten.
All of the classes in the generated file should be partial. You can take advantage of this by creating another class (in a different file, but in the same project), make sure it has the same declaration (and namespace), and have it implement the interface. This way the class will implement the interface, but will not be overwritten the next time you refresh the schema from the database.
I am currently working on a project where we are rewriting software that was originally written in Visual DataFlex and we are changing it to use SQL and rewriting it into a C# client program and a C#/ASP.Net website. The current database for this is really horrible and has just had columns added to table or pipe(|) characters stuck between the cell values when they needed to add new fields. So we have things like a person table with over 200 columns because stuff like 6 lots of (addressline1, addressline2, town, city, country, postcode) columns for storing different addresses (home/postal/accountPostal/ect...).
What we would like to do is restructure the database, but we also need to keep using the current structure so that the original software can still work as well. What I would like to know is would it be possible using Linq to write a DataContext Object Model Class that could sort of interpret the data base structures so that we could continue to use the current database structure, but to the code it could look like we where using the new structure, and then once different modules of the software are rewritten we could change the object model to use the correct data structure???
First of all, since you mention the DataContext I think you're looking at Linq to SQL? I would advice to use the Entity Framework. The Entity Framework has more advanced modeling capabilities that you can use in a scenario as yours. It has the ability to construct for example a type from multiple tables, use inheritance or complex types.
The Entity Framework creates a model for you that consists of three parts.
SSDL which stores how your database looks.
CSDL which stores your model (your objects and the relationships between them)
MSL which tells the Entity Framework how to map from your objects to the database structure.
Using this you can have a legacy database and map this to a Domain Model that's more suited to your needs.
The Entity Framework has the ability to create a starting model from your database (where all tables, columns and associations are mapped) en then you can begin restructuring this model.
These classes are generated as partial so you could extend them by for exampling splitting the database piped fields into separate properties.
Have you also thought about using Views? If possible you could at views to your database that give you a nicer dataschema to work with and then base your model on the views in combination with stored procedures.
Hope this gives you any ideas.
What exactly is an active record? I hear this term used in Ruby post and nhibernate.
Active record was initially a design pattern which involved wrapping database table operations in a class.
In a Rails context, however, it is also the proper name of a particular software component which implements the design pattern in Ruby for object-relational mapping, and the name of its associated module.
Active record is a design pattern where you map each table to a class. This means that each object created from this class represents a row in the table.
http://martinfowler.com/eaaCatalog/activeRecord.html
http://en.wikipedia.org/wiki/Active_record_pattern
It s a Design Pattern.
http://martinfowler.com/eaaCatalog/activeRecord.html