openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Hong Jin Lu <luho...@cn.ibm.com>
Subject A problem using openjpa 1.0.0 -- em.merge(obj) generate insert into sql statment while enhancement at runtime
Date Tue, 13 May 2008 08:32:06 GMT

Hi, forks:
I was encountered a situation. And I am not sure whether it come out of my
dev environment or my code:

My Environment:
Windows XP, DB2, JDK5, OpenJPA 1.0.0

Code Scenario:
CaseType has a "many to many" relationship with CaseItemType
TestCase is using CaseType's DAO class

Code:
orm.xml
      <entity name="CaseType" class="CaseType">
            <table name="CASETYPE"></table>
            <named-query name="findCaseTypeByBusID">
                  <query>SELECT caseType FROM CaseType caseType where
caseType.typeCode = ?1</query>
            </named-query>
            <attributes>
                  <id name="typeCode">
                        <column name="CASETYPECODE" column-definition=
"VARCHAR(20)" />
                  </id>
                  <basic name="description">
                        <column name="DESCRIPTION"
                              column-definition="VARCHAR(20)" />
                  </basic>
                  <many-to-many name="caseItemTypes" target-entity=
"CaseItemType" fetch="LAZY">
                        <join-table name="CASETYPE_CASEITEMTYPE">
                               <join-column name="CASETYPECODE"
referenced-column-name="CASETYPECODE"/>
                               <inverse-join-column name="CASEITEMTYPECODE"
referenced-column-name="CASEITEMTYPECODE"/>
                        </join-table>
                        <cascade>
                              <cascade-merge />
                        </cascade>
                  </many-to-many>
            </attributes>
      </entity>

      <entity name="CaseItemType" class="CaseItemType">
            <table name="CASEITEMTYPE"></table>
            <named-query name="findCaseItemTypeByBusID">
                  <query>SELECT caseItemType FROM CaseItemType caseItemType
where caseItemType.typeCode = ?1</query>
            </named-query>
            <attributes>
                  <id name="typeCode">
                        <column name="CASEITEMTYPECODE" column-definition=
"VARCHAR(20)" />
                  </id>
                  <basic name="description">
                        <column name="DESCRIPTION"
                              column-definition="VARCHAR(20)" />
                  </basic>
            </attributes>
      </entity>

DAO java class :

      public String createCaseType(CaseType caseType) {
            if(log.isDebugEnabled()) {
                  log.debug("entering createCaseType method in
AbstractCaseTypeDAO class");
            }
            EntityManager em = emf.createEntityManager();
            EntityTransaction trans = em.getTransaction();
            trans.begin();
            if(caseType.getCaseItemTypes() != null) {
                  Iterator iter = caseType.getCaseItemTypes().iterator();
                  while(iter.hasNext()) {
                        CaseItemType caseItemType =
(CaseItemType)iter.next();
                        em.persist(caseItemType);
                  }
            }
            em.persist(caseType);
            trans.commit();
            em.close();
            if(log.isDebugEnabled()) {
                  log.debug("exiting createCaseType method in
AbstractCaseTypeDAO class");
            }
            return caseType.getTypeCode();
      }

      public CaseType updateCaseType(CaseType caseType) {
            if(log.isDebugEnabled()) {
                  log.debug("entering updateCaseType method in
AbstractCaseTypeDAO class");
            }
            EntityManager em = emf.createEntityManager();
            EntityTransaction trans = em.getTransaction();
            trans.begin();
            em.merge(caseType);
            trans.commit();
            em.close();
            if(log.isDebugEnabled()) {
                  log.debug("exiting updateCaseType method in
AbstractCaseTypeDAO class");
            }
            return caseType;
      }

Test Case:

      JPADAOFactory daoFactory = null;
      CaseTypeDAO caseTypeDAO = null;

      protected void setUp() throws Exception {
            daoFactory = JPADAOFactory.getInstance();
            caseTypeDAO = (CaseTypeDAO)daoFactory.getDAO("CaseTypeDAO");
      }

      protected void tearDown() throws Exception {
            daoFactory = null;
            caseTypeDAO = null;
      }


      public void testCreateCaseType() {
            CaseType caseType = new CaseType();
            caseType.setTypeCode("CtypeCode001");
            caseType.setDescription("Cdescription001");

            CaseItemType caseItemType = new CaseItemType();
            caseItemType.setTypeCode("CItypeCode001");
            caseItemType.setDescription("CIdescription001");
            caseType.getCaseItemTypes().add(caseItemType);
            String returnID = caseTypeDAO.createCaseType(caseType);
            Assert.assertEquals("CtypeCode001", returnID);
      }

      public void testUpdateCaseType() {
            CaseType caseType = new CaseType();
            caseType.setTypeCode("CtypeCode001");
            caseType.setDescription("Cdescription001U");

            caseType = caseTypeDAO.updateCaseType(caseType);

            Assert.assertEquals("CtypeCode001", caseType.getTypeCode());
      }

      public void testUpdateCaseTypeWithRef() {
            CaseType caseType = new CaseType();
            caseType.setTypeCode("CtypeCode001");
            caseType.setDescription("Cdescription001U");

            CaseItemType caseItemType = new CaseItemType();
            caseItemType.setTypeCode("CItypeCode001");
            caseItemType.setDescription("CIdescription001U");
            caseType.getCaseItemTypes().add(caseItemType);

            caseType = caseTypeDAO.updateCaseType(caseType);

            Assert.assertEquals("CtypeCode001", caseType.getTypeCode());
      }

Symptom (testCreateCaseType method could create data in DB2,
testUpdateCaseType method is not right):
when I run it with jvm argument: -javaagent:"D:/IBM/Share
Library/JPA/apache-openjpa-1.0.0/openjpa-1.0.0.jar"
 then exception I've got :
(See attached file: exception.txt)
However, if I remove jvm argument mentioned above, then the code could
work.

So Enhancement might result different SQL statement


Attachment:

(See attached file: code.zip)

Appreciate your help!
Hongjin Lu (鲁红金)
Developer, GCG Global Business Solution Centre (GBSC) - GBS Asset
Management@CDL
Tel: +86 10 82454718, TieLine: 9152244 x 4718, Fax: +86-10-8245-2904
E-mail: luhongj@cn.ibm.com
Address: 3/F DeShi Building, No. 9 ShangDi Dong Lu (East Road)  Haidian
District Beijing, 100094 P.R. China
Mime
View raw message