Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inheritance and schema creation using JPA annotations

 
Alan Shiers
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

In my little project I have a hierarchy of classes:

abstract User
|
|__ Administrator extends User
|__ Instructor extends User
|__ Student extends User

The User class is annotated like so:



Notice that I specifically state:
@Id @GeneratedValue(strategy = GenerationType.AUTO)
for the id variable.

One would think that when I ran hbm2ddl to create the schema the USERS table would have set the USER_ID column to auto increment:

USER_ID bigint generated by default as identity (start with 1)

but that isn't what happened. Instead I got:

create table USERS (
USER_ID bigint not null,
...
primary key (USER_ID)
);

What's up with that?

Please advise,

Alan
 
Edvins Reisons
Ranch Hand
Posts: 364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Alan Shiers:

@Id @GeneratedValue(strategy = GenerationType.AUTO)



For GenerationType.AUTO, the strategy that Hibernate chooses is database-dependent. To get a specific strategy, like IDENTITY, specify it.
[ February 01, 2007: Message edited by: Edvins Reisons ]
 
Alan Shiers
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This annotation: @Id @GeneratedValue(strategy = GenerationType.AUTO)
is the default and causes the database to use its own incrementing strategy. This I already know. This is the behaviour I want.

I would like to clarify my situation a little more with added code. Perhaps someone can spot the problem.

The User class is annotated like so:




Notice that I specifically state:
@Id @GeneratedValue(strategy = GenerationType.AUTO)
for the id variable.

One would think that when I ran hbm2ddl to create the schema the USERS table would have set the USER_ID column to auto increment like so:

USER_ID bigint generated by default as identity (start with 1)

but that isn't what happens. Instead I get:

create table USERS (
USER_ID bigint not null,
...
primary key (USER_ID)
);

I believe this has something to do with the Company class. It contains a collection of type Users, like so:




When I eliminate the Company class altogether and all references to it in the User class, then the annotation for the id variable is correctly noted in the database schema:

USER_ID bigint generated by default as identity (start with 1)

This tells me there is something strange in the relationship between the Company class and the User class.

I need the Company class and I also need the User class to have a bidirectional reference back to an instance of the Company class. The way I have it setup now, however, prevents the hbm2ddl from setting the auto increment on the id field in the User class. I don't really see what is going on here. Perhaps you do?

Alan
 
Edvins Reisons
Ranch Hand
Posts: 364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hopefully, somebody can see it already. To help the rest of us, please reduce the number of annotations to a strict minimum, perhaps using p. 2.1.8.2 of the JPA specification as a guideline.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic