last day (15 days later) » 

5:44 PM
4
Q: How to create a generic entity model class that supports generic id including auto generated ids?

Luiggi MendozaI have three kinds of primary keys for tables: INT auto generated primary key which use AUTO_INCREMENT capacity from database vendor (MySQL) CHAR(X) primary key to store a user readable value as key (where X is a number and 50 <= X <= 60) Complex primary keys, composed by 2 or 3 fields of the t...

 
What do the superclasses add?
 
@flup basically, the generic id for the entity.
 
So instead of writing extends BaseEntity<String> you could add the annotated id property to the Table2 class. Then you can annotate it with an appropriate generator for the String type.
 
@flup if you read the code in my example, I don't need a generated String for the id. Instead, I want/need to provide my own id for the String value. And I also want to have a default class structure for all the tables who supports an id field. Only in extreme cases when this structure doesn't adapt, I'll create a class on its own with the necessary fields for it.
 
You put a generator on it now, in the BaseEntity superclass, for all types T, so including the String class.
 
5:44 PM
@flup that means the generator will work everywhere, and that's not the desired behavior.
 
I mean to say that you are already doing that
the generator is in the BaseEntity so your string id class already has a generator
but one that is not valid for String, hence the error
 
 
2 hours later…
7:36 PM
hello @flup
I can talk now
sorry for not entering into chat earlier
 
7:47 PM
hi!
 
hello :)
looks like you have questions about my question
 
8:31 PM
wondering why you want to put the id annotation in the generic base class
cause there you then also put the annotation with the generator
and that annotation causes the error
so if you need not annotate it in the base class, the problem goes away, so to speak
 
I don't want to replicate the @Id annotation on every class
it seems useless
 
now you've to replicate the extends
 
uhm
the base class will handle the @Id for all its subclasses
that's the idea
 
yes, but I mean, it gives you very little in return
it gives you this problem, plus it demands to be the base class of your pojo's
 
it will be the base class or almost all pojos
 
8:34 PM
and in return you need not write the two lines for the id attribute
 
there are quite few pojos that doesn't meet having a bigint id field
 
(playing devil's advocate here, I can think of a couple reasons why you'd want the base class)
 
in fact
 
(but it's important to know why you want the id attribute there)
 
I have a similar model in a project that uses Hibernate 3 and XML mapping
and since the XML handles the type of the id and its generation separately from the annotation design
it works as expected
 
8:36 PM
nodnod
 
but now we're doing some tests using Hibernate 4 and annotations
 
problem comes now that you need to put the annotation where the attribute is
 
and we hit with this wall
 
I think there's something to be said for pure pojo's when you can get away with them
(not sure how pure they remain when you need to put the annotations on :) )
 
they're not pure anymore, that's a trade off
and we're testing if the trade off is worth the pita
that's part of the tests
 
8:39 PM
you can create a special base class for the string id classes, with a different generator
superduper base class without id
one extension for generic auto generate id
other extension for the string id, without auto generate
 
that's what I'm doing currently in another branch
 
whether all that's worth it, somewhat depends on what you get out of the base classes to begin with
I'm working on a project where the entities get added by uploading to an existing application.
so then there's a lot of meta meta talk and meta classes
 
yep
I understand
I've worked in that kind of projects too
 
if I could code the pojo's anyways, I'd revel in the luxury and make the most of that
simply put String id field in the Table class, happily leave out the annotation cause I don't want it there
 
but now that doesn't support classes with multiple keysw
we have 2 or 3 tables with that case
and we already have a class that works as this complex id
the annotation design breaks this model
and I'm looking for a solution on this
your proposal would sound good but it's not applicable for all my needs
in my test I just worked with 2 cases
I left the 3rd case for the end
for being the most complicated of them
 
8:46 PM
the multiple ID class, you created a separate class that holds the key fields? I'm not sure I understand you correctly
 
yes, I do that
 
and you feed the key field class as generic parameter to the base class, correct?
 
that's correct
 
understood
 
as I've said before, that model using XML supports all these requirements
when being passed into annotation, it breaks =\
 
8:47 PM
yuss
 
the easier but ugly solution would be to remove the @GeneratedValue from the base class
 
yes, that's the one that's causing the problem
 
and create 3 more classes which extend from BaseEntity, VersionedEntity and MaintainedEntity using a Long as parameter
 
or to make the two base classes, one with, one without the @GeneratedValue
 
and override the getId method to apply @Id @GeneratedValue
uhm
I cannot have 2 classes
 
8:49 PM
one third at the top
utterly empty
 
because Java doesn't allow multiple inheritance
please refer to my question again
I have to meet these requirements
a BaseClass that holds the Id
a VersionedEntity class that adds a @Version int version field
and a MaintainedEntity class that adds some fields for maintainability (audit) purposes like user that created the entity and the date the entity was created
after doing this, there are tables in the databases that may fall in one of these categories
 
grins question understood, tempted to try for an answer
 
and tables that do not fall in these categories will be treated separately
you can post an answer
you're welcome for doing so
I still have to test the current answer
that still don't convince me
 
9:18 PM
if you have an answer, please provide it
 

last day (15 days later) »