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:
Code:
package scholastic.models;
//import org.hibernate.annotations.*;
import javax.persistence.*;
import java.io.Serializable;
import java.util.*;
@Entity
@Table(name = "USERS")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class User implements Serializable, Comparable
{
@Id @GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "USER_ID")
private Long id = null;
@ManyToOne(targetEntity = scholastic.models.Company.class)
@JoinColumn(name = "COMPANY_ID", nullable = false)
private Company company;
public User() {}
public User(Company company, String employeeID, String fname, String lname)
{
this.company = company;
this.employeeID = employeeID;
this.firstname = fname;
this.lastname = lname;
}
public Company getCompany()
{ return company;
}
public void setCompany(Company company)
{
this.company = company;
}
...
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:
Code:
@Entity
@Table(name = "COMPANIES")
public class Company implements Serializable
{
@Id @GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "COMPANY_ID")
private Long id = null;
...
@OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE}, mappedBy = "company")
@org.hibernate.annotations.Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
@org.hibernate.annotations.CollectionOfElements
@JoinTable(name = "USERS", joinColumns = @JoinColumn(name = "USER_ID"))
@org.hibernate.annotations.CollectionId(columns = @Column(name="COMPANY_USER_ID"), type = @org.hibernate.annotations.Type(type = "long"), generator = "sequence")
private Collection<User> ADMINISTRATORS = new ArrayList<User>();
...
}
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. Could someone please help?
Alan