This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

CMP - CMR for the same field?  RSS feed

Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't find anything in the specification about this.
Do you know if it is possible to have the same field represented as both a CMP and a CMR field? E.g. sometimes you want the value for the field (the actual foreign key value) but at other times you want the relationship.

If so, then I guess it is not allowed to have a set method for the CMP field?

Thanks in advance,
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kim,

Did you mean whether the same db table column could be represented as both a CMP and a CMR field?

As far as I understand such representation is legal. If your entity bean is read-only bean then there are no problems.

However, the problem will arise when a bean is a read/write bean.

For instance, a client updates entity's property by calling a setter on CMP field (or cmr field) and then while still in the same transaction accesses the same entity property through a getter on CMR field. CMR field getter method will return an invalid data provided that it was not refreshed (container did not call ejbLoad()). CMR field will be still be associated with the entity property's data that used to be associated with cmp field.

Or a real life example. Account can have one Employee (many-to-one relationship, on db side Account table has a foreign constraint to Employee). AccountBean has exposed Employee as a cmp field (mapped to foreign key column) and as a cmr field (mapped to EmployeeBean's local interface). In the beginning of a transaction Account is associated with an Employee whose primary key is "12" - cmp field getter returns "12" and cmr field returns EmployeeBean (its local comp interface) with primary key of "12". While in the same transaction employee cmp field has been updated (through cmp field setter) to "24". Then in the same transaction AccountBean is asked for an employee CMR field, which did not get auto updated and therefore will return an EmployeeBean with primary key of "12".

We could do synchronization of Employee cmp and cmr fields in AccountBean (eg, when set cmp field -> find EmployeeBean by pk and set cmr field) but it does not seem as a very smart way to do things with entity beans.

In conclusion, I'd say that an entity's property should be exposed with cmp field or with cmr field but not both at the same time.

Hope it helps.
When people don’t understand what you are doing they call you crazy. But this tiny ad just doesn't care:
how do I do my own kindle-like thing - without amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!