EJB CMP 实体 bean 何时实际创建

发布于 2024-09-12 16:05:43 字数 997 浏览 3 评论 0原文

我有一个提供业务方法的会话 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

本王不退位尔等都是臣 2024-09-19 16:05:43

我发现事务仅在业务方法完成时执行,因此第一个实体bean不会存储在数据库中以供检索。一个简单的解决方案如下

public void businessMethod(int number) {
    try {
        MyBeanHome home = lookupMyBean();
        DataSource dataSource = getMyDataSource();
        Statement statement = dataSource.getConnection().createStatement();
        ResultSet result;
        String tableName = "MYBEAN";
        result = statement.executeQuery("select max(ID) + 1 from " + tableName);
        result.next();
        int newID = result.getInt(1);
        for (int i = 0; i < number; i++) {
            System.out.println(newID);
            MyBeanLocal lineLocal = home.create(newID++);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

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

public void businessMethod(int number) {
    try {
        MyBeanHome home = lookupMyBean();
        DataSource dataSource = getMyDataSource();
        Statement statement = dataSource.getConnection().createStatement();
        ResultSet result;
        String tableName = "MYBEAN";
        result = statement.executeQuery("select max(ID) + 1 from " + tableName);
        result.next();
        int newID = result.getInt(1);
        for (int i = 0; i < number; i++) {
            System.out.println(newID);
            MyBeanLocal lineLocal = home.create(newID++);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文