Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using an Entity (and their Primary Key) as another Entity's Id

So, I'm not sure how to ask this question, as it seems like it should be pretty easy to find the answer to this one.

I have 3 tables; ContentHeader, ContentType1 and ContentType2. ContentHeader has a primary, auto-increment key. ContentType1 and ContentType2 both maintain foreign keys to ContentHeader's primary key. These foreign keys are also the primary keys for their respective tables.

CREATE TABLE contentHeader (contentID INT AUTO_INCREMENT PRIMARY KEY, ...) ENGINE=InnoDB;

CREATE TABLE contentType1 (contentID INT PRIMARY KEY, FOREIGN KEY (contentID) REFERENCES contentHeader (contentID), ...) ENGINE=InnoDB;

CREATE TABLE contentType2 (contentID INT PRIMARY KEY, FOREIGN KEY (contentID) REFERENCES contentHeader (contentID), ...) ENGINE=InnoDB;

I have created four classes:

@Entity
public class ContentHeader {

  @Id
  @GeneratedValue
  protected int contentID;

  ...
}

@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class Content {

  @Id
  @OneToOne
  protected ContentHeader contentHeader;

  ...
}

@Entity
public class ContentType1 extends Content {
  ...
}

@Entity
public class ContentType2 extends Content {
  ...
}

This throws a null pointer when trying to generate the schema. I'm pretty sure I'm just missing something simple. I noticed the PrimaryKeyJoinColumn, but I'm not sure if it's what I need or not.

like image 864
Drew Avatar asked May 24 '09 20:05

Drew


People also ask

How does entity class define primary key?

With an entity, make sure that you specify the primary key in the class hierarchy. When you specify the primary key, follow the below rules: For a simple (not complex type) primary key, specify @Id in the persistence field or persistence property or specify the key in the O/R mapping file.

Which annotation is used to define primary key of an entity class?

Simple primary keys use the javax.persistence.Id annotation to denote the primary key property or field. Composite primary keys are used when a primary key consists of more than one attribute, which corresponds to a set of single persistent properties or fields.

How do you set a primary key in an entity?

Each entity type will typically use its own row in the table to generate the primary key values for that entity class. Primary key values are positive integers. A primary key generator, which you can reference by name, defined at one of the package, class, method, or field level.


1 Answers

You can create a composite id class that only has the ContentHeader:

@Embeddable
public class ContentKey implements java.io.Serializable {

    @ManyToOne(cascade = {}, fetch = FetchType.LAZY)
    @JoinColumn(name = "ID", updatable = true)
    private ContentHeader header;
    // ...
}

@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class Content {

    @Id
    public ContentKey  getContentKeyId()
    // ...
}

That should do the trick.

like image 86
Miguel Ping Avatar answered Oct 27 '22 01:10

Miguel Ping