跨 bean 的对象引用
今天我遇到了一种情况,就是这样。
我有一个要保留的数据库对象。我使用 SampleBean.add 方法。在该方法中,我从序列中获取主键,将其设置为 db 对象的 id 属性并将其保存到 db (使用 PreparedStatement
)。但我无法从我调用该 bean 方法的方法中获取 db 对象的 id 属性(它为 0,即使它是在持久方法中设置的)。
请向我澄清这一点。
DB 对象
public class LogRow implements Serializable {
public long cardLogId;
public String data;
}
持久方法
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());
}
}
我可以在持久方法中看到持久的 id 值。
调用方法
RemoteLookup.getSampleBean().insertLogRow(logRow);
log.println("log id " + logRow.logId());
该调用方法打印 0
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是答案:
由于这个问题很难理解,我试图重新表述它,在不理解它的情况下无法编辑问题。
我有一个对象
User
保存在数据库中,在我的SampleBean.add
方法中,我通过获取某个序列的下一个 val 来获取 id,例如userSequence< /代码>。
相同的值(从
userSequence
获取)被设置为 User 对象的 id。现在,在保留该对象时,我没有获取已在
SampleBean.add
什么?
我正在使用
PreparedStatement
。 这部分是答案你正在做 远程查找,在 Java 远程对象中,不会发送给定的副本,而是将其序列化并发送它,如果您想在删除服务中查看对对象所做的更改,那么您必须将对象发回,
因此需要更改代码:
更改您的
insertLogRow
方法并使其返回已保存的对象,如下所示:现在更改调用者代码:
重建、部署并运行。
现在您应该看到已在 insertLogRow 方法内的对象上设置的 id。
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
User
to be saved in database, In mySampleBean.add
method I fetch the id by getting the next val of some sequence sayuserSequence
.The same value (fetched from
userSequence
is 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.add
method.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
insertLogRow
method 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.
在我看来,在构建准备好的语句后,您缺少
setLong(1, logRow.getCardLogId())
调用。我假设该语句类似于:然后您需要设置
?
参数的值:但也许该对象是某种神奇的 EJB 代理,数据库会自行更新吗?
Looks to me that you are missing a
setLong(1, logRow.getCardLogId())
call after you build your prepared statement. I assume the statement is something like:Then you need to set the values for the
?
arguments:But maybe the object is some sort of magic EJB proxy that does the database update itself?