Semantics of equality between entities in JPQL - jpa

JPA specification says:
Only the values of like types are permitted to be compared. A type is like another type if they correspond to the same Java language type, or if one is a primitive Java language type and the other is the
wrappered Java class type equivalent (e.g., int and Integer are like types in this sense). There is one
exception to this rule: it is valid to compare numeric values for which the rules of numeric promotion
apply. Conditional expressions attempting to compare non-like type values are disallowed except for
this numeric case.
Note that the arithmetic operators and comparison operators are permitted to be applied to
state fields and input parameters of the wrappered Java class equivalents to the primitive
numeric Java types.
Two entities of the same abstract schema type are equal if and only if they have the same primary key
value.
Only equality/inequality comparisons over enums are required to be supported.
Comparisons over instances of embeddable class or map entry types are not supported.
Does this mean that queries
where entity1 = entity2
and
where entity1.id = entity2.id
should always give the same result (and have same performance) or are there some edge cases (e.g. if one of entities is null)? Is the behavior the same in all implementations?

These can be different, depending where your entity comes from, and your JPA implementation.
entity1 = entity2, may allow the JPA implementation to optimize out a join in certain cases.

Related

How to implement disjoint (nonoverlapping) subtype with total completeness in MongoDB?

I designed an entity relationship diagram (ERD) with the goal of creating two child entities (INSTITUTION and SPACECRAFT) of a parent entity (LOCATION), which is distinguished depending on the value of a boolean attribute (IS_SPACECRAFT). Implementing this in MongoDB, I'd like to link the INSTITUTION entity to another separate entity when IS_SPACECRAFT is false (0), and the SPACECRAFT entity to this other separate entity when IS_SPACECRAFT is true (1).
Right now, I am doing this manually by creating two attributes (SPACECRAFT_ID and INSTITUTION_ID) in this unrelated entity and pasting the object ID of either the INSTITUTION or SPACECRAFT entity in this attribute depending on the value of IS_SPACRECRAFT. But this inevitably leads to one of those attributes being NULL, which I think is poor database design and is something I'd like to avoid. With that in mind, is there a way to link another entity (i.e., as a foreign key) depending on a Boolean value in MongoDB?
Attached below is the ERD I mentioned earlier so the hierarchy is clear. The discriminator (d) is meant to represent a disjoint (overlapping) subtype, and the double lines represent a total completeness constraint, such that all the member of the parent LOCATION entity are in either SPACECRAFT or LOCATION, but not both.
For reference, a disjoint subtype is defined as:
Disjoint subtypes, also known as nonoverlapping subtypes, are subtypes that contain a unique subset of the supertype entity set; in other words, each entity instance of the supertype can appear in only one of the subtypes.
And total completeness is defined as:
The completeness constraint specifies whether each entity supertype occurrence must also be a member of at least one subtype. The completeness constraint can be partial or total. Partial completeness means that not every supertype occurrence is a member of a sub-type; some supertype occurrences may not be members of any subtype. Total completeness means that every supertype occurrence must be a member of at least one subtype.
https://www.google.com/books/edition/Database_Systems_Design_Implementation_M/4JN4CgAAQBAJ

OCL collection operation on list attribute

I'm modelling a problem in which there are students and courses.
Each courses have some other courses as prerequisites and I want to make an OCL constraint on the "joinCourse()" operation of students to allow them to join a courses if and only if they have finished required courses.
this is a draft of the UML class diagram I made:
now, my question is:
is possible to make a collection operation, like "includeAll" on a "list attribute" like finishedCourses?
I made this ocl constraint:
context Student::joinCourse(c: Course)
pre: self.finishedCourses->includesAll(c.requiredCourses)
correct?
thanks in advance!
In short: yes, it's correct.
Justification
In the UML 2.5 specifications there is a clear link between multiplicity and collection:
7.5.3.2. A MultiplicityElement is an Element that may be instantiated in some way to represent a collection of values. (...)
The OCL specification explains how self relates to the context (clause 7.3.4):
The OCL expression can be part of a Precondition or Postcondition,
corresponding to «precondition» and «postcondition» stereotypes of
Constraint associated with an Operation or other behavioral feature.
The contextual instance self then is an instance of the type that owns
the operation or method as a feature.
So in your expression, self refers to a Student, and a dot allows to access its properties, such as self.finishedCourse refers to a collection property.
OCL defines collection types (clause 11.6) and general operations that are well formed for all collections (clause 11.7.1):
includesAll(c2 : Collection(T)) : Boolean
Does self contain all the elements of c2 ?
post: result = c2->forAll(elem | self->includes(elem))
So self.finishedCourses->includesAll(c.requiredCourses) is a valid expression that is true or false exactly in the situation that you expect.
Additional remark
A property may represent an attribute of a classifier or a member end of an association. You could have represented finishedCourse as the end of a second association between Student and Course (being understood that all courses taken are not necessarily finished).

Can I store documents and key-value pairs in the same DynamoDB instance?

This is probably an obvious question, but I'm not seeing it in Amazon's documentation.
Can I store documents and key-value pairs in the same DynamoDB instance? Or can an instance only have one type?
I also don't see how to specify the object type when writing an item.
The wording is confusing. The primary keys do need to be one Data type.
DynamoDB supports many different data types for attributes within a
table. They can be categorized as follows:
Scalar Types – A scalar type can represent exactly one value. The scalar types are number, string, binary, Boolean, and null.
Document Types – A document type can represent a complex structure with nested attributes—such as you would find in a JSON document. The
document types are list and map.
Set Types – A set type can represent multiple scalar values. The set types are string set, number set, and binary set.
When you create a table or a secondary index, you must specify the
names and data types of each primary key attribute (partition key and
sort key). Furthermore, each primary key attribute must be defined as
type string, number, or binary.
DynamoDB is a NoSQL database and is schemaless. This means that, other
than the primary key attributes, you don't have to define any
attributes or data types when you create tables. By comparison,
relational databases require you to define the names and data types of
each column when you create a table.
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html

Something I don't quite understand about Conditional Mappings

In most cases, we can map a field in a table either to a property or we can map it using conditional mapping, but not both. Only exception is if condition is set to Is NotNull, since then we can also map to a column.
a) Is this the reason why we are able to map a DB column only once - namely, if field was allowed to have both a property mapping and a conditional mapping, then property mapping would tell EF to retrieve all table rows, while conditional mapping would tell EF to retrieve only those rows that satisfy the condition?!
b) If my reasoning under a) is correct, then why is field allowed to have both mappings when condition is set to Is NotNull? Why won't that create a conflict?
Thank you
Mapping with condition Is NotNull has special meaning because it requires subsequent change in your model. The mapped property in the model must not be nullable. So your column in the database is nullable, your mapping condition filters all records with null value and your property always receives only records with non-null values. Also you can never assign null to the property.
In case of common condition with value equality this special behaviour is not possible.

EF: a separate table for each entity type in the hierarchy

Working with EF 4 data model, let's say we have a type named "Animal" and two other types "Bird" and "Horse" that inherit from Animal type.
I want the properties on type Animal to be inherited on sub types but I want those sub types to have their of separate tables, each repeating the columns inherited from Animal.
Type Animal will not exist in the database.
From a database perspective there will not be inheritance but having inheritance in the EF model will ease management of model.
Is is possible to define such a model/mapping in EF?
You need TPC mapping (Table per Concrete Type or Table per Class).
Yes - its called "Table per Type" and there are walkthroughs here
http://msdn.microsoft.com/en-us/library/cc716702.aspx
http://msdn.microsoft.com/en-us/library/bb738685.aspx
Edit:
However, if you may also want to look at Table per Concrete Type. The various type schemes are compared here
http://msdn.microsoft.com/en-us/library/cc716779.aspx
"In this mapping scenario, non-abstract types are each mapped to an individual table. Each of these tables must have columns that map to all of the properties of the derived type, including the properties inherited from the base type."