本文介绍了用实体的(和它们的主键)为另一实体的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我不知道怎么问这个问题,因为它似乎像它应该是pretty容易找到这个问题的答案之一。

我有3个表; ContentHeader,ContentType1和ContentType2。 ContentHeader有小学,自动递增键。 ContentType1和ContentType2既保持外键ContentHeader的主键。这些外键也为各自的表的主键。

  CREATE TABLE contentHeader(内容识别INT AUTO_INCREMENT PRIMARY KEY,...)ENGINE = InnoDB的;CREATE TABLE contentType1(内容识别INT PRIMARY KEY,FOREIGN KEY(内容识别)参考文献contentHeader(内容识别),...)ENGINE = InnoDB的;CREATE TABLE contentType2(内容识别INT PRIMARY KEY,FOREIGN KEY(内容识别)参考文献contentHeader(内容识别),...)ENGINE = InnoDB的;

我已经创建了四个类:

  @Entity
公共类ContentHeader {  @ID
  @GeneratedValue
  保护INT内容识别;  ...
}@实体
@Inheritance(策略= InheritanceType.TABLE_PER_CLASS)
公共抽象类内​​容{  @ID
  @OneToOne
  保护ContentHeader contentHeader;  ...
}@实体
公共类ContentType1扩展内容{
  ...
}@实体
公共类ContentType2扩展内容{
  ...
}

这抛出试图生成架构时一个空指针。我是pretty肯定,我只是失去了一些东西简单。我注意到PrimaryKeyJoinColumn,但我不知道这是否是我需要与否。


解决方案

 您可以创建一个复合ID类,只有具备ContentHeader:@Embeddable
公共类ContentKey实现java.io.Serializable {    @ManyToOne(级联= {},取= FetchType.LAZY)
    @JoinColumn(名称=ID中,可更新=真)
    私人ContentHeader头​​;
    // ...
}@实体
@Inheritance(策略= InheritanceType.TABLE_PER_CLASS)
公共抽象类内​​容{    @ID
    公共ContentKey getContentKeyId()
    // ...
}

这是应该做的伎俩。

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.

解决方案
You can create a composite id class that only have 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.

这篇关于用实体的(和它们的主键)为另一实体的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 00:54