Re: [Beankeeper-discussion] save method
Brought to you by:
demonsystem
From: Brautigam R. <de...@ne...> - 2009-03-02 20:02:43
|
Shay Matasaro írta: > Hi, Hi, > the following is taken form form the Save Jdoc, i belive that the > description should be expended,please see my questions inline : You're right, the APIdoc does not explain the save logic enough. It is also outdated a bit. > Save the given object to the store. The given object's all private > non-transient fields will be saved. If the object was not selected from > the store, and not yet saved, it will be created in the store, and a > unique id will be assigned, so all subsequent calls to save the given > object will only modify the already existing instance in store. A few tips: > 1) only private fields ? what about public and protected? Currently all nontransient nonstatic fields are saved. The apidoc is wrong, whether the field is private does not matter. > 2) the existence or no existence of accessors methods does not affect > the library at all? No it does not. It works kind of like java serialization. The internal state of the object is what matters, so that will be saved, no matter the methods. > * Use simple beans. Although this library does not scan methods to > determine the attributes to save, it is a good idea to simplify > work with them. > > 3) what do you mean by simple? for example i am using the > XMLRootElement annotation, and i implement numerous interfaces , should > i avoid it? It means it is advised to use Java beans. Simple means it should only have private fields and getters, maybe setters. This has nothing to do with beankeeper, it's just sane measures for a good data model. > 4) if the library does not use reflection how does it get the list of > fields? It does use reflection to get the fields. It does get the methods, it gets the fields directly. > * If you do not use simple beans, watch out that your object does > not reference unnecessary objects, because if it does, all will be > saved/inserted and tracked. > > 5) i assume that marking such objects as transient will address that? Yes. > * You CAN use objects which reference other beans though. But > beware, that all objects which are directly referenced will be > loaded when the parent object loads. > > 6) can you expend on the above? Direct reference means there is a field which is an object. Indirect reference means an object hold another in a Map or Collection. All directly referenced objects are loaded when the parent object is loaded, so the more objects are referenced directly the longer it takes to load the object. This is a known problem among O/R libraries. There is no known good way to solve this currently. > * You CAN use Map, and List types in your beans. Check the > documentation. > > some samples on what not do and best practices, would probably be helpful. Yes, you're right. As a rule of thumb, what seems easy will be easy to do, what looks complex will be harded to select/save. For example beans with only primitive fields are the easiest. Beans with other beans referenced are harder. Maps and Collections are harder still. The more complex the data model, the harder to select, etc. > hopefully anybody that reads my posts , will get a some hints ,on what > not to do :) Generally you should have a clean data model, that is not deep (level of direct references), and direct references should be typed if that's possible (not Object, but something more concrete). > Thanks, > Shay I know these are only general tips, you're right that we should write up a more detailed list of good and bad practices. Robert. |