aspose file tools*
The moose likes Object Relational Mapping and the fly likes inheritance and schema creation using JPA annotations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "inheritance and schema creation using JPA annotations" Watch "inheritance and schema creation using JPA annotations" New topic
Author

inheritance and schema creation using JPA annotations

Alan Shiers
Ranch Hand

Joined: Sep 24, 2003
Posts: 237
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

Joined: Dec 11, 2006
Posts: 364
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

Joined: Sep 24, 2003
Posts: 237
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

Joined: Dec 11, 2006
Posts: 364
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inheritance and schema creation using JPA annotations