Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the best practice to store an object graph?

 
Tong Wang
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an object graph (created at runtime as a result of user input) associated with an entity. I don't need to map this object graph to/from DB. I just need to be able to save/load it to/from DB with the entity. So, what is the best practice to store it? I appreciate if someone could shed a light on this.
 
Javid Jamae
Author
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean when you say you don't need to "map this object graph to/from the DB" the object graph, but that you need to "save/load it to/from the DB"? That seems contradictory.

Do you already have a database schema defined? What tool are you using for ORM?
 
Tong Wang
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the confusion. Here is a short code snippet that helps to explain this:



I have an entity Promotion, which has 3 simple fields (id, name, description) and all 3 will be mapped to DB. So far so good. Problem is with the 4th field, ruleModel, which is an instance of RuleModel and when constructed at runtime, it will contain object instances of a dozen other classes. I don't want to map RuleModel to a DB table, because that is a lot extra work and also unnecessary. I just want to store the ruleModel object instance into DB and then be able to load ruleModel from DB and restore the object instance in memory.
 
Javid Jamae
Author
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I'm still not understanding. Are you saying that you don't want a separate table for the RuleModel? If you want the RuleModel to be persisted in the same table as your Promotion, then you can use @Embedded/@Embeddable annotations.

That being said, the decision on whether an entity is mapped to its own table or in the table of its parent shouldn't be made on how hard it is, it should be made based on whether the object instances are entity types or value types. For example, if its a true 1-to-1 relationship and the RuleModel exists only within the context of the Promotion, and its lifecycle is completely managed by the Promotion, then embed it. But if other objects will reference the same object instance of RuleModel that is referenced by the Promotion, then you'll have to map it to a table and make it a true entity type.

That being said, mapping an entity relationship in JPA is not a lot of work at all.
 
Tong Wang
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I don't want to use @Embed. As I said, the RuleModel itself contains references to a dozen other classes, mapping all those classes would be a lot work and really unnecessary, because I will never run a SQL query on them, I just need to be able to load/store the ruleModel as a whole thing. So I just want to store the object instance of a RuleModel in a single field; the only thing I can think of is to store the binary form of the object instance as a @BLOB, but not sure if this will work.
 
Javid Jamae
Author
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why put it in a blob? Why not just create a table for the RuleModel and store the data for that object in there, but just make the references to other objects on the RuleModel transient so they're not stored (hence your object graph is not persisted). The advantage is that you can always access the data for the RuleModel if its in a table. If its in a Blob, you can't really see it using DB tools, SQL, etc.
 
Tong Wang
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RuleModel is not meant to be queried, or used by any DB tools. Setting its references to other objects to transient is also not acceptable, because I need to store the complete object graph and later restore it.
 
Javid Jamae
Author
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahhh, sorry. So you want to store the entire graph as a blob, not just the RuleModel object. I think you just have to serialize everything in your graph (add a Serializable interface to everything), then use @Lob on your RuleModel reference in the Promotion. This should persist it as a Blob (though I have to admit that I haven't tried this with JPA).
 
Tong Wang
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not a problem. Actually I just figured out another way of storing the object graph: first use xml serialization to marshal the graph to xml, then store the xml in string format. But I am not sure what is the best mapping for large xml strings.
 
jacklty lam
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can consider using castor xml or JAXB

Once you define your object graph structure in xsd scheme. You are free to marshal/unmarshal object to/from xml text.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic