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

Moderator: admin

Post Reply
Igor Makarov
Posts: 14
Joined: Tue Sep 08, 2015 3:27 am
Contact:

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

Post 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.

romsky
Posts: 39
Joined: Thu Jan 29, 2015 4:49 pm
Contact:

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

Post 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.

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

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

Post by admin »

Could you please point me to the spec where it says order is defaulted by Identity?
If you like our products and services, please help us by posting your review here.

romsky
Posts: 39
Joined: Thu Jan 29, 2015 4:49 pm
Contact:

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

Post 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.

Igor Makarov
Posts: 14
Joined: Tue Sep 08, 2015 3:27 am
Contact:

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

Post 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.

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

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

Post 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.
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 14 guests