About Question enthuware.oce-jpad.v6.2.597 :

Moderator: admin

Post Reply
tioola
Posts: 6
Joined: Mon Sep 23, 2013 5:19 pm
Contact:

About Question enthuware.oce-jpad.v6.2.597 :

Post by tioola » Mon Sep 26, 2016 5:09 am

Comparing this question with the question enthuware.oce-jpad.v6.2.587 , the explanation on question 587 makes me understand that the option 3 is also correct based on the following sentences

"The map key and the map value independently can each be a basic type, an embeddable class, or an entity".

"when the map value is a basic type or embeddable class, the ElementCollection annotation is used; when the map value is an entity, the OneToMany or ManyToMany annotation is used."

For ElementCollection the value should not be an Entity.

For ManyToMany , OneToMany can I really have both (key and value as an entity)?

admin
Site Admin
Posts: 8065
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.oce-jpad.v6.2.597 :

Post by admin » Mon Sep 26, 2016 8:26 pm

Yes, map key and value can both be entities. But option 3 says that they cannot be. That is why option 3 is incorrect.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

__JJ__
Posts: 125
Joined: Thu Jul 05, 2018 6:44 pm
Contact:

Re: About Question enthuware.oce-jpad.v6.2.597 :

Post by __JJ__ » Sat Sep 22, 2018 3:05 pm

Your explanation for option one states that a map that has an entity for a value can be an element collection:
[Wrong answer: Map key cannot be an entity.] It can be. For example, FulltimeEmployee, Project are entities:

Code: Select all

@Entity public class FulltimeEmployee{      
    //When map value is a class     
    @ElementCollection     
    @CollectionTable(name="EMP_PROJECTS")     
    protected Map<Project, ProjectAllocation> currentProjects;    
OR        
    //when map value is a basic type     
    @ElementCollection     
    @CollectionTable(name="EMP_PROJECTS", joinColumns=@JoinColumn(name="MY_EMP_ID"))     
    @Temporal(TemporalType.DATE)     
    @MapKeyJoinColumn(name="THE_PROJECT_ID")     
    @Column(name="STARTDATE")     
    protected Map<Project, Date> currentProjects;      
    ... 
}
The second mapping (element collection when the map value is a basic type) is fine but the first (element collection when the map value is an entity) is not.

This contradicts explanations in prior questions, which matches what is stated in ProJPA2 (I'm sorry I don't know where this would be in the spec):
It is always the type of the value object in the Map that determines what kind of mapping must be
used. If the values are entities, the Map must be mapped as a one-to-many or many-to-many
relationship, whereas if the values of the Map are either embeddable or basic types, the Map is mapped as
an element collection.

admin
Site Admin
Posts: 8065
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.oce-jpad.v6.2.597 :

Post by admin » Sat Sep 22, 2018 10:13 pm

The first mapping i.e. protected Map<Project, ProjectAllocation> currentProjects; uses ProjectAllocation as value and ProjectAllocation is not an entity. It is just a regular class. Only FulltimeEmployee, Project are entities.
If you like our products and services, please help us by posting your review here.

__JJ__
Posts: 125
Joined: Thu Jul 05, 2018 6:44 pm
Contact:

Re: About Question enthuware.oce-jpad.v6.2.597 :

Post by __JJ__ » Sun Sep 23, 2018 9:04 pm

admin wrote:
Sat Sep 22, 2018 10:13 pm
The first mapping i.e. protected Map<Project, ProjectAllocation> currentProjects; uses ProjectAllocation as value and ProjectAllocation is not an entity. It is just a regular class. Only FulltimeEmployee, Project are entities.
How does that work? I've not seen any examples anywhere of classes that aren't either an entity, embeddable or basic type - by which I mean, built-in java type.
I just tried persisting a collection of a regular class ie unannotated with @Entity, @Embeddable, or @MappedSuperclass, and it did not work:

Code: Select all

@Entity
public class Person implements Serializable {
...
    @OneToMany(cascade = CascadeType.ALL)
    private List<Z> zs;
...
}


import java.io.Serializable;
public class Z implements Serializable {
    private String name;
//...getters, setters, constructors
}

//main...
Person p1 = new Person();
....
p1.getZs().add(new Z("foo"));
p1.getZs().add(new Z("bar"));            
em.getTransaction().begin();
em.persist(p1);                   
em.getTransaction().commit();
and what I got was this:

Code: Select all

org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: jpa.entity.Person.zs[jpa.entity.Z]

admin
Site Admin
Posts: 8065
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.oce-jpad.v6.2.597 :

Post by admin » Mon Sep 24, 2018 12:03 am

You are right. It cannot be a regular class. I checked the code for ToyJPAApp (where all the code used in the questions was tested) and I see that ProjectAllocation is annotated with @Embeddable.

So, what's the issue if Project is an entity and ProjectAllocation is an Embeddable? The explanation is only talking about having an Entity as Map key, which is possible.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests