PDA

View Full Version : object-relational mapping support?



brcain
26th October 2006, 16:28
Hello,

Does Qt provide support for object-relational mapping?

I know there is some support with QSqlTableModel such as: select, insert, update, and delete. But, I don't see support for object relationships such as: association, inheritance, polymorphism, composition, and collections.

Any help greatly appreciated.

Thanks,
Ben

wysota
26th October 2006, 17:16
You mean with databases? Qt only handles relational databases.

brcain
26th October 2006, 20:00
I'm looking for something to glue the model in code to the model in the database ... not just unrelated attributes ... but the attributes of a class object, it's parent object, objects it contains, etc.

This is called object-realational mapping in database world. It's a programming technique that links databases to object-oriented language concepts.
http://en.wikipedia.org/wiki/Object-relational_mapping

As an example, there's a service in Java called Hibernate that performs this for you.
http://www.hibernate.org/

Anyways, I'm hoping to leverage Qt to support as much of this as is possible.

Thanks,
Ben

wysota
26th October 2006, 21:54
If you need an object oriented database, why don't you use an object database management system instead of using a relational database? There are differences between object databases and relational databases, you can't simply "translate" one into another without loosing its functionality. As for the object model - you have the QAbstractItemModel interface which probably has everything you need - parent-child relationships and "properties" (called "roles" in Qt context). You can make a custom model from your database if you wish. QSqlTableModel is of course an implementation of that design, but it's flat - no parent-child relation. If you want one, you have all means to implement it.

jacek
26th October 2006, 22:18
If you need an object oriented database, why don't you use an object database management system instead of using a relational database? There are differences between object databases and relational databases, you can't simply "translate" one into another without loosing its functionality.
He doesn't want to use object oriented database. What he wants is automatically generated classes that will allow him to conveniently access the relational database.

I've never seen anything like this for Qt (note that it doesn't mean it doesn't exist), but I think it should be fairly easy to write a little Perl script that would generate QSqlTableModel subclasses with some accessors methods and enum for column identification.


If you want one, you have all means to implement it.
The idea of object-relational mapping is to avoid writing code by hand.

brcain
26th October 2006, 22:36
What he wants [...] classes that will allow him to conveniently access the relational database..Exactly.


The idea of object-relational mapping is to avoid writing code by hand.
Indeed. It's quite difficult and time consuming.

From my reading there are two alternatives ... runtime reflection or code generation. I don't have a preference for either approach. Although, my experience lends itself more towards runtime intospection (which Qt provides some capability there already).

mm78
26th October 2006, 22:36
Does Qt provide support for object-relational mapping?


Qt doesn't have a persistance framework such as Java's Hibernate (http://www.hibernate.org) if that was what you were thinking of, but it does have a module that provides capabilities for seamless database integration to your Qt applications.

Check out the detailed description of the Qt Sql Module (http://doc.trolltech.com/4.2/qtsql.html#details).

brcain
26th October 2006, 22:43
Qt doesn't have a persistance framework such as Java's Hibernate (http://www.hibernate.org) if that was what you were thinking of

That's what I need ... even if I have to write my own.

The thing that seems missing with QtSQL is how to map the object assocations. I can see how it handles stand-alone attributes. But, I don't think there's any inherent support for object relationships such as aggregation, inheritance, etc.

jacek
26th October 2006, 22:49
From my reading there are two alternatives ... runtime reflection or code generation. I don't have a preference for either approach. Although, my experience lends itself more towards runtime intospection (which Qt provides some capability there already).
You could try to exploit Qt's property system, but you will loose type safety this way, as you will have to pass data as QVariants.

brcain
26th October 2006, 22:56
You could try to exploit Qt's property system.

Yes ... that's my intended approach. And I think that will suffice on the code side.

However, I'm still left with how to map the object relationships :eek:

If I'm terribly concerned about type-safety, then I guess I'm stuck with code generation ... perhaps using template metaprogramming (which I have very limited experience with).

mm78
26th October 2006, 23:02
That's what I need ... even if I have to write my own.

Feel free to write it - you're probably not the only one who want such a framework ;)



The thing that seems missing with QtSQL is how to map the object assocations. I can see how it handles stand-alone attributes. But, I don't think there's any inherent support for object relationships such as aggregation, inheritance, etc.

Correct. You'll be able to connect to databases, run queries and fetch the results quite effortlessly, but Qt won't do magic and handle associations or map the fetched data to objects. It still beats using native drivers though, both in flexibility and complexibility. Anyhow, the native drivers doesn't support associations or OR-mapping either.

I think such an OR-mapping framework would be outside the scope of Qt anyway. To me it sounds more like a framework on top of Qt, just like Hibernate is a framework on top of Java and JDBC.

Do you have any suggestions for how such a framework would work? From the users point of view.

mm78
26th October 2006, 23:11
BTW this is a good article on OR-mapping:
Mapping Objects to Relational Databases: O/R Mapping In Detail (http://www.agiledata.org/essays/mappingObjects.html)

brcain
26th October 2006, 23:19
Feel free to write it - you're probably not the only one who want such a framework
Easier said than done. :eek:



Do you have any suggestions for how such a framework would work? From the users point of view.

I wish I had more experience in this area. First I guess I'd look at the Data Access Object (DAO) design pattern. Also, hopefully I can find enough documentation to get a glimpse at how Hibernate is designed.

Any suggestions are worth gold at this point.

brcain
26th October 2006, 23:33
BTW this is a good article on OR-mapping:
Mapping Objects to Relational Databases: O/R Mapping In Detail (http://www.agiledata.org/essays/mappingObjects.html)
Much thanks ... looks like a great article ... reading now.

Do you know of any C++ toolkits ... that I might integrate with Qt? There appear to be numerous options for Java. :(

mm78
26th October 2006, 23:46
I wish I had more experience in this area. First I guess I'd look at the Data Access Object (DAO) design pattern. Also, hopefully I can find enough documentation to get a glimpse at how Hibernate is designed.
Any suggestions are worth gold at this point.

Do you know of any C++ toolkits ... that I might integrate with Qt? There appear to be numerous options for Java. :(

What do you need this OR-Mapping framework for? I've worked on several applications where we simply handcoded the OR-Mapping because that was the most efficient thing to do at the time. A framework would be better and more flexible of course, but would it really be worth the effort?

Hibernate and other persistence frameworks would be a good starting point to see how this could be implemented, but I think the most important thing at this moment would be to figure out if you really need it and what your requirements are if you really do need it.

Sadly I don't know about any C++ persistance frameworks that would integrate with Qt. Maybe that is what I should build my future "billion dollar business plan" up on? :p

brcain
27th October 2006, 00:30
What do you need this OR-Mapping framework for? I've worked on several applications where we simply handcoded the OR-Mapping because that was the most efficient thing to do at the time. A framework would be better and more flexible of course, but would it really be worth the effort?

That's a loaded question. I'll try to give you a quick summary. I'm devloping a tool to analyze the performance of highly distributed engineering/constructive dynamical simulation systems. It will also provide a "live" simulation exercise monitoring capability. The initial simulation system being used to prototype the tool is a military weapon system.

Besides the obvious analytical components (e.g. object/attribute browser, plots, graphs, query wizard), the tool will also provide a 3D visualization component -- allowing systems to be viewed more naturally.

The tool will provide an open database schema and plugin architecture to support user extensibility of new models and view types.

The most difficult part ... the tool makes as little assumptions as possible about the specific domain of the data being modeled. It will use the database schema to inspect the types of objects being modeled ... their attributes and object associations. As much as possible, I'm trying to keep the domain as open as possible ... not assuming any particular types of dynamic platforms, sensors, constraints, etc.

Here's a little more detail ... hope it's coherent ... it has been a long day :o

Automate analysis and visualization of large, complex, distributed computing simulation systems – while leveraging capital of legacy systems potentially constructed using disparate programming languages and mixed levels of fidelity.

Implement an architecture that leverages existing/open component technologies where possible while enabling organizations to extend/contribute capability back to user-community through an open and extensible plug-in interface mechanism. Component technologies would consist of data collection, relational database querying, and COTS/GOTS charting, 2D/3D maps, plotting, imaging, and analysis software.

n-tier ... distibuted simulations to send messages to a data collector for buffering/translating/writing data to a database. A trigger mechanism would notify tool when an update occurs. This simplifies adapting the tool to various languages/interfaces and allows it to easily support both live and playback modes.

Multiple, distributed tools could be active, querying data from the same or dissimilar scenarios at various points in time.

Within the tool, multiple graphics windows for side-by-side comparison of data from multiple view types to inspect time-dependent behavior.

mm78
28th October 2006, 12:31
It will use the database schema to inspect the types of objects being modeled ... their attributes and object associations. As much as possible, I'm trying to keep the domain as open as possible ... not assuming any particular types of dynamic platforms, sensors, constraints, etc.

I guess this would be a two component tool. First a code generator that runs at compile time to generate classes for your value objects and sql, and second a run time component that create and populate your objects.

If you want to do everything at runtime I guess you'd be better of by using Ruby than C++.

My database knowledge might be a bit dusty, but I'm not sure how inherentance is expressed in the schema. If it is at all. Isn't it just a purely logical concept in the logical data model?

Anyway, Qt won't give you this. It will give you a nice interface to doing platform independent database access, but not this high-level framework. Not even Hibernate would give you what you need (I think).

brcain
30th October 2006, 18:51
If you want to do everything at runtime I guess you'd be better of by using Ruby than C++.

Only problem is I need C++ for graphical plugins (e.g. OpenSceneGraph, OGRE, OpenGL, shaders). I've heard there is now a Java binding for some; but, that's a bit of a jump from where I am now.


My database knowledge might be a bit dusty, but I'm not sure how inherentance is expressed ... Isn't it just a purely logical concept in the logical data model?
Likely. Again, my experience leaves a bit to be desired here. BTW, thanks for ideas.


Anyway, Qt won't give you this. Not even Hibernate would give you what you need (I think).
From Hibernate's description, it purports to handle the logical mapping for several types of class assocations such as inheritance and composition.

If I don't find such a framework, I may have to handle this incrementally ... first writing my own mapping functions.

ufukgun
28th January 2016, 11:42
We developed an open source library named QHibernate.

QHibernate is a Hibernate ORM(Object Relation Mapping) port for C++ and Qt Framework.
Current version is a proof of concept work.
It is used with PostgreSQL 9.4 and Qt 5.4.1.

Features:

Qt5
PostgreSQL
Class mapping xml
Class mapping xml
Hibernate configuration xml
one-to-many, one-to-one, many-to-one mappings



You can find some details, source and download links here:
http://www.cstech.com.tr/en/QHibernate