EJB CMP 实体 bean 何时实际创建
我有一个提供业务方法的会话 bean,在该方法中它创建了几个 CMP 实体 bean,如下所示
public void businessMethod(int number) {
try {
MyBeanHome home = lookupMyBean();
DataSource dataSource = getMyDataSource();
Statement statement = dataSource.getConnection().createStatement();
ResultSet result;
String tableName = "MYBEAN";
for (int i = 0; i < number; i++) {
result = statement.executeQuery("select max(ID) + 1 from " + tableName);
result.next();
int newID = result.getInt(1);
System.out.println(newID);
MyBeanLocal lineLocal = home.create(newID);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
MyBean
的 create 方法只是创建一个带有 newID
的新 bean。但是,上述代码仅适用于 number = 1
。如果数字> 1
,它尝试创建具有相同 ID 的第二个 bean(System.out.println(newID);
打印相同的值)。我猜测新的 bean 尚未存储在数据库中,因此查询返回相同的值。对此可以做什么呢?
非常感谢!
I have a session bean that provides a business method, in which it creates several CMP entity beans, something like this
public void businessMethod(int number) {
try {
MyBeanHome home = lookupMyBean();
DataSource dataSource = getMyDataSource();
Statement statement = dataSource.getConnection().createStatement();
ResultSet result;
String tableName = "MYBEAN";
for (int i = 0; i < number; i++) {
result = statement.executeQuery("select max(ID) + 1 from " + tableName);
result.next();
int newID = result.getInt(1);
System.out.println(newID);
MyBeanLocal lineLocal = home.create(newID);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
The create method of MyBean
simply creates a new bean with newID
. However, the above code only works with number = 1
. If number > 1
, it tries to create a second bean with the same ID (System.out.println(newID);
prints the same value). I'm guessing the new bean is not stored in the database yet, and thus the query returns the same value. What can be done to this?
Many thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现事务仅在业务方法完成时执行,因此第一个实体bean不会存储在数据库中以供检索。一个简单的解决方案如下
I figured it out that the transaction is only executed when the business method finishes, so the first entity beans would not be stored in the database for retrieval. A simple solution is below