openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Kevin Sutter <kwsut...@gmail.com>
Subject Re: How to get generated keys without requerying the database?
Date Tue, 25 Jan 2011 15:13:17 GMT
To follow up on Jeremy's post...  We have junits for this test scenario, but
they are focused on the use of PK fields.  Like Jeremy said, this should
also work with non-PK fields...  The other thing to ensure is that your DB2
database is accurately being detected.  Not all databases support this
getGeneratedKeys() method capability, so we need to ensure that the target
database supports this functionality.  It might be worthwhile to turn on
TRACE to verify the database configuration is properly detected.

Good luck,
Kevin

On Tue, Jan 25, 2011 at 9:02 AM, Jeremy Bauer <techhusky@gmail.com> wrote:

> Hi,
>
> Based on the code snippets you've provided, it looks like you my just
> either
> need to call em.flush() to force the DB update or commit the transaction
> before getting the recId from the dto.  If that doesn't help, repost and
> I'll dig a little deeper.  OpenJPA refreshes primary key identity fields
> after a flush or commit.  I think it will/should do the same for non-PK
> generated fields.
>
> hth,
> -Jeremy
>
> On Mon, Jan 24, 2011 at 8:33 PM, robsinner <robsinner@yahoo.com> wrote:
>
> >
> > 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.
> >
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message