无法让 DBUnit 为 CollectionTable 导入数据

发布于 2024-10-15 06:31:24 字数 6336 浏览 3 评论 0原文

我有以下实体:

    @Entity
    @Table
    @NamedQuery(name = Constants.FINDALLFINDERNAME,
                query = Constants.FINDALLQUERY)
    public class Customer extends AuditableEntity {

        ...

        @ElementCollection(fetch = FetchType.EAGER)
        @CollectionTable(name = Constants.PHONES, joinColumns = @JoinColumn(name = Constants.CUSTOMER_ID))
        @Column(name = Constants.CUSTOMER_PHONES, nullable = true)
        private List<Phone> phones;

我想在测试期间将以下数据导入到我的架构中:

    <?xml version='1.0' encoding='UTF-8'?>
    <dataset>
        <CUSTOMER id='101' USERNAME="user1" FIRSTNAME="Mick" LASTNAME="Knutson"/>

        <PHONES AREACODE="415" P_NUMBER="5551212" TYPE="WORK" CUST_ID="101" />
    </dataset>

但我收到此错误:

    [EL Info]: 2011-01-31 10:57:56.945--ClientSession(30624226)--Communication failure detected when attempting to create transaction on database.  Attempting to retry begin transaction. Error was: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110129-r8902): org.eclipse.persistence.exceptions.DatabaseException
    Internal Exception: org.h2.jdbc.JdbcSQLException: The object is already closed [90007-148]
    Error Code: 90007.
    Dataset written

    org.dbunit.dataset.NoPrimaryKeyException: PHONES

这是我的单元测试:

    ...

    public class CustomerTest {

        //-----------------------------------------------------------------------//
        // Attributes
        //-----------------------------------------------------------------------//
        private static EntityManagerFactory emf;
        private static EntityManager em;
        private static EntityTransaction tx;

        //-----------------------------------------------------------------------//
        // Lifecycle Methods
        //-----------------------------------------------------------------------//
        @BeforeClass
        public static void initEntityManager() throws Exception {
            emf = Persistence.createEntityManagerFactory(Constants.PERSISTENCEUNIT);
            em = emf.createEntityManager();
        }

        @AfterClass
        public static void closeEntityManager() throws SQLException {
            if (em != null) em.close();
            if (emf != null) emf.close();
        }

        @Before
        public void initTransaction() throws Exception {
            tx = em.getTransaction();
            seedData();
        }

        @After
        public void afterTests() throws Exception {
            dumpData();
        }

        //-----------------------------------------------------------------------//
        // Unit Tests
        //-----------------------------------------------------------------------//

        @Test
        public void test__Create__and__Read_SingleCustomer() throws Exception {
            // Creates an instance of Customer
            Customer customer = CustomerFixture.createSingleCustomer();

            // Persists the Customer to the database
            tx.begin();
            em.persist(customer);
            tx.commit();

            tx.begin();
            assertNotNull("ID should not be null", customer.getId());
            // Retrieves a single Customer from the database
            TypedQuery<Customer> q = em.createNamedQuery(
                    Constants.FINDALLFINDERNAME, Customer.class);
            List<Customer> customers = q.getResultList();
            assertThat(customers.size(), is(4));
            tx.commit();
        }

        @Test
        public void test__DeleteCustomer() throws Exception {
            tx.begin();

            // Uses Sting Based Criteria
            CriteriaBuilder cb = em.getCriteriaBuilder();
            CriteriaQuery<Customer> c = cb.createQuery(Customer.class);
            Root<Customer> cust = c.from(Customer.class);
            c.select(cust)
                    .where(cb.equal(cust.get("username"), "user1"));
            Customer result = em.createQuery(c).getSingleResult();

            em.remove(result);

            // Retrieves all the Customers from the database
            TypedQuery<Customer> q = em.createNamedQuery(
                    Constants.FINDALLFINDERNAME, Customer.class);
            List<Customer> customers = q.getResultList();

            tx.commit();

            assertThat(customers.size(), is(3));
        }                    

        //-----------------------------------------------------------------------//
        // DBUnit Helper Methods
        //-----------------------------------------------------------------------//

        protected void seedData() throws Exception {
            tx.begin();
            Connection connection = em.unwrap(java.sql.Connection.class);

            try {
                IDatabaseConnection dbUnitCon = new DatabaseConnection(connection);

                dbUnitCon.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY,
                        new H2DataTypeFactory());

                IDataSet dataSet = getDataSet();

                DatabaseOperation.REFRESH.execute(dbUnitCon, dataSet);
            } finally {
                tx.commit();
                connection.close();
            }
        }

        protected IDataSet getDataSet() throws Exception {
            return new FlatXmlDataSetBuilder().build(new FileInputStream("./src/test/resources/dataset.xml"));
        }

        protected void dumpData() throws Exception {
            tx.begin();
            Connection connection = em.unwrap(java.sql.Connection.class);

            try {
                IDatabaseConnection dbUnitCon = new DatabaseConnection(connection);
                dbUnitCon.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY,
                        new H2DataTypeFactory());

                IDataSet dataSet = dbUnitCon.createDataSet();

                FlatXmlDataSet.write(dataSet, new FileOutputStream("./target/test-dataset_dump.xml"));
                System.out.println("Dataset written");
            } finally {
                tx.commit();
                connection.close();
            }
        }
    }

有人可以帮助我了解如何使用 DBUnit 导入 CollectionTables 吗?

I have the following Entity:

    @Entity
    @Table
    @NamedQuery(name = Constants.FINDALLFINDERNAME,
                query = Constants.FINDALLQUERY)
    public class Customer extends AuditableEntity {

        ...

        @ElementCollection(fetch = FetchType.EAGER)
        @CollectionTable(name = Constants.PHONES, joinColumns = @JoinColumn(name = Constants.CUSTOMER_ID))
        @Column(name = Constants.CUSTOMER_PHONES, nullable = true)
        private List<Phone> phones;

I want to import the following data into my schema during a test:

    <?xml version='1.0' encoding='UTF-8'?>
    <dataset>
        <CUSTOMER id='101' USERNAME="user1" FIRSTNAME="Mick" LASTNAME="Knutson"/>

        <PHONES AREACODE="415" P_NUMBER="5551212" TYPE="WORK" CUST_ID="101" />
    </dataset>

But I get this error:

    [EL Info]: 2011-01-31 10:57:56.945--ClientSession(30624226)--Communication failure detected when attempting to create transaction on database.  Attempting to retry begin transaction. Error was: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110129-r8902): org.eclipse.persistence.exceptions.DatabaseException
    Internal Exception: org.h2.jdbc.JdbcSQLException: The object is already closed [90007-148]
    Error Code: 90007.
    Dataset written

    org.dbunit.dataset.NoPrimaryKeyException: PHONES

Here is my unit test:

    ...

    public class CustomerTest {

        //-----------------------------------------------------------------------//
        // Attributes
        //-----------------------------------------------------------------------//
        private static EntityManagerFactory emf;
        private static EntityManager em;
        private static EntityTransaction tx;

        //-----------------------------------------------------------------------//
        // Lifecycle Methods
        //-----------------------------------------------------------------------//
        @BeforeClass
        public static void initEntityManager() throws Exception {
            emf = Persistence.createEntityManagerFactory(Constants.PERSISTENCEUNIT);
            em = emf.createEntityManager();
        }

        @AfterClass
        public static void closeEntityManager() throws SQLException {
            if (em != null) em.close();
            if (emf != null) emf.close();
        }

        @Before
        public void initTransaction() throws Exception {
            tx = em.getTransaction();
            seedData();
        }

        @After
        public void afterTests() throws Exception {
            dumpData();
        }

        //-----------------------------------------------------------------------//
        // Unit Tests
        //-----------------------------------------------------------------------//

        @Test
        public void test__Create__and__Read_SingleCustomer() throws Exception {
            // Creates an instance of Customer
            Customer customer = CustomerFixture.createSingleCustomer();

            // Persists the Customer to the database
            tx.begin();
            em.persist(customer);
            tx.commit();

            tx.begin();
            assertNotNull("ID should not be null", customer.getId());
            // Retrieves a single Customer from the database
            TypedQuery<Customer> q = em.createNamedQuery(
                    Constants.FINDALLFINDERNAME, Customer.class);
            List<Customer> customers = q.getResultList();
            assertThat(customers.size(), is(4));
            tx.commit();
        }

        @Test
        public void test__DeleteCustomer() throws Exception {
            tx.begin();

            // Uses Sting Based Criteria
            CriteriaBuilder cb = em.getCriteriaBuilder();
            CriteriaQuery<Customer> c = cb.createQuery(Customer.class);
            Root<Customer> cust = c.from(Customer.class);
            c.select(cust)
                    .where(cb.equal(cust.get("username"), "user1"));
            Customer result = em.createQuery(c).getSingleResult();

            em.remove(result);

            // Retrieves all the Customers from the database
            TypedQuery<Customer> q = em.createNamedQuery(
                    Constants.FINDALLFINDERNAME, Customer.class);
            List<Customer> customers = q.getResultList();

            tx.commit();

            assertThat(customers.size(), is(3));
        }                    

        //-----------------------------------------------------------------------//
        // DBUnit Helper Methods
        //-----------------------------------------------------------------------//

        protected void seedData() throws Exception {
            tx.begin();
            Connection connection = em.unwrap(java.sql.Connection.class);

            try {
                IDatabaseConnection dbUnitCon = new DatabaseConnection(connection);

                dbUnitCon.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY,
                        new H2DataTypeFactory());

                IDataSet dataSet = getDataSet();

                DatabaseOperation.REFRESH.execute(dbUnitCon, dataSet);
            } finally {
                tx.commit();
                connection.close();
            }
        }

        protected IDataSet getDataSet() throws Exception {
            return new FlatXmlDataSetBuilder().build(new FileInputStream("./src/test/resources/dataset.xml"));
        }

        protected void dumpData() throws Exception {
            tx.begin();
            Connection connection = em.unwrap(java.sql.Connection.class);

            try {
                IDatabaseConnection dbUnitCon = new DatabaseConnection(connection);
                dbUnitCon.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY,
                        new H2DataTypeFactory());

                IDataSet dataSet = dbUnitCon.createDataSet();

                FlatXmlDataSet.write(dataSet, new FileOutputStream("./target/test-dataset_dump.xml"));
                System.out.println("Dataset written");
            } finally {
                tx.commit();
                connection.close();
            }
        }
    }

Can someone help me to understand how to import CollectionTables with DBUnit?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

何处潇湘 2024-10-22 06:31:25

我可以在此处输入链接描述并尝试解决方案,但对其进行了一些修改:

属性:

        // Set the property by passing the new IColumnFilter
        dbUnitCon.getConfig().setProperty(
                DatabaseConfig.PROPERTY_PRIMARY_KEY_FILTER,
                new NullPrimaryKeyFilter("ID", "ADDRESS_KEY", "P_NUMBER", "HOBBY_NAME"));

然后是过滤器类:

    class NullPrimaryKeyFilter implements IColumnFilter {
        private String[] keys = null;

        NullPrimaryKeyFilter(String... keys) {
            this.keys = keys;
        }

        public boolean accept(String tableName, Column column) {
            for(String key: keys){
                if(column.getColumnName().equalsIgnoreCase(key)){
                    return true;
                }
            }
            return false;
        }
    }

现在我可以在测试中对所有表使用过滤器。

I can across enter link description here and tried the solution, but modified it a bit:

The property:

        // Set the property by passing the new IColumnFilter
        dbUnitCon.getConfig().setProperty(
                DatabaseConfig.PROPERTY_PRIMARY_KEY_FILTER,
                new NullPrimaryKeyFilter("ID", "ADDRESS_KEY", "P_NUMBER", "HOBBY_NAME"));

then the Filter Class:

    class NullPrimaryKeyFilter implements IColumnFilter {
        private String[] keys = null;

        NullPrimaryKeyFilter(String... keys) {
            this.keys = keys;
        }

        public boolean accept(String tableName, Column column) {
            for(String key: keys){
                if(column.getColumnName().equalsIgnoreCase(key)){
                    return true;
                }
            }
            return false;
        }
    }

Now I can use the filter for all my TABLES in my test.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文