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

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.

Related

Issue with Map<String, Entity> in 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)

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.

Entity framework Association in conceptual model for one to many relationship

I have two entity CUSTOMER and ORDER..there is one to many relation from CUSTOMER to ORDER where CustomerID is primary key for customer and foreign key in ORDER..now I want to add customer name property from CUSTOMER entity in ORDER entity...I have copied this property and paste it in ORDER table and have added CUSTOMER table and map this property to the CUSTOMER table's same property..but when i trying to validate it vs giving me a Error that is
3024: Problem in mapping fragments starting at line 239:Must specify
mapping for all key properties (ORDER.OrderID) of the EntitySet ORDER
That is not possible in mapping. You cannot add property from Customer table into Order entity this way. Mapping properties from multiple tables to the same entity has very strict rules and it is not possible for this case.
You can expose customer's name in your Order class without defining it in the mapping. Create partial part of Order class and add custom computed property (non mapped):
public partial class Order
{
public string CustomerName
{
get
{
// Customer is navigation property to Customer entity
return Customer.Name;
}
}
}
This will require loading Customer with your Order (eager loading) or using lazy loading. Also this property cannot be used in Linq-to-entities queries.

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?

#ManyToMany annotated list of strings? Is it ever possible?

I have users and each user has several roles. Roles and users are in many-to-many relationship. But the role is just a name. I've got stuck in this phase:
#ManyToMany
#JoinTable(name="users2roles")
private Collection<String> roles;
There is missing the name of target table. I was searching on Internet, but nothing have found. I don't wannt to create special entity class to wrap one string value. Is there some another way, isn't?
Is there any way to make it work or makes JPA me create a new entity?
A Collection of Strings isn't relating one object to another so cannot be a #OneToMany or #ManyToMany - ought to be #ElementCollection. It's a collection of Strings, nothing more. Yes, it can be stored in a join table, but using #CollectionTable.