Home » Eclipse Projects » EclipseLink » Negative Ids
| | |
Re: Negative Ids [message #390603 is a reply to message #390600] |
Tue, 28 July 2009 18:31 |
|
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 06:34 |
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 10:05 |
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 13:33 |
|
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 13:49 |
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.
>
>
>
|
|
|
Goto Forum:
Current Time: Sat Jan 25 23:26:21 GMT 2025
Powered by FUDForum. Page generated in 0.04817 seconds
|