-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: creating a schema on hierarchy
PostPosted: Wed Jan 31, 2007 7:27 pm 
Newbie

Joined: Fri Jan 26, 2007 3:51 pm
Posts: 16
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


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 03, 2007 7:35 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
@OneToMany
and
@org.hibernate.annotations.CollectionOfElements

do not make sense on the same collection

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.