• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to know if any of the CMP fileds are modified ?

 
Mallik Hiremath
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We do have a requirements that says if any of the CMP EJB fields are modified then we need to set modified_by and modified_date fields also ( note: each CMP EJB has modified_by and modified_date CMP fields ).
I know that just before the end of a transaction container calls the ejbStore() method of the CMP bean. But its funny here, no matter what happened to the CMP fields i.e wheatear they are modified or not the container (9iAS OC4J )is going to call the ejbStore() and then it decides to write to DB or not ( i.e. after it calls the ejbStore it checks for the cmp fields internal dirty flags ).
I thought I will set the CMP fields updated_by and updated_date in the ejbStore method, but the problem is a call to ejbStore does not necessarily mean there were changes in the CMP fields, in that case if I set the updated_by, updated_date in the ejbStore its just these fields that are changed and cmp is going to write these two fields to DB with out any other fields being changed!!!
Does any one have any idea how to know from with in the CMP EJB if any one of the current CMP EJBs fields has changed their values?
Thank you
--Mallik
 
Rufus BugleWeed
Ranch Hand
Posts: 1551
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In every setter dirty = true;
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're writing a CMP, how can you implement setters yourself...?
 
Rufus BugleWeed
Ranch Hand
Posts: 1551
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somewhere you have a business method that modifies a persistant field.
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That will be lot of work. Every time I make a call to set methods I need to update a some boolean field which keeps track of whether the CMP fields are updated.
To me that is only option.

Originally posted by Rufus BugleWeed:
Somewhere you have a business method that modifies a persistant field.
 
Rufus BugleWeed
Ranch Hand
Posts: 1551
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If nothing is changed it may be that CMP will not update the record. So could one add a trigger that updates the fields whenever the table is changed?
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When using CMP, the container is probably using the dirty flag technique anyway and there's really nothing much one can do.
Here's a nice list of optimizations for entity beans:
http://developer.java.sun.com/developer/technicalArticles/ebeans/sevenrules/
 
Rufus BugleWeed
Ranch Hand
Posts: 1551
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose the trigger does not work because it does not know modified_by.
IMO, you dirty flag has to be a persisted field too, ejb_store must clear it. Because, you don't contol ejb_load either. Hopefully this will force the container to clear your flag.
 
Rufus BugleWeed
Ranch Hand
Posts: 1551
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stepping back from the problem, it appears your design is using an entity bean when it only needs read-only access. This is an EJB anti-pattern.
BTW, are Value Objects and Transfer Objects two names for the same concept or was Value Object closed to modification?
[ September 14, 2003: Message edited by: Rufus BugleWeed ]
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, are Value Objects and Transfer Objects two names for the same concept or was Value Object closed to modification?

You might want to have a look at this earlier thread.
 
Mallik Hiremath
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all of you,
But I wonder I did not get any clear answer for my question, I hope so many people would have faced the same problem and might have some soulution.
Let me explain my requirement here once again,
Before container writes the CMP EJB to DB I have to set up the UPDATED_BY ( = the current user's loginID ) and UPDATED_DATE (= the current system date) for that particular record. If CMP is not going to write that particular row to the DB I don't want to set these two fields.
To my strange!!! the container calls the ejbStore() method and does not let me know if its going to write to DB for sure.
I am only interested in knowing if the CMP is going to write to DB for sure, I don't know how to get this information!!! if some one knows please let me know.
I know one method of implementing this, but I feel its too lengthy not consistant with the J2EE api's, this is done as follows
1. declare a CMP EJB instance variable boolean flag and name it as isModified or some thing and set it to flase at the end of ejb post create method and in the ejbLoad() method and also at the end of ejbStore() method.
2. Write a wrapper methods for each of the setField methods such as updateField() and with in this wrapper method set isModified to true and then call the corresponding setField() method.
3. Any EJB clients that wants to set some new value to a field that client has to use the updateField() method instead of setField() method
oooooooooooooooops so much of work!!!
Now querying the isModified field of the CMP would let me know if its going to write to DB for sure.
I can check for this status in the ejbStore() method and then decied to set the other two field values i.e UPDATED_BY and UPDATED_DATE values of the CMP EJB Fields.
Thank you
Mallik
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!