相关疑难解决方法(0)

JPA Hibernate一对一的关系

我有一对一的关系,但hibernatetool在生成模式时抱怨.这是一个显示问题的示例:

@Entity
public class Person {
    @Id
    public int id;

    @OneToOne
    public OtherInfo otherInfo;

    rest of attributes ...
}
Run Code Online (Sandbox Code Playgroud)

Person与OtherInfo有一对一的关系:

@Entity
public class OtherInfo {
    @Id
    @OneToOne(mappedBy="otherInfo")
    public Person person;

    rest of attributes ...
}
Run Code Online (Sandbox Code Playgroud)

人拥有OtherInfo的一面.OtherInfo是拥有的一方,因此人们用来mappedBy在Person中指定属性名称"otherInfo".

使用hibernatetool生成数据库模式时出现以下错误:

org.hibernate.MappingException: Could not determine type for: Person, at table: OtherInfo, for columns: [org.hibernate.mapping.Column(person)]
        at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:292)
        at org.hibernate.mapping.SimpleValue.createIdentifierGenerator(SimpleValue.java:175)
        at org.hibernate.cfg.Configuration.iterateGenerators(Configuration.java:743)
        at org.hibernate.cfg.Configuration.generateDropSchemaScript(Configuration.java:854)
        at org.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:128)
        ...
Run Code Online (Sandbox Code Playgroud)

知道为什么吗?我做错了什么或者这是一个Hibernate错误?

java hibernate jpa

62
推荐指数
3
解决办法
11万
查看次数

使用一个实体(及其主键)作为另一个实体的 Id

所以,我不知道如何问这个问题,因为似乎应该很容易找到这个问题的答案。

我有 3 张桌子;ContentHeader、ContentType1 和 ContentType2。ContentHeader 有一个主要的、自动递增的键。ContentType1 和 ContentType2 都维护 ContentHeader 主键的外键。这些外键也是它们各自表的主键。

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;
Run Code Online (Sandbox Code Playgroud)

我创建了四个类:

@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 …
Run Code Online (Sandbox Code Playgroud)

entity annotations hibernate jpa

4
推荐指数
1
解决办法
5750
查看次数

标签 统计

hibernate ×2

jpa ×2

annotations ×1

entity ×1

java ×1