About Question enthuware.oce-jpad.v6.2.526 :
Moderator: admin
About Question enthuware.oce-jpad.v6.2.526 :
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.
Online
-
- Site Admin
- Posts: 10170
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.oce-jpad.v6.2.526 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 197
- Joined: Mon Jun 20, 2016 5:06 pm
- Contact:
Re: About Question enthuware.oce-jpad.v6.2.526 :
Could you please advise why
Does not satisfy requirement of Employee having only one department and @ManyToOne is required.
Code: Select all
@Entity
public class Employee
@OneToOne
private Department department;
Online
-
- Site Admin
- Posts: 10170
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.oce-jpad.v6.2.526 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 197
- Joined: Mon Jun 20, 2016 5:06 pm
- Contact:
Re: About Question enthuware.oce-jpad.v6.2.526 :
But it's possible to have one to one from Employee to Department, and many to one from Department to Employee (2 unidirectional relationships)
Online
-
- Site Admin
- Posts: 10170
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.oce-jpad.v6.2.526 :
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.
-
- Posts: 197
- Joined: Mon Jun 20, 2016 5:06 pm
- Contact:
Re: About Question enthuware.oce-jpad.v6.2.526 :
Ok, I will try to explain my point :
Those are two unidirectional associations (Problem does not say that it has to be bidirectional relationship)
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
Online
-
- Site Admin
- Posts: 10170
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.oce-jpad.v6.2.526 :
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.
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.
-
- Posts: 197
- Joined: Mon Jun 20, 2016 5:06 pm
- Contact:
Re: About Question enthuware.oce-jpad.v6.2.526 :
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.
Online
-
- Site Admin
- Posts: 10170
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.oce-jpad.v6.2.526 :
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
If you like our products and services, please help us by posting your review here.
-
- Posts: 197
- Joined: Mon Jun 20, 2016 5:06 pm
- Contact:
Re: About Question enthuware.oce-jpad.v6.2.526 :
I think it does not, it would do so only in case of bidirectional relationship, which is not the case here.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.
-
- Posts: 125
- Joined: Thu Jul 05, 2018 6:44 pm
- Contact:
Re: About Question enthuware.oce-jpad.v6.2.526 :
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.johnlong wrote: ↑Sat Dec 24, 2016 6:11 pmOk, 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
Those are two unidirectional associations (Problem does not say that it has to be bidirectional relationship)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
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.
Who is online
Users browsing this forum: No registered users and 1 guest