Issue with Map<String, Entity> in JPA - jpa

I have the following field in my entity
#ManyToMany
#JoinTable(
name = "licensingpolicy_eulamatches"
)
Map<String, EULADocument> eulaMatches = new HashMap<String, EULADocument>();
I use JPA 2.0 with EclipseLink.
The table generated has a unique key constraint on the ID of the entity and the ID of the EULADocument (the value in the map) entity. This is wrong as I could certainly have several times the same EULADocument in my hashmap. I can modify the table definition but I would prefer if there was a more standard way of doing it.
(I could use an element collection but I would have to invert the key and value types and I'd rather not if possible)

Related

JPA how ensure uniqueness over 2 fields, string and boolean

I want to create an entity containing 2 fields that need to be unique in together. One of the fields is a Boolean:
#Entity
public class SoldToCountry {
private String countryId;
private Boolean isInt;
}
For a given String there should never exist more than 2 entries one with isInt:true and the other isInt:false.
I read the doc about #Id but it seems that Boolean is not supported. For me it would also be ok to have a unique constraint spanned over both fields and using a generated id.
What is the best way to get this constraint via JPA?
If your table has really two fields only, and you want they are unique, then they should be the composite PK of the table. Take a look at How to create and handle composite primary key in JPA
If, instead, you have another PK, consider Sebastian's comment.

How to get foreign key value from not fetched relationship?

Having two entities defining relationship by #ManyToOne and #OneToMany, how can I get foreign key without asking from related object and just by looking at defining tables? How do I get OWNER_ID from Owned by something like owned.getOwnerId() instead of owned.getOwner().getId() and still be able to owned.getOwner()?
Map the field in your entity as a basic mapping allows you to use the foreign key directly. You can keep the object reference mapping as well, but one of the two mappings must then be marked as insertable=false, updatable=false so that JPA knows which mapping controls the field in the event they show different values.

How to model a many to one relationship with a string?

My entity has a String property. many instances of the entity share the same string. So for query performance i want to configure a many-to-one relationship for this string.
To use the jpa-annotation would need to write a new entity class that only caries the string and reference the new entity. In my opinion this would be kind of overkill.
Is it possible to configure jpa so the string is externalized to a new table with one row per distinct value?

How to add a Map<String, Person> in an entity class?

I want to add a mapping as
Map<String, Person> personMap;
inside an entity class, where Person is the entity. The Map is to identify the exact Person corresponding to the String (let it be a nickname of that person). The same person may have different names and whenever any of the names is given, the same Person has to be found.
Persistance API used is JPA and the provider is EclipseLink. What annotation should I use and how?
As per section 2.7 of JSR-317, if the value of the Map is an entity (which is your case) a join table is created and then a OneToMany / ManyToOne annotation should be used.
As for the key, if it is a Basic Type, the #MapKeyColumn can be used to customize the mapping column of the key. So here is my take on your example:
#OneToMany
#MapKeyColumn(name="person_nickname")
Map<String, Person> personMap;
EDITED:
After some testing, the following seems to work pretty well:
#ElementCollection
#CollectionTable(name="<name_of_join_table>")
#MapKeyColumn(name="<name_of_map_key_in_table>")
Map<String, Person> personMap;
The above generates a join table with three fields: one for the mapping holder id, one for the key and one for the value.

Entity Framework: Model doesn't reflect DB

I'm probably thinking about this all wrong but I have the following db tables:
When I run the EF Wizard in VS2008 I get the following model:
You'll notice that in the EF model shows that the Entity has no field for EntityTypeID or EntityStatusId. Instead it shows it as a navigation property, so the field appears to not be addressable when I instantiate an Entity (pardon the terminology confusion: Entity is a Table/Class in my name space not in the EF namespace). How can I assign an EntityTypeID and StatusTypeID when instantiating an Entity?
Yes, the entity framework hides foreign key ID properties and shows navigation properties instead. There is a lengthy discussion about why it does that, here. The usual means of assigning a reference to another entity is to assign the entity instance, rather than the foreign key ID value, like this:
var foo = new Entity();
var status = (from .... select ...).FirstOrDefault();
foo.StatusCodes = status;
However, it is possible to assign a foreign key ID directly, if you happen to know what it is:
foo.StatusCodesReference = new EntityKey(
"MyEntityContextName.StatusCodesEntitySetName", "StatusCodeId", value);
Obviously, substitute the real values in the above.