File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sun Assessment Exam Question 2

 
Amandeep Singh
Ranch Hand
Posts: 850
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
# 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.
 
Rahul Babbar
Ranch Hand
Posts: 210
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Manish Shah
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
}

...
}



 
Amandeep Singh
Ranch Hand
Posts: 850
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mark Garland
Ranch Hand
Posts: 226
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic