Necessity of the #Parent annotation - entity

I am relooking at the objectify documentation and wondering what the benefit or necessity of the #Parent annotation is. It appears as keys or refs would do the trick. Any way you can share your experience on what the necessity or benefit is? Thanks.

#Parent gives you two benefits:
All entities with a common ancestor count as a single entity group in transactions.
You can perform strongly-consistent ancestor queries.
Some useful reading here:
https://cloud.google.com/appengine/docs/java/datastore/structuring_for_strong_consistency

Related

Is correct relationships of class diagram in UML?

The image shows the logistics of the Warehouse. Very very simplistic. What is its concept: There are documents: ReceivingWayBill, DispatchingWaybill, ReplacementOrder.
They interact with the main classes: Warehouse, Counterparty, Item.
And the Register class: ItemRemainsInWarehouse. It turns out, the document is confirmation of the operation, reception, sending, and so on. The Register simply stores information about the number of remaining goods.
If you miss a lot of problems of this scheme, such as: the lack of generalization, getters and setters and a heap of everything else.
Who can tell: the relationship between classes, and there is concrete aggregation everywhere, are placed correctly, or can we somehow consider the association in more detail?
It is so hard (maybe impossible) to correct your whole model with provided explanation. I give some improvements.
You should put Multiplicity of you relationships. They are so important. In some relationship, you have 1 (ReplacementOrder , Warehouse) and some of your relatioships are maybe * (Item , ReceivingWayBill)
You put Aggregation between your classes and we know that Aggregation is type of Association. You can put Associations too. You can find a lot of similar questions and answers that explain differences between Association and Aggregation (and Composition). see Question 1, Question 2 and Question 3. But I recommend this answer.
I think, there is NOT a very significant difference between Aggregation and Association. See my example in this question.
Robert C. Martin says (see here):
Association represents the ability of one instance to send a message to another instance.
Aggregation is the typical whole/part relationship. This is exactly the same as an association with the exception that instances
cannot have cyclic aggregation relationships (i.e. a part cannot
contain its whole).
Therefor: some of your relationships are exactly an Aggregation. (relationship between Item and other classes). Your Counterparty has not good API definition. Your other relationships is about using Warehouse class. I think (just guess) the other classes only use Warehouse class services (public methods). In this case, they can be Associations. Otherwise, if they need an instance of Warehouse as a part, they are Aggregations.
Aggregation is evil!
Read the UML specs about the two variants they introduced (p. 110):
none: Indicates that the Property has no aggregation semantics. [hear, hear!]
shared: Indicates that the Property has shared aggregation semantics. Precise semantics of shared aggregation varies by application area and modeler.
composite: Indicates that the Property is aggregated compositely, i.e., the composite object has responsibility for the existence and storage of the composed objects (see the definition of parts in 11.2.3).
Composite aggregation is a strong form of aggregation that requires a part object be included in at most one composite object at a time. If a composite object is deleted, all of its part instances that are objects are deleted with it.
Now, that last sentence clearly indicates where you should use composite (!) aggregation: in security related appications. When you delete a person record in a database you need to also delete all related entities. That often used example with a car being composed of motor, tires, etc. does not really fit. The tires do not vanish when you "delete" the car. Simply because you can not delete it. Even worse is the use of a shared composite since it has no definition per definition (sic!).
So what should you do? Use multiplicities! That is what people usually want to show. There are 0..n, 1, etc. elements related to to the class at the other side. Eventually you name these by using roles to make it explicit.
If you consider DispatchingWaybill and ReceivingWaybill it looks like those are association classes. With the right multiplicities (1-* / *-1) you can leave it this way. (Edit: note the little dots at the association's ends which tell that the class at the opposite has an attribute named after the role.)
Alternatively attach either with a dashed line to an association between the classes where they are currently connected to.

Composition and Aggregation in a Class diagram

I am having a hard time understanding these when it comes to designing a class diagram, from what I know composition is a "has-a" relationship but Aggregation ?? I have no idea what is it. and one thing when will I know to use Composition in a class diagram? and when will I know to use Aggregation in a class diagram?? a example will be highly appreciated.
The main difference between an aggregation and a composition is the property of your "has-a" relationship. It' either strong or weak.
The aggregations "has-a" relationship is of "weak-type". Weak meaning the linked components of the aggregator may survive the aggregations life-cycle or may be accessed in some other way. A simple example would be a football club with its members. If the club is dissolved, you still got the members - which in fact could also be members of other clubs and thus are kept alive.
The composition "has-a" relationship is of "strong-type". Strong meaning that one can't exist without the other. The component's life-cycles are directly linked to the "parent". An example would be a house with rooms. If you decide to tear the house down, you will also lose your rooms.
Maybe a little abstract but I think that's the idea behind it.
See answers to previous questions here, here and here.
Personally I don't use Aggregation. The semantics are too weak to be useful. It causes more problems than it solves. There's only one place where it has well-defined and potentially useful properties that distinguish it from a simple binary association (see footnote).
Composition can be useful because it defines important properties on immutability and lifecycle management (see 1st link above). I've survived quite happily for many years without ever needing to use Aggregation.
hth.
Footnote. Aggregation can be applicable if you need to model a recursive tree relationship, e.g. a part decomposed recursively into sub-parts. Aggregation says there can be no circular relationships, i.e. a part can't be a sub-part of itself - directly or indirectly. However most people reading the model are unlikely to know that. -So you'd need to add a comment. Which means you'd be as well to stick with a binary association and avoid the confusion of using Aggregation.

Unidirectional parent-child association not null

I have a class structure which is akin to a PurchaseOrder (parent) and PurchaseOrderLine (child) pattern, where the order lines will only be saved to the DB by saving the parent PurchaseOrder and will only ever be accessed via the parent too.
The DB has PurchaseOrderLine.PurchaseOrder set to not permit null values.
It seems from searching through the web that it is not possible to have a uni-directional association from PurchaseOrder via an IList property without having to have a property on the line pointing back when the child has a NOT NULL constraint for its PurchaseOrder column.
Is this really the case? It seems like one of the most basic things one would want to do with an ORM, I find it difficult to accept that a product as mature as NHibernate cannot handle such a basic scenario.
No it's not the case. Please see the example provided in the answer to this question: When to use inverse=false on NHibernate / Hibernate OneToMany relationships?
Well, it may be the case that you can't have unidirectional one-to-many relationship defined only on one side, but I'll argue with your statement that this is "one of the most basic things one would want to do with an ORM".
One of the most basic things would be to have unidirectional one-to-many defined only on many side - as it is natural for RDBM tables. And ORMs (despite the common misconception) are not intended (or able) to fully abstract domain model from underlying data source. Even if in some cases they can, the database side suffers from select N+1 problems or very ineffective queries.
Defining one-to-many at one side makes an impression that i.e. counting the collection is cheap. It is the case with plain object graphs, but not with NHibernate entities, as reading collection causes (at least one) call to the database. Eager fetching from one side is also not able to properly use database join mechanism in the way it's intended to be used (opposite to eager fetch from many side).
Even if I don't agree with a lot of arguments, I think it is useful to read some of the articles saying that "ORM is an anti-pattern", like this one. They helped me to leverage the way I think about ORMs and make me think about ORMs as a compromise between two not matching paradigms, but not the way to hide one behind another.
This can now be achieved in NH3 using the Not.KeyNullable()
this.HasMany(x => x.Actions)
.Access.BackingField()
.KeyColumn("[Application]")
.Not.KeyNullable()
.Cascade.AllDeleteOrphan();

Is there any difference between UML Use Case "extends" and inherance?

Or the "extends" is a use case inheriting another?
--update
Just a clarification, I've read books and made a lot of diagrams. But I just can't see any difference between extends on UML and on inherance. As Bill said, UML extends indicates optional behavior, but in inherance either you get new behavior you can or not use. So, what's the difference?
I think in UML the difference is in that "extends" is based on extension points, which means there has to be a named point in the use case where the extension will be applied. The semantics are not very precise about this. Inheritance for use cases means changing some behaviour, not exzactly specifying where.
Another important point is about the inheritance and the Liskov substitution principle. You should be able to use one use case, which inherits from another, in any place you can use the another one. This does not hold for the way "extends" is understood. When one use case is extended by another, it means that one might be modified by another, but still it contains the main scenario path, which might be forked and joined by the extending use case. This is, I think, about the difference between structural and behavioural inheritance. Inheritance is about achieving the same goal and satisfying the same interests - same responsibility and behaviour constraints, where extension is about modification of the structure of the path of scenario, which might be triggered by additional interests - like error checking.
Inheritance actually is not a very good mechanism to be used for use cases, combined with actor inheritance, which makes more sense, it can lead to unwanted paradoxes. Following the advice of Alistair Cockburn (Writing effective Use Cases), inheritance should be used only for expressing variations in technical details or data formats for a particular use case.
This is very similar to inheritance. See a detailed description of this concept here.
Enjoy!
In the tool I use in my company we have modeling restrictions.
Inheritance:
From Actor to Actor, from UseCase to Usecase, from System to System. You can't have other inheritances in your diagram because I got a forbidden sign.
Extend can only be done between two Usecases and not between other elements.
I don't really see the difference between inheritance and extends between two usecases.
I will keep on reading the next posts because after reading all answers I still don't understand:-)
Extends is used to add additional, optional, behavior to the use case that is being extended, but does not change any of the behavior in the base use case.
An inheriting use case would replace one or more of the behaviors of the inherited use case. In other words changing the behavior of the base use case, rather than simply adding new functionality. Note that use case inheritance is not quite the same as class inheritance.
See this article.

Desigining Proper Classes

I've read all the books about why to create a class and things like "look for the nouns in your requirements" but it doesn't seem to be enough. My classes seem to me to be messy. I would like to know if there are some sort of metrics or something that I can compare my classes to and see if there well designed. If not, who is the most respected OO guru where I can get the proper class design tips?
Creating classes that start clean and then get messy is a core part of OO, that's when you refactor. Many devs try to jump to the perfect class design from the get go, in my experience that's just not possible, instead you stumble around, solving the problem and then refactor. You can harvest, base classes and interfaces as the design emerges.
if you're familiar with database design, specifically the concept of normalization, then the answer is easy: a data-centric class should represent an entity in third normal form
if that is not helpful, try this instead:
a class is a collection of data elements and the methods that operate on them
a class should have a singular responsibility, i.e. it should represent one thing in your model; if it represents more than one thing then it should be more than one class.
all of the data elements in a class should be logically associated/related to each other; if they aren't, split it into two or more classes
all of the methods in a class should operate only on their input parameters and the class's data elements - see the Law of Demeter
that's about as far as i can go with general abstract advice (without writing a long essay); you might post one of your classes for critique if you need specific advice
Try to focus on behaviour instead of structure. Objects are 'living' entities with behaviour and responsibilities. You tell them to do things. Have a look at the CRC-card approach to help you model this way.
i think Object design is as much art as it is science. It takes time and practice to understand how to design clean & elegant classes. Perhaps if you can give an example of a simple class you've designed that you aren't happy with SO users can critique and give pointers. I'm not sure there are any general answers outside of what you've already read in the texts.
The most respected OO guru i personally know is StackOverflow. Put your classnames here and i reckon you'll get a goodly number of reviews.
Classes are typically used to model concepts of the problem domain. Once you have a well-defined problem (aka the set of use cases), you will be able to identify all participants. A subset of the participants will be intrinsic to the system you are designing. Start with one big black box as your system. Keep breaking it down, as and when you have more information. When you have a level where they can no longer be broken down (into concepts in your problem domain), you start getting your classes.
But then, this is a subjective view of a non-guru. I'd suggest a pinch of salt to the menu.
Metrics? Not so's that you'd trust them.
Are your classes doing the job of getting the program working and keeping it maintainable through multiple revisions?
If yes, you're doing ok.
If no, ask yourself why not, and then change what isn't working.