Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Negative Ids
Negative Ids [message #390599] Tue, 28 July 2009 09:28 Go to next message
Enrico is currently offline Enrico
Messages: 82
Registered: July 2009
Member
Hi All,

in my DB I have tables with negative id fields.
It seems I have problem when I work with negative id using eclipselink
(1.1.1). I know there is option (in persistence.xml) to allow zero id
values...but for negative values it should work or there is some option
to enable?

BR,
Enrico
Re: Negative Ids [message #390600 is a reply to message #390599] Tue, 28 July 2009 10:37 Go to previous messageGo to next message
Enrico is currently offline Enrico
Messages: 82
Registered: July 2009
Member
I tried to enable the zero id value:

<property name="eclipselink.allow-zero-id" value="true"/>

what happen now is that it seems there is some kind of conflict with the
@GeneratedValue in my Entity. Trying to make 2 entities persistent does
not work..just one is stored on DB, while the other is niot stored (like
both entities have the same id = 0, and only one is persistent).
Enabling the finest eclipse logging I see only one insert event if I
have two entities object.

Any suggestion?

BR,
Enrico

Enrico ha scritto:
> Hi All,
>
> in my DB I have tables with negative id fields.
> It seems I have problem when I work with negative id using eclipselink
> (1.1.1). I know there is option (in persistence.xml) to allow zero id
> values...but for negative values it should work or there is some option
> to enable?
>
> BR,
> Enrico
Re: Negative Ids [message #390602 is a reply to message #390600] Tue, 28 July 2009 12:40 Go to previous messageGo to next message
Tom Eugelink is currently offline Tom Eugelink
Messages: 806
Registered: July 2009
Senior Member
maybe the equals method declares them equal?


Enrico wrote:
> I tried to enable the zero id value:
>
> <property name="eclipselink.allow-zero-id" value="true"/>
>
> what happen now is that it seems there is some kind of conflict with the
> @GeneratedValue in my Entity. Trying to make 2 entities persistent does
> not work..just one is stored on DB, while the other is niot stored (like
> both entities have the same id = 0, and only one is persistent).
> Enabling the finest eclipse logging I see only one insert event if I
> have two entities object.
>
> Any suggestion?
>
> BR,
> Enrico
>
> Enrico ha scritto:
>> Hi All,
>>
>> in my DB I have tables with negative id fields.
>> It seems I have problem when I work with negative id using eclipselink
>> (1.1.1). I know there is option (in persistence.xml) to allow zero id
>> values...but for negative values it should work or there is some
>> option to enable?
>>
>> BR,
>> Enrico
Re: Negative Ids [message #390603 is a reply to message #390600] Tue, 28 July 2009 14:31 Go to previous messageGo to next message
James Sutherland is currently offline James Sutherland
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

I don't think currently singleton primitive negative ids are allowed
without the flag (non-primitive or composite are ok, 0 or negative).

Are you trying to allow negative ids on something that uses sequencing?
That would be odd, but if you allow 0, and have a primitive int/long id
then it will default to 0, so EclipseLink will think your two new objects
are the same object. You would need to use a Integer or Long so that the
value is null when not assigned, or remove the allow zero id flag. You
can also set allowing 0 as an id on a per class basis using the
@PrimaryKey and IdValidation settings,

See,
http://www.eclipse.org/eclipselink/api/1.1.1/org/eclipse/per sistence/annotations/PrimaryKey.html


James : Wiki : Book : Blog : Twitter
Re: Negative Ids [message #390604 is a reply to message #390603] Wed, 29 July 2009 02:34 Go to previous messageGo to next message
Enrico is currently offline Enrico
Messages: 82
Registered: July 2009
Member
Thank you for your replies,

my code is the following:

in the Entity:
@Id
@GeneratedValue(generator="MyGenerator")
@TableGenerator(
name="MyGenerator",
table=DBSCHEMA + "COUNTERTABLE",
pkColumnName="IDCOUNTER",
valueColumnName="COUNTER",
pkColumnValue="CONFIGS",
initialValue=1,
allocationSize=1) //increment
@JoinColumn(name="ID", nullable=false)
public int getId() {
return id;
}

In the table related to this entity I have preloaded some negative ids
(-1,-2...etcc..). Moreover, this entity has relation (ontomany, many to
one...) with other entity that have yet negative Ids. If I try to load
some entity i have no problem but, when I try to make persistent or
merge some new/edit entity I got the following error on entiry relations:

"Exception Description: Null primary key encountered in unit of work clone".

If I work with positive Ids I have no problem.

So I tried to add in the persistence.xml the:

<property name="eclipselink.allow-zero-id" value="true"/>

(this is just to enable zero-id I think, by the name of the
property,...or it has some effect also on negative ids?).
Using this property the exception is solved but it seems that the Id are
not generated correctly (making persistent two entities, just one is
stored on the DB with ID=0).

Enrico


James ha scritto:
> I don't think currently singleton primitive negative ids are allowed
> without the flag (non-primitive or composite are ok, 0 or negative).
>
> Are you trying to allow negative ids on something that uses sequencing?
> That would be odd, but if you allow 0, and have a primitive int/long id
> then it will default to 0, so EclipseLink will think your two new
> objects are the same object. You would need to use a Integer or Long so
> that the value is null when not assigned, or remove the allow zero id
> flag. You can also set allowing 0 as an id on a per class basis using
> the @PrimaryKey and IdValidation settings,
>
> See,
> http://www.eclipse.org/eclipselink/api/1.1.1/org/eclipse/per sistence/annotations/PrimaryKey.html
>
>
Re: Negative Ids [message #390605 is a reply to message #390604] Wed, 29 July 2009 06:05 Go to previous messageGo to next message
Enrico is currently offline Enrico
Messages: 82
Registered: July 2009
Member
I am switching to use only positive IDs in my DB table.
But I think..it is not a "big" limitation or bug to be not able to
maange easily negative ids?

Maybe I am wrong something...but when I use positive ids I have no
problem..using negative id (int) I got a lot of problems.

Is this a common problem of all JPA implementatiosn or just for
Eclipselink?Will EclipseLink 2.0 solve that problem?

BR,
Enrico

Enrico ha scritto:
> Thank you for your replies,
>
> my code is the following:
>
> in the Entity:
> @Id
> @GeneratedValue(generator="MyGenerator")
> @TableGenerator(
> name="MyGenerator",
> table=DBSCHEMA + "COUNTERTABLE",
> pkColumnName="IDCOUNTER",
> valueColumnName="COUNTER",
> pkColumnValue="CONFIGS",
> initialValue=1,
> allocationSize=1) //increment
> @JoinColumn(name="ID", nullable=false)
> public int getId() {
> return id;
> }
>
> In the table related to this entity I have preloaded some negative ids
> (-1,-2...etcc..). Moreover, this entity has relation (ontomany, many to
> one...) with other entity that have yet negative Ids. If I try to load
> some entity i have no problem but, when I try to make persistent or
> merge some new/edit entity I got the following error on entiry relations:
>
> "Exception Description: Null primary key encountered in unit of work
> clone".
>
> If I work with positive Ids I have no problem.
>
> So I tried to add in the persistence.xml the:
>
> <property name="eclipselink.allow-zero-id" value="true"/>
>
> (this is just to enable zero-id I think, by the name of the
> property,...or it has some effect also on negative ids?).
> Using this property the exception is solved but it seems that the Id are
> not generated correctly (making persistent two entities, just one is
> stored on the DB with ID=0).
>
> Enrico
>
>
> James ha scritto:
>> I don't think currently singleton primitive negative ids are allowed
>> without the flag (non-primitive or composite are ok, 0 or negative).
>>
>> Are you trying to allow negative ids on something that uses
>> sequencing? That would be odd, but if you allow 0, and have a
>> primitive int/long id then it will default to 0, so EclipseLink will
>> think your two new objects are the same object. You would need to use
>> a Integer or Long so that the value is null when not assigned, or
>> remove the allow zero id flag. You can also set allowing 0 as an id
>> on a per class basis using the @PrimaryKey and IdValidation settings,
>>
>> See,
>> http://www.eclipse.org/eclipselink/api/1.1.1/org/eclipse/per sistence/annotations/PrimaryKey.html
>>
>>
Re: Negative Ids [message #390609 is a reply to message #390605] Wed, 29 July 2009 09:33 Go to previous messageGo to next message
James Sutherland is currently offline James Sutherland
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

I think there is a bug logged about negative ids, you could search for and
vote for it.

You would be best using an Integer id instead of int, the issue is that
the generator needs to know if to generate a new id or not, normally this
is done for null, but for int there is not null,
so 0 is assumed to be uninitialized, some people also use -1 or negative
numbers for uninitialized, so these are assumed to mean null as well, as
the generator will never generate a negative number.


James : Wiki : Book : Blog : Twitter
Re: Negative Ids [message #390610 is a reply to message #390609] Wed, 29 July 2009 09:49 Go to previous message
Enrico is currently offline Enrico
Messages: 82
Registered: July 2009
Member
HI James,
thank you.
I will vote for that logged bug.

I could use Integer id
instead of int. The problem is that I use persistence also for my JSP
page....I
need to make persistent value set by user in form on jsp page. I use
java bean
and they work with primitive types (int, ..string,...array of
primitives...).
If I use Integer, I need always make control to cast int type to integer
(into
get methods).
I prefer they solve the bug :-)

Enrico

James ha scritto:
> I think there is a bug logged about negative ids, you could search for
> and vote for it.
>
> You would be best using an Integer id instead of int, the issue is that
> the generator needs to know if to generate a new id or not, normally
> this is done for null, but for int there is not null,
> so 0 is assumed to be uninitialized, some people also use -1 or negative
> numbers for uninitialized, so these are assumed to mean null as well, as
> the generator will never generate a negative number.
>
>
>
Previous Topic:Data row initialization for DataBase Table
Next Topic:Eclipselink without connection pool
Goto Forum:
  


Current Time: Thu Apr 24 06:36:28 EDT 2014

Powered by FUDForum. Page generated in 0.02111 seconds