users@jersey.java.net

Re: [Jersey] How to insert using a default pk assignment?

From: Marc Hadley <Marc.Hadley_at_Sun.COM>
Date: Wed, 21 Jan 2009 14:25:07 -0500

On Jan 20, 2009, at 5:26 PM, Marsh Eric wrote:
> Regarding the problem that I'm experiencing, I had previously
> removed the put/get methods along with the field descriptor from one
> of the generated entity files for a column that I didn't want to
> have INSERTED into the database. The effect was that all references
> to that column disappeared from the INSERT statement. However I
> can't do the same with the primary key because of the other
> dependencies on it, though again I simply don't want it to be
> INSERTED.
>
Perhaps you need to tweak the generated JPA classes. If the primary
key is autogenerated by the DB then you'll need to add something like:

     @Id
     @GeneratedValue(strategy = GenerationType.IDENTITY)

to the corresponding field to let the JPA engine know not to try to
insert a value.

HTH,
Marc.

> Here's the JSON text that I sent via an HTTP POST which is
> responsible for the exception below. Note that at the time the date
> formatter wasn't working right which is why the date was insert into
> the database as a NULL.
> However, beyond that it looks like I'm getting close to having this
> thing working after a period of frustration. Thanks for the help.
> BTW, any what code might be causing the duplicate records that are
> appearing from a GET?
> {"accuracy":"17",
> "mykey":{"@uri":"http://192.168.0.3:8080/HereIAmDB/resources/peoples/emarsh/
> "},
> "@uri":"http://192.168.0.3:8080/HereIAmDB/resources/mylocations/",
> "longitude":"-97.62597",
> "time":"2009-19-19T11:19:53-06:00" ,
> "latitude":"29.92574",
> "peoplecollection":{"people":[{"@uri":"http://192.168.0.3:8080/HereIAmDB/resources/peoples/
> "}],
> "uri":"http://192.168.0.3:8080/HereIAmDB/resources/peoples/"}}
>
> --------
> Hi Eric,
>
> Jersey does not do anything with databases. Are you referring to
> NetBeans generated code?
>
> Could you describe the HTTP operation you performed and with what
> data?
>
> Paul.
>
>
> On Jan 20, 2009, at 7:01 PM, Marsh Eric wrote:
>
> > Hello,
> >
> > I'm working on inserting a new record that was created remotely and
> > then passed via RESTful services and inserted into a common
> > database. Because of the record's remote creation it does not know
> > what primary key will be used when it is inserted into the main
> > database. The database itself will generate the primary key's value.
> >
> > The problem I'm experiencing is that when I pass a record to the
> > local server without a primary key value being passed a null value
> > is being generated for the primary key somewhere in the Jersey
> > layers the system is attempting to insert it. This is causing the
> > following exception:
> >
> > Local Exception Stack:
> > Exception [TOPLINK-4002] (Oracle TopLink Essentials - 2.0.1 (Build
> > b09d-fcs (12/06/2007))):
> > oracle.toplink.essentials.exceptions.DatabaseException
> > Internal Exception: org.postgresql.util.PSQLException: ERROR: null
> > value in column "pk" violates not-null constraint
> > Error Code: 0
> > Call: INSERT INTO mylocation (pk, accuracy, time, latitude, imat,
> > longitude, bindatakey, mykey) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
> > bind => [null, 17, null, 29.92574, null, -97.62597, null, 1]
> > Query: InsertObjectQuery(hereiam.Mylocation[pk=null])
> >
> > How can I prevent the system from attempting to insert a null value
> > (or for that matter any value) for the primary key? I've tried
> > changing the @Column value to "insertable = false" but that hasn't
> > worked.
> >
> > Thanks,
> >
> > Eric
> >
> >
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> > For additional commands, e-mail: users-help_at_jersey.dev.java.net
> >
>
>
>

---
Marc Hadley <marc.hadley at sun.com>
CTO Office, Sun Microsystems.