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

Moderator: admin

Post Reply
M_Z

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

Post by M_Z » Mon Mar 12, 2012 3:40 pm

I don't see "mappedBy" attribute on any of annotations, so it's impossible to create a single two-direction relationship. Instead two single-directional relationships can be created.

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

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

Post by admin » Mon Mar 12, 2012 7:02 pm

While what you are saying is correct, the question does not ask to create single bidirectional. To satisfy the requirement given in the question, mappedBy is not necessary.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

johnlong
Posts: 185
Joined: Mon Jun 20, 2016 5:06 pm
Contact:

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

Post by johnlong » Wed Dec 21, 2016 7:10 pm

Could you please advise why

Code: Select all

@Entity
public class Employee
@OneToOne
private Department department;
Does not satisfy requirement of Employee having only one department and @ManyToOne is required.

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

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

Post by admin » Fri Dec 23, 2016 12:16 am

When you apply @OneToOne to department, it means that Employee refers to one Department (which is ok) and Department refers to one Employee (which is not ok). The question clearly says that a Department can have any number of Employees. That is why you need ManyToOne on department variable.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

johnlong
Posts: 185
Joined: Mon Jun 20, 2016 5:06 pm
Contact:

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

Post by johnlong » Fri Dec 23, 2016 2:58 pm

But it's possible to have one to one from Employee to Department, and many to one from Department to Employee (2 unidirectional relationships)

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

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

Post by admin » Fri Dec 23, 2016 10:26 pm

Not sure I understand what you mean. There cannot be as many departments as employees. So a OneToOne in Employee on department variable wouldn't make any sense.
If you like our products and services, please help us by posting your review here.

johnlong
Posts: 185
Joined: Mon Jun 20, 2016 5:06 pm
Contact:

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

Post by johnlong » Sat Dec 24, 2016 6:11 pm

Ok, I will try to explain my point :

Code: Select all

@Entity
public class Employee

@OneToOne  // Requirement 1 of 2 is fulfilled "An employee can work in only one department"
private Department Department

Code: Select all

@Entity
public class Department

@OneToMany  // Requirement 2 of 2 is fulfilled "Department can have any number of Employees"
private Collection<Employee> employees
Those are two unidirectional associations (Problem does not say that it has to be bidirectional relationship)

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

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

Post by admin » Sun Dec 25, 2016 12:16 pm

But the first part of your example i.e. @OneToOne also implies that in this relationship a Department has one employee, which is not given in the problem statement.
Your example implies that there are two relationships between Employee and Departement, which is not given in the problem statement. I understand that you are interpreting it that way but that interpretation is too convoluted. You should go with the most obvious interpretation that there is only one relationship between them.

thank you,
Paul.
If you like our products and services, please help us by posting your review here.

johnlong
Posts: 185
Joined: Mon Jun 20, 2016 5:06 pm
Contact:

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

Post by johnlong » Sun Dec 25, 2016 6:17 pm

But if there is only one relationship (bidirectional relationship) then one side has to be owning, and inverse side has to add mappedBy element, but it does not.

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

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

Post by admin » Mon Dec 26, 2016 12:54 am

Yes, you are right. Your solution is technically correct. It just doesn't feel right though and I can't pin point the reason why :oops:
If you like our products and services, please help us by posting your review here.

johnlong
Posts: 185
Joined: Mon Jun 20, 2016 5:06 pm
Contact:

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

Post by johnlong » Wed Dec 28, 2016 7:45 am

But the first part of your example i.e. @OneToOne also implies that in this relationship a Department has one employee, which is not given in the problem statement.
I think it does not, it would do so only in case of bidirectional relationship, which is not the case here.

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

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

Post by __JJ__ » Thu Sep 20, 2018 6:55 pm

johnlong wrote:
Sat Dec 24, 2016 6:11 pm
Ok, I will try to explain my point :

Code: Select all

@Entity
public class Employee

@OneToOne  // Requirement 1 of 2 is fulfilled "An employee can work in only one department"
private Department Department

Code: Select all

@Entity
public class Department

@OneToMany  // Requirement 2 of 2 is fulfilled "Department can have any number of Employees"
private Collection<Employee> employees
Those are two unidirectional associations (Problem does not say that it has to be bidirectional relationship)
The reason (I think) why this is wrong is that it makes the mistake of seeing each part of an integral whole ("An employee can work in only one department and a Department can have any number of Employees") as a part in and of itself, and it sees each such part as corresponding to the/a mapping annotation on one side. Or put more simply, it sees 2 requirements and not one.

Saying "An employee can work in only one department and a Department can have any number of Employees" is the same as saying, there is a one-to-many from dept to emp. This is one relationship. It's not two relationships. It's expressed in an ERD with a particular notation. It's expression in OO modelling with a particular notation. It's expressed in ORM in JPA in one of two ways, depending on whether it is unidirectional, bidirectional, or two unidirectionals; if unidirectional, there is either an @OneToMany or @ManyToOne, if bidirectional, you have both, with the many side having mappedBy; if there are 2 unidirectional then you ahve both without mappedBy.

Also note that it says this in ProJPA2:
The JPA annotations were designed to be readable, easy to specify, and flexible enough to allow different combinations of metadata...As with all trade-offs, the piper must be paid however, and the cost of flexibility is that many possible permutations of top-level metadata will be syntactically correct but semantically invalid. The compiler will be of no use, but the provider runtime will often do some basic checking for improper annotation groupings. The nature of annotations, however, is that when they are unexpected, they will often just not get noticed at all. This is worth remembering when attempting to understand behavior that might not match what you thought you specified in the annotations. It could be that one or more of the annotations are being ignored.

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests