views:

56

answers:

2

Let's say theres a Teacher object and that Teachers holds a list of Courses objects. The courses for the Teacher can change. Some get added, some get deleted. What's the best approach to update this changes in the database. 1. Right after every change, update the database. e.g.: A course get added, immediately add that into the database as well . 2. After all changes are made to the entity/object Teacher (courses are added, courses are deleted), only then update the database with all the changes. 3. Others ??

I can see for both 1 and 2 advantages and disadvantages. For 1: I don't know how good it is when data models have direct access to the database. For 2: The algorithm its more complex because you have to compare the information in the data models with information in the database all at once.

Thank you

+1  A: 

Take a look at some of the ORM tools available for your language or platform. An object's representation of an entity in the database can always get out-of-sync. For example, if a user changed a certain property, and a database update was attempted but for whatever reasons that update failed. Now these two items are not synchronized.

It may also depend on the frequency of updates to your database. If you don't expect massive activity in terms of writes to the database, triggering instant database updates on any property change may be an option.

Otherwise, you may include a dirty flag in your object to indicate when it goes out of sync with the database. Object changes could be updated as they happen, or when a event is triggered such as when the user decides to save their progress, or periodically, say every x minutes.

Different languages and frameworks implement these model objects differently. In Rails, a model object is subclasses from ActiveRecord and knows how to persist itself into a database. In Java, you would rarely mix domain objects with the way they're persisted. That's usually taken care of by an ORM framework or custom DAO objects.

Anurag
A: 

I found out about Hibernate. It's exactly what i need and it's simple.

Blitzkr1eg