Hi VP,
I’m testing once again with ORM components, and I have a question about how to use them 8)
Initially, I wanted to work with the model of Image1. The reason is that I have a large amount of objects which were associated with a small class, and I try to convert that small class into an ORM Component to save tables in the DB.
But… that small class was abstract and the concrete classes that generalize it have some different attributes. So I was wondering if I could put all this attributes into several ORM Component classes (generalizations of one single parent, abstract), and then associate this single abstract parent with the ORM Persistent class. What? :shock:
It’s easier than it seems after reading me (excuse my strange explanation). The Image1 shows nearly that. The original picture was nearly the same, but replacing ORM Components with ORM Persistable entities.
So, are you ready to launch? Well… I try syncing to ERD and… the generated Entity only has “global1” attribute all the other attributes stored in generalized classes were lost from persistence.
Maybe this is what is was supposed to happen. So I ended up with the model shown in the second picture, Image2. This time, I think that the model is ok because the generated Entity has all the fields (Image3) defined
Uhm… but a closer look told me that the “childParams_child1_paramOne1” is set as “Nullable=false”. Oops… what happens if my code does something like:
Parent parent=new Parent();
parent.childParams=new ChildParams();
parent.childParams.child2=new Child2();
Save(parent) etc...
I think that NHibernate will complain because the reference to child1 was not set. So, maybe the only solution is to use a “null object” previously created, with all the values set to something special, that later will be recognized as a “null component”. Something like:
parent.childParams.child1=nullChild1; (previously created)
But then, I remembered that I have read something about this problem. And finally found it. Paraphrasing Kuate (from “NHibernate in action” book, Manning EAP 2007, a fantastic book):
“There is no elegant way to represent a null reference to a Component. In lieu of an elegant approach, NHibernate represents null components as null values in all mapped columns of the component. This means that if you store a component object with all null property values, NHibernate will return a null component when the owning entity object is retrieved from the database.”
So, I think that this can be solved with that int parameter set as “nullable”, as well as all the others. I set the multiplicity to “0…1” to make this happen, but it did not
Is it all ok? or maybe I am confused about some theoretical concepts? Please let me know
As always, many thanks for all your support Image1.png
Image2.png
Image3.png