This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

About myexamcloud mock exam 1 , about oneToMany unidirectional relationship  RSS feed

 
Ranch Hand
Posts: 1701
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Question:
...This set includes table EMPLOYEE , DEPARTMENT and Project. Tables Employee and Department do not define any foreign
key constraints to table Project. Table Project defines foreign key constraints with references to table Employee and
Department and an extra column called COST.Table Employee is mapped to an entity Employee and the table Department to an
entity Department.
How can the table Project be mapped so that the persistence application can be portable?
A. Such sets of tables cannot be mapped and keep the application portable.
B. Map the table Project as a secondary table for either Employee or Department entity.
C. Map the table Project to bidirectional many-to-many relationships between the Employee and Department using a java.util.Map as
the type of the field or property.
D. Map the table Project to an entity Project with a field or property cost mapped to the Column COST and one-to-Many relationships
to this entity in the Employee and the Department.
Answer D.
Explanation:
Unidirectional OneToMany Relationships
Entity A references a collection of Entity B.
Enity  B does not reference Entity A.
Unidirectional relationship has only an owning side, which must be Entity A.
...





I think the code should be like this:


My reason is that Project has a foreign key to employee and a foreign key to department based on the question.
So, project owns employee and departments. Employee and Department should not have any reference to Project as the Employee table and Department table do not have any foreign keys of Project table.
 
Himai Minh
Ranch Hand
Posts: 1701
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My previous recommendation does not work.
I have to do this  assume there is a Project table with Department_ID and Emp_ID as foreign keys for department and employee:



 
Creator of Enthuware JWS+ V6
Bartender
Posts: 3121
259
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer to the question is wrong. By default an unidirectional OneToMany relationship will use a JoinTable which means that there will be a table inbetween Department and Project to store the foreign keys.

The solution is to use two unidirectional ManyToOne relationships:
  • one unidirectional ManyToOne relationship from the Project entity to the Department entity and
  • one unidirectional ManyToOne relationship from the Project entity to the Employee entity.
  •  
    Himai Minh
    Ranch Hand
    Posts: 1701
    12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This question is a little bit tricky.

    I guess in this question, the Project table is the table in between Department and Employee because Project table has a foreign key of Department and a foreign key of Employee.
    Typically, it should be Department and Employee have @ManyToMany relationships among each other and the Project table is the @JoinTable between them.

    But in this question, it seems that Department and Employee tables are not related to each other. Instead, Project table relates to Department and Employee.
    There is also a Project entity being mapped to the Project table.


     
    Himai Minh
    Ranch Hand
    Posts: 1701
    12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    One more note, in chapter 10 of   JPA Pro 2nd edition,
    There is a joinTableManyToOne example:





    CREATE TABLE EMPLOYEE (ID INTEGER  NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
                           NAME VARCHAR(255), SALARY BIGINT, PRIMARY KEY (ID));
    CREATE TABLE DEPARTMENT (ID INTEGER  NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
                             NAME VARCHAR(255), PRIMARY KEY (ID));
    CREATE TABLE EMP_DEPT (Employee_ID INTEGER NOT NULL, department_ID INTEGER NOT NULL, PRIMARY KEY (Employee_ID, department_ID));



    This is from the JoinTable specification:


    A join table is typically used in the mapping of many-to-many and unidirectional one-to-many associations. It may also be used to map bidirectional many-to-one/one-to-many associations, unidirectional many-to-one relationships, and one-to-one associations (both bidirectional and unidirectional).


    Reference: http://docs.oracle.com/javaee/7/api/javax/persistence/JoinTable.html
     
    Frits Walraven
    Creator of Enthuware JWS+ V6
    Bartender
    Posts: 3121
    259
    Android Chrome Eclipse IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I am not sure what you are trying to say but a JoinTable can not have an extra attribute like in this example (Table Project defines foreign key constraints with references to table Employee and Department and an extra column called COST.).

    Or in other words Project has to be an entity.
     
    Himai Minh
    Ranch Hand
    Posts: 1701
    12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I see. So, Project cannot serve as an entity and being mapped to a join table at the same time.
     
    Frits Walraven
    Creator of Enthuware JWS+ V6
    Bartender
    Posts: 3121
    259
    Android Chrome Eclipse IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Correct.
     
    Frits Walraven
    Creator of Enthuware JWS+ V6
    Bartender
    Posts: 3121
    259
    Android Chrome Eclipse IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    ... well there is one small exception (where the JoinTable can have an extra attribute) and that is when an unidirectional OneToMany relationship is mapped via a Map relationship. In that scenario the Map key ends up in the JoinTable, but never mind that was not one of the options to choose.


    The solution to the question is (however not among the possible answers):


     
    Himai Minh
    Ranch Hand
    Posts: 1701
    12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    there is one small exception (where the JoinTable can have an extra attribute) and that is when an unidirectional OneToMany relationship is mapped via a Map relationship.
    In that scenario the Map key ends up in the JoinTable



    You mean this  ?

    That means Project table has  columns : Emp_ID, Dept_ID and COST.
     
    Frits Walraven
    Creator of Enthuware JWS+ V6
    Bartender
    Posts: 3121
    259
    Android Chrome Eclipse IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Almost, the Map value has to be the Entity (or you will have to use the ElementCollection annotation) and the relationship is from Employee to Department.



    Note that this is not a realistic mapping of course, just an example of a JoinTable with an extra attribute
     
    Himai Minh
    Ranch Hand
    Posts: 1701
    12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi, Frits. Thanks for your suggestion.
    At first, I thought about this mapping too.
    But in practice, a cost may not be unique and it is not used as a key of a map.
     
    Himai Minh
    Ranch Hand
    Posts: 1701
    12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


        Question:
        ...This set includes table EMPLOYEE , DEPARTMENT and Project. Tables Employee and Department do not define any foreign
        key constraints to table Project. Table Project defines foreign key constraints with references to table Employee and
        Department and an extra column called COST.Table Employee is mapped to an entity Employee and the table Department to an
        entity Department.
        How can the table Project be mapped so that the persistence application can be portable?
        A. Such sets of tables cannot be mapped and keep the application portable.
        B. Map the table Project as a secondary table for either Employee or Department entity.
        C. Map the table Project to bidirectional many-to-many relationships between the Employee and Department using a java.util.Map as
        the type of the field or property.
        D. Map the table Project to an entity Project with a field or property cost mapped to the Column COST and one-to-Many relationships
        to this entity in the Employee and the Department.
        Answer D.
        Explanation:
        Unidirectional OneToMany Relationships
        Entity A references a collection of Entity B.
        Enity  B does not reference Entity A.
        Unidirectional relationship has only an owning side, which must be Entity A.
        ...

    @Entity
    public class Project{ ....}

    @Entity
    public class Employee{
    @OneToMany
    private Collection<Project> projects;
    }

    @Entity
    public class Department{
    @OneToMany
    private Collection<Project> projects;
    }
    @Entity
    public class Project{ ....}

    @Entity
    public class Employee{
    @OneToMany
    private Collection<Project> projects;
    }

    @Entity
    public class Department{
    @OneToMany
    private Collection<Project> projects;
    }



    I finally understand what this question is intending to ask.
    It is asking for two unidirectional relationship. One is between Employee to Project with @OneToMany relationship. Another one is between Department and Project with another @OneToMany relationship.
    So, Project table is not even a join table between Employee and Department tables.
     
    Frits Walraven
    Creator of Enthuware JWS+ V6
    Bartender
    Posts: 3121
    259
    Android Chrome Eclipse IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That doesn't make sense to me. An unidirectional OneToMany by default uses a join table. Which means that there will be more than 3 tables in the given mapping. Apart from that the description says that the Project table contains 2 foreign keys.

    the mapping in the answer is wrong. So what is your suggested mapping?
     
    Himai Minh
    Ranch Hand
    Posts: 1701
    12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think the given answer should be this  if the intention is to use 2 unidirectional relationships which is employee to project  and department to project:

    @Entity
    public class Employee{
    @OneToMany
    private Collection<Project> projects;
    }

    @Entity
    public class Department{
    @OneToMany
    private Collection<Project> projects;
    }
     
    Frits Walraven
    Creator of Enthuware JWS+ V6
    Bartender
    Posts: 3121
    259
    Android Chrome Eclipse IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ok, that is indeed possible 

    Or they meant two unidirectional ManyToOne relationships from the Project entity. . . anyway the answer is not correct the way it is given.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!