Page 1 of 1

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

Posted: Tue Sep 22, 2015 7:59 am
by Igor Makarov
Hi.

Actually it's not completely clear from the question what actually we want to achieve.
The question says:
"You have a ManyToMany bidirectional relationship between Student and Course entities. You want the order of Students who register for a Course to be preserved but you do not want any additional persistent field in the entities to support this."

Let's summarize it:
1) We have @ManyToMany
2) We want to preserve the order of elements but we don't want to create a new persistence fields in Entity.

I chose the answer "Use @OrderBy annotation on Student collection field in Course entity" and failed.

The explanation of the question says that we can't use @OrderBy because we don't want to specify any persistence column in it
("orderBy is used when you have a persistent field in the entity of the collection by which you want to order. ")

But what if we didn't specify any column in @OrderBy at all. In this case the persistence provider will use identity as a sorting criteria.
I see that it's quite legal answer to the questions because we preserved order and didn't create new persistence columns, isn't it?

Thanks,
Igor.

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

Posted: Wed Sep 23, 2015 11:33 am
by romsky
@OrderBy does not need "any additional persistent field in the entities", because it is defaulted by identity.

Your sample is misleading. Following works perfect w/o additional field "registrationDate".

@Entity
public class Course {   

@ManyToMany   
@OrderBy   
List<Student> students;
}

The question has two correct options.

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

Posted: Wed Sep 23, 2015 6:20 pm
by admin
Could you please point me to the spec where it says order is defaulted by Identity?

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

Posted: Thu Sep 24, 2015 10:11 am
by romsky
If the ordering element is not specified for an entity association, ordering by the primary key of the
associated entity is assumed.

@Target({METHOD, FIELD}) @Retention(RUNTIME)
public @interface OrderBy {
String value() default "";
}

Default: Ascending ordering by the primary key.

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

Posted: Thu Sep 24, 2015 2:08 pm
by Igor Makarov
http://docs.oracle.com/javaee/6/api/jav ... derBy.html

Also JPA 2.0 specification chapter 11.1.38

If the ordering element is not specified, ordering by the primary key of the associated entity is assumed.

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

Posted: Fri Sep 25, 2015 8:40 am
by admin
Igor Makarov wrote:http://docs.oracle.com/javaee/6/api/jav ... derBy.html

Also JPA 2.0 specification chapter 11.1.38

If the ordering element is not specified, ordering by the primary key of the associated entity is assumed.
I am not sure what am I missing.
Let's say you have Student 1 (PK 1) and Student 2 (PK 2) and a Course C1. Now, if Student 2 registers for the Course before Student 1, how will an empty OrderBy solve the problem?

The problem statement clearly states that you want to preserve the order in which students register for a course. That means, if you get a list of registered students from the course C1, you should get S2 and S1 in that order. Not S1 and S2.

-Paul.