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 and other Java EE Technologies and the fly likes CMP - CMR for the same field? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "CMP - CMR for the same field?" Watch "CMP - CMR for the same field?" New topic
Author

CMP - CMR for the same field?

Kim Jensen
Greenhorn

Joined: Jun 28, 2004
Posts: 3
Hi,
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,
Kim
Alex Sharkoff
Ranch Hand

Joined: Apr 11, 2004
Posts: 209
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.


Alex (SCJP 1.4, SCBCD 1.3, SCWCD 1.4, SCJD 1.4)
 
GeeCON Prague 2014
 
subject: CMP - CMR for the same field?