Forums Register Login

Sun Assessment Exam Question 2

+Pie Number of slices to send: Send
# 16. A domain model comprises two entities: Dog and Flea. The developer is required to construct these entities so that the Dog and Flea entities are in a unidirectional one-to-many relationship.
#
# Which statement about implementing this model is NOT correct?
# The @OneToMany annotation must be placed in the Dog entity.
# The @ManyToOne annotation must NOT be used on the Flea entity.
# A join table can be used to implement this relationship.
# The Flea entity will be the owning side of the relationship.

the correct answer is 4. Why 2nd option is incorrect.
+Pie Number of slices to send: Send
Dog has one to many relationship with Flea...that means one dog can have many fleas, that means the Flea table will have the foreign key to the Dog table...(Flea table should have something like dogId, referencing soe Dog)...The table containing the FK is always the owning side of the relationship...so 4 is correct...
3 is not correct because a Join table will(should) only be used if it is a many to many relationship....but here it's one to many...
+Pie Number of slices to send: Send
the structure will look like this

@Entity
public class Dog{
...
private Collection<Flea> flea=new ArrayList<Flea>();

@One to Many
@JoinColumn(name="FLEA_ID")
public Collection<Flea> getFlea(){
return flea;
}

...
}

@Entity
public class Flea{
...

}

No need of having mapping information in Flea class since this is unidirectional.

Dog class is the owner side since it is annotated by @JoinColumn.

This approach can also be implemented by join tables.

@Entity
public class Dog{
...
private Collection<Flea> flea=new ArrayList<Flea>();

@One to Many
@JoinTable(name="DOG_FLEA"),
joinColumns{@JoinColumn(name="DOG_ID")},
inverseJoinColumns={@JoinColumn(name="FLEA_ID")}
)
public Collection<Flea> getFlea(){
return flea;
}

...
}



+Pie Number of slices to send: Send
Thank's Rahul for your reply.

Sorry i want to actually tell, 2nd is the right answer. I inculded the question as also answer. So that's my numbering changed.


anyway, read the question clearfully

Which statement about implementing this model is NOT correct?



# The Flea entity will be the owning side of the relationship.



it is asking which is not correct. but 4th is correct statement. so 2nd should be the answer.
+Pie Number of slices to send: Send
Hi,

Rahul - I disagree with "3 is not correct because a Join table will(should) only be used if it is a many to many relationship....but here it's one to many... "

If the relationship is a one-to-many, and it's *unidirectional*, you will need a join table.
Otherwise, (from a db perspective) how would you store multiple references to fleas in a single dog row? That doesn't make sense.
I accept that you could put the reference on the flea side, but now you've changed the direction.

As an example, "Mastering EJBs", page 245 says
"One-to-many relationships are also typically set up by a foreign key relationship
in the database. In a unidirectional one-to-many relationship, the
application server automatically generates a join table.
"

I'll qualify this by saying that I am new to EJB, so welcome correction/explanation if I'm wrong.

Cheers,

MG
Don't sweat petty things, or pet sweaty things. But cuddle this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 972 times.
Similar Threads
Answers of Sun's Free Proficiency Assessment
Sun Assessment Exam Question
Entities association relationship defaults - Choose Correct statement
A entity question
Newbie Question: What's the relationship between JPA and EJB?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 05:04:35.