I have an Entity with a non primary key identity column. 

This entity  
Part.java
has the following fields among others

        @EmbeddedId
        private PartPK pk;


        @GeneratedValue(strategy=GenerationType.IDENTITY)
        @Column(name="REC_ID")
        private Integer recId;

Note the EmbeddedId does not contain the  Generated Value , the PartPk is
made up of two strings, one for company and one for part number.

So the Entity has a primary key which is  a business composite key. This
table also has a column in DB2 which is a IDENTITY column which is not part
of the primary key. This identity column which is not part of the primary
key is called REC_ID.

After insert I would like the recId to be populated onto the entity however
it is not. In JDBC this is usually accomplished via
statement.getGeneratedKeys after the insert statements has been run. 

For example in a straight JDBC Data Access Object this would be accomplished
after the insert statement by 
                        // retrieve values from auto-increment columns
                        rs = stmt.getGeneratedKeys();
                        if (rs != null && rs.next()) {
                                dto.setRecId( new Integer( rs.getInt(1) ) );
                        }

However when I run a junit test using a simple JPA data access object the
recId column is null after insert. 

        public void testInsert() 
        {
                        log.info("insert");
                        log.info("part="+part);

                        manager = getManager();
                        
                        pk.setIncomp(comp);
                        pk.setInpart(part);

                        dto.setPk(pk);

                        log.info(dto);
                        manager.insert(dto);
                                                
                        log.info("recId="+dto.getRecId());
                        assertFalse("recId is null after insert should not be", 
dto.getRecId() ==
null); //fails here
      }


The manager class is wrapping this data access object code

        public PK insert(DTO dto) throws Exception {
                EntityManager em = getEntityManager();
                em.persist(dto);
                return dto.getPk();
        }
 
How do I specify the mapping in the entity with a non primary key identity
column such that after an insert the non primary key identity column is
populated onto the entity. 

Among other things I am using Db2 with spring and openjpa 2.2.0.

I believe this has something to do with getGeneratedKeys and how I'm doing
the mapping in the entity and possibly DB2 with openjpa.

The table are legacy and are not easily modifiable to accomplish this. Im
trying to avoid having to relook up the non primary key with a seperate
query by the primary key after the insert. 

Thank you in advance for your assistance. I searched in the nabble users
list and could not find anything related. If you need more detailed
information I can post more. I believe this is a simple configuration done
incorrectly at the entity level. 

Thank you in advance for any assistance you provide.
-- 
View this message in context: 
http://openjpa.208410.n2.nabble.com/How-to-get-generated-keys-without-requerying-the-database-tp5957346p5957346.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Reply via email to