-
1. Re: Optional auto-generation
epbernard Apr 21, 2005 6:06 AM (in response to dkrizic)Besides it is a really bad design, you can't do that because when your sequence or you identity will encoun ter a value you've et, you'll be in deep trouble.
You really really should not try to do so. -
2. Re: Optional auto-generation
dkrizic Apr 21, 2005 6:39 AM (in response to dkrizic)But the Real World[tm] requires something like this. Batch jobs usually insert values that have predefined primary keys and other applications get a generated key where the sequence works in another number range.
Besides of this: Is there a technical solution for this? -
3. Re: Optional auto-generation
kkoster Apr 21, 2005 8:10 AM (in response to dkrizic)Yes, the Real World[tm] does require something like this. But it is normally outside the scope of normal business logic. (e.g. fixing up data, conversions, etc.) In these cases you probably should work outside the normal business logic infrastructure. You may want to do this entirely outside of the J2EE environment. Or, if it is a regular occurance and you want to keep it in the EJB container, construct special instances of the particular entities involved that deal with these special conditions leave the generation annotation off. (it defaults to NONE) You may, of course, have to fix up any data that is used by the Id generator for the normal business logic after you are finished.
-
4. Re: Optional auto-generation
dkrizic Apr 21, 2005 8:13 AM (in response to dkrizic)But if I turn off the generator - where do I get the primary keys from when I need them?
-
5. Re: Optional auto-generation
kkoster Apr 21, 2005 8:28 AM (in response to dkrizic)Let me clarify, when I said "construct special instances of the particular entities" that would mean a seperate implementation of these entities for the particular needs of the batch, non-normal process.
For instance, you have an entity Widget described for you normal business processes. You would create a seperate implementation of the entity WidgetSpecial that would simple have a different @Id annotation. In fact you could probably construct a base class WidgetBase that contains all necessary business logic and is annotation free and have Widget and WidgetSpecial derive from it with the appropriate annotations.
This actually begs some interesting questions. Are entity annotations "final"? If I derive an some entity from an annotated entity 1) does it inherit all of the annotations? 2) does the container recognize these annotations and allow the entity to participate in the persistence framework? 3) can I override annotations on the derived class and change its persistence behaviour?
I eagerly await the reply of the experts. -
6. Re: Optional auto-generation
epbernard Apr 22, 2005 6:18 AM (in response to dkrizic)The technical solution would be a user defined id generator, this is not possible right now, but this is not an enormous amount of work.
This actually begs some interesting questions. Are entity annotations "final"? If I derive an some entity from an annotated entity 1) does it inherit all of the annotations? 2) does the container recognize these annotations and allow the entity to participate in the persistence framework? 3) can I override annotations on the derived class and change its persistence behaviour?
Well this is a dangerous area :-)
1) If the sub entity has @Entity, it will be part of the entity hierarchy and thus e persisted as a subclass (see @Inheritance for more details)
2) thus yes.
3) Basically no. Overriding is allowed through XML, but altering a sub entity behavior for the same Entity manager configuration could have lots of side effects. -
7. Re: Optional auto-generation
dkrizic Apr 22, 2005 7:09 AM (in response to dkrizic)I did not look into the code of the current "AUTO"-Implementation but I think the only needed change is
Long primaryKey;
if( objectToBePersisted.getPrimaryKey() != null ) {
primaryKey = objectToBePersisted.getPrimaryKey();
} else {
primaryKey = generatePrimaryKey();
}
Or do I see this *too* simple?
...darko -
8. Re: Optional auto-generation
epbernard Apr 22, 2005 9:05 AM (in response to dkrizic)"dkrizic" wrote:
Or do I see this *too* simple?
It does not work when cascading the persist and merge operations is performed