This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes EJB 3, Entity question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "EJB 3, Entity question" Watch "EJB 3, Entity question" New topic
Author

EJB 3, Entity question

Camilo Morales
Ranch Hand

Joined: Jul 14, 2007
Posts: 92
Hey,

I cant understand this question ... it may be very simple question but I just can't understand it. Please help:

13). Given an excerpt of a Book entity (it is just missing the import
statements):

10. @Entity
11. public class Book implements Serializable {
12. @Id
13. @GeneratedValue(strategy = GenerationType.AUTO)
14. private Integer id;
15. String bookName;
16. protected int price;
17. enum Status {IN, OUT};
18. @Enumerated( EnumType.ORDINAL )
19. Status status;
20. transient int bar;
21. java.util.Map<Integer, String> comments;
22. protected Book() {};
23. }

No descriptors are used.

Which statement is correct about this entity?

A) There is an error on line 11. It must NOT implement Serializable.
B) Adding a single @Transient annotation makes this entity valid.
C) The visibility declarations on some of the variables causes an
exception.
D) The enumeration or its field definition on lines 17, 18, or 19 is
NOT valid.
E) There is an error in the identity definition on lines 12, 13, or
14.


I already know the answer (anyway I don't understand it) but I think
it would be best if we could discuss the question here.

The first time I saw the question, even before reading the possible
answers I thought of this part of the specification:

"The persistent state of an entity is represented by instance
variables, which may correspond to Java-
Beans properties. An instance variable may be directly accessed only
from within the methods of the
entity by the entity instance itself. Instance variables must not be
accessed by clients of the entity. The
state of the entity is available to clients only through the entity's
accessor methods (getter/setter methods)
or other business methods. Instance variables must be private,
protected, or package visibility."

Anyway, it is NOT a MUST to use JavaBeans standards.

- So my Entity-generic check list is the following:

1- All the fields are private, protected or default.
2- The entity have a no-args constructor.
3- Default constructor is public or protected.
4- The entity have the @Id annotation.
5- Entities are allowed to have Enum type fields with the @Enumerated
annotation.
6- Collection type fields must be: Collection, List, Set or Map types.
(Generic syntax is allowed)
7- There are No embeddable classes or composite Id's. (This would make
this list larger)

After the check list I can discard A and C but not sure about the
rest.

So, everything on my check list is OK ... what am I missing here ?

Regards,


Camilo Morales<br />SCJP 5, SCBCD 5
Shivani Chandna
Ranch Hand

Joined: Sep 18, 2004
Posts: 380
Originally posted by Camilo Morales:

13). Given an excerpt of a Book entity (it is just missing the import
statements):

10. @Entity
11. public class Book implements Serializable {
12. @Id
13. @GeneratedValue(strategy = GenerationType.AUTO)
14. private Integer id;
15. String bookName;
16. protected int price;
17. enum Status {IN, OUT};
18. @Enumerated( EnumType.ORDINAL )
19. Status status;
20. transient int bar;
21. java.util.Map<Integer, String> comments;
22. protected Book() {};
23. }

No descriptors are used.

Which statement is correct about this entity?

A) There is an error on line 11. It must NOT implement Serializable.
B) Adding a single @Transient annotation makes this entity valid.
C) The visibility declarations on some of the variables causes an
exception.
D) The enumeration or its field definition on lines 17, 18, or 19 is
NOT valid.
E) There is an error in the identity definition on lines 12, 13, or
14.

After the check list I can discard A and C but not sure about the
rest.


A and C are definitely wrong.

Also B is wrong - if you check the specs it says that either the field is having the modifier transient or it is marked with annotation @Transient.

E is also wrong , for the id is annotated correctly.

This leaves D with the answer. But why D?

I had tried running a small example using enum and it started to give error for enum declared inside the entity. If the enum was declared in separate class and then used inside with the @Enumerated (EnumType.ORDINAL) it was working fine. I was using Jboss 4.2 container.

Definition of enum in OReilly's code - CustomerType enum was done in a separate file and then it was used in the entity class.

Any one to corroborate this?
Regards,
Shivani


/** Code speaks louder than words */
Benoît de Chateauvieux
Ranch Hand

Joined: Aug 10, 2007
Posts: 183
I ran the example in my machine and had the following error:

Caused by: Exception [TOPLINK-7155] (Oracle TopLink Essentials - 2.0 (Build b41-beta2 (03/30/2007))): oracle.toplink.essentials.exceptions.ValidationException
Exception Description: The type [interface java.util.Map] for the attribute [comments] on the entity class [class javaranch.Book] is not a valid type for a serialized mapping. The attribute type must implement the Serializable interface.

In ejb-3_0-fr-spec-persistence, chapter 2.1.1, i found:
"The persistent fields or properties of an entity may be of the following types: Java primitive types;
java.lang.String; other Java serializable types (including wrappers of the primitive types,
java.math.BigInteger, java.math.BigDecimal, java.util.Date,
java.util.Calendar[7], java.sql.Date, java.sql.Time, java.sql.Timestamp,
user-defined serializable types, byte[], Byte[], char[], and Character[]); enums; entity
types and/or collections of entity types; and embeddable classes (see section 2.1.5)."

So, I think that if you dont't use a relationship annotation (@OneToMany), it tries to persist it like a serializable field.
And Map isn't serializable (but HashMap, Hashtable, TreeMap are serializable).

And a question for you, Camilo: Where does the question comes from?
Thanks,

Beno�t

[ August 16, 2007: Message edited by: Beno�t de CHATEAUVIEUX ]
[ August 16, 2007: Message edited by: Beno�t de CHATEAUVIEUX ]

SCJP5 | SCBCD5 | SCEA5 Part 1
Camilo Morales
Ranch Hand

Joined: Jul 14, 2007
Posts: 92
Hey,

The question comes from this training test I found on Sun's website. Unfortunately the page does not give you the correct answer of each question ... just how many questions you had right or wrong in each section.

This question is the most difficult I found there ... I didnt have all the questions correct in the first attempt, but at least after a little while I understands the correct choice of almost all of them.

Thanks for your replies, according to the many attemps I've done in the Sun's webpage on this question ... the correct answer is B. I also discarted it before knowing the correct choice ... anyway ... thats the correct one.

Maybe the test have an error in this question ... LOL.

Regards,
Camilo Morales
Ranch Hand

Joined: Jul 14, 2007
Posts: 92
I forgot to copy/paste the link to the test:

https://suned.sun.com/secure/assessment/091.jsp

Regards,
Shivani Chandna
Ranch Hand

Joined: Sep 18, 2004
Posts: 380
Yes that's a little surprising - would test further and let you know.

Regards.
Camilo Morales
Ranch Hand

Joined: Jul 14, 2007
Posts: 92
I've been looking around and all the examples about the @Enumerated annotation have the ENUM definition outside the actual Entity class (for example in the EJB 3 Persistence Specs, page 181) ... and in the code examples of the Bill Burke O'Reilly's book, he actually use another .java file to define the enum.

I'll try that on my JBoss .. just posting this in case someone could confirm thats the correct explanation of the B answer.

Regards,
Benoît de Chateauvieux
Ranch Hand

Joined: Aug 10, 2007
Posts: 183
Yes: I think the correct answer is B.
You need to put a @Transient over the Map.
If you don't do it, the EJB container will try to persist the Map as a serializable object (because it doesn't have @OneToMay anotation).
And the interface Map doesn't implements Serializable.
So, without the @Transient over the Map, the entity isn't valid.

Your opinion ?

Beno�t
Camilo Morales
Ranch Hand

Joined: Jul 14, 2007
Posts: 92
Well, if Map do not implements Serializable ,,, it does make sense that there is a problem ,,, but then, Why the specification says this:

Collection-valued persistent fields and properties must be defined in terms of one of the following collection-
valued interfaces regardless of whether the entity class otherwise adheres to the JavaBeans
method conventions noted above and whether field or property-based access is used:
java.util.Collection, java.util.Set, java.util.List[4], java.util.Map.[5]

...

[4] Portable applications should not expect the order of lists to be maintained across persistence contexts unless the OrderBy construct
is used and the modifications to the list observe the specified ordering. The order is not otherwise persistent.
[5] The implementation type may be used by the application to initialize fields or properties before the entity is made persistent; subsequent
access must be through the interface type once the entity becomes managed (or detached).


also Mikalai Zaikin notes says:


The persistent fields or properties of an entity MAY BE of the following types:

*

Java primitive types
*

java.lang.String
*

other Java serializable types (including wrappers of the primitive types, java.math.BigInteger, java.math.BigDecimal, java.util.Date, java.util.Calendar, java.sql.Date, java.sql.Time, java.sql.Timestamp, user-defined serializable types, byte[], Byte[], char[], and Character[])
*

enums
*

entity types and/or collections of entity types
*

embeddable classes


I think that the problem is that Collection-types can only be used in a Many-related relationship ... and from the Relational point of view it does make sense ... because how is the Persistent Provider goin to persist a collection if it isnt with ForeingKey column without a Unique constraint (OnteToMany and ManyToOne bi-directional), or creating a new Relationship table (the case of ManyToMany Uni-directional and Bi-directional, and OneToMany and ManyToOne Uni-directional) ?

Anyway I didnt that explanation in the Specs or in the MZ notes ... I guess that if we are right ... they assumed we will never use Collection types for non-relationships persistent fields.

Still, I haven't test this on my JBoss ... I'll tell you later.

Regards,
Camilo Morales
Ranch Hand

Joined: Jul 14, 2007
Posts: 92
"Find" workd is missing in the last paragraph and have no sense without it:


Anyway I didnt FIND that explanation in the Specs or in the MZ notes ... I guess that if we are right ... they assumed we will never use Collection types for non-relationships persistent fields.
 
GeeCON Prague 2014
 
subject: EJB 3, Entity question