Today I came across one situation, here it is.
I have a db object to persist. I use SampleBean.add method. Inside that method, I’m getting primary key from a sequence, setting it to db object’s id property and persisting it to db (using PreparedStatement). But I could not get the db object’s id property (it comes as 0, even though it got set in persisting method) from the method I’am calling that bean method.
Please clarify me this.
DB Object
public class LogRow implements Serializable {
public long cardLogId;
public String data;
}
Persist method
public void insertLogRow(LogRow logRow) {
Connection con = null;
PreparedStatement ps = null;
try {
String insertSql = LogRow.INSERT_LOG;
con = global.getConnection();
if (logger.isEnabled(IMessage.DEBUG1)) {
logger.println(IMessage.DEBUG1, CLASS_NAME + "insertLogRow():: insertSql " + insertSql);
}
ps = con.prepareStatement(insertSql);
logRow.setLogId(seq.getNextVal());
ps.executeUpdate();
} catch (SQLException sqe) {
logger.println("insertLogRow():: SQLException occurred");
throw new ProcessingErrorException(sqe.getMessage());
}
}
I am able to see the persisted id value here in persist method.
Calling method
RemoteLookup.getSampleBean().insertLogRow(logRow);
log.println("log id " + logRow.logId());
This calling method prints 0
This is not an Answer:
Since the question is very difficult to understand I have tried to rephrase it, cant edit the question without understanding it.
I have an object say
Userto be saved in database, In mySampleBean.addmethod I fetch the id by getting the next val of some sequence sayuserSequence.The same value (fetched from
userSequenceis set as the id of User object.Now while persisting the object I am not getting the value of id which I have already set in
SampleBean.addmethod.For Persistence I am using
PreparedStatement. Can you help me in understanding why is this happening ?This part is Answer
Your are doing a RemoteLookup, and in Java Remote objects are not sent given copy instead it serializes it and sends it, if you want to see the changes done to your object in remove service then you will have to send your object back
So the code changes required:
Change your
insertLogRowmethod and make it return the object which it has persisted like:Now change the caller code:
Rebuild, deploy and run.
Now you should see your id which has been set on the object inside insertLogRow method.