• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

[JPA]which is conventional way saving an incoming CountryId which is carried by an drop down of form

 
Csaba Szegedi
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me start with an straight example.

We have a person profile with country object.

In the standard case:
We got an CountryId selected in the drop down. And we have to update the user profile with new Country object if it is needed.

My questions are:
1. Which is best way to creating an Country object from the CountryId ?
2. Which is best practice to comparing the incoming Country and the old Country whether they are equal or not ?

Is there any JPA example with drop downs in this field ?
Is there any built in JPA mechanism to solve the question above ?

Thanks in advance.

cscsaba





 
James Sutherland
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can get a Country for the CountryId using EntityManager.find(), or EntityManager.getReference().

The difference between the two is that getReference is allowed to return a proxy/un-fetched object, so may not access the database, even if the object is not in the cache.

If the country may not exists, then you should do a find() and if it returns null, then create a new Counter.

Assuming the Person is from the same persistence context, you can compare the Countries using either equals() or ==, as they should be the identical object if in the same persistence context.

You may also consider putting Country objects in the drop down in the first place, then you don't have to look up the country by id.
 
Csaba Szegedi
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello James,

Thanks for your quick reply.
I asked my question because I have an Profile object which accept the forms variables and all of the general types (int, string) can be loaded into the Profile automatically (Stripes framework) except the drop down served object ids.
And I have to make variables for the selected drop down ids.(it doesn't seem effective approach over the JPA)

All in all you suggest that the best practice is using the em.find(class,key) call.

Secondly, you prefer using equals, I thought using JPA (CascadeType=All) I can solve every CRUD operations over (Profile has Country) relation.

Thanks again, have a nice day!

cscsaba





James Sutherland wrote:You can get a Country for the CountryId using EntityManager.find(), or EntityManager.getReference().

The difference between the two is that getReference is allowed to return a proxy/un-fetched object, so may not access the database, even if the object is not in the cache.

If the country may not exists, then you should do a find() and if it returns null, then create a new Counter.

Assuming the Person is from the same persistence context, you can compare the Countries using either equals() or ==, as they should be the identical object if in the same persistence context.

You may also consider putting Country objects in the drop down in the first place, then you don't have to look up the country by id.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic