如何在 hibernate 3 中映射派生身份?

发布于 2024-10-03 05:25:31 字数 1416 浏览 0 评论 0原文

我有以下情况:

@Entity
class A{ 
  @Id
  @SequenceGenerator(name = "SEQ_AID", sequenceName = "SEQ_AID")
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_AID")
  private Long aId;
  @OneToOne(fetch=FecthType.LAZY,optional=false,cascade=CascadeType.ALL)
  private B b;
  ...
}
@Entity
class B{
  @Id
  private A a;
  ...
}

换句话说:A和B之间存在OneToOne关联。B是一个弱实体,它的Id是从A类派生的。

我已经测试了一些解决方案,在@OneToOne下添加@PrimaryKeyJoinColumn作为这篇文章 提到。但我收到此错误:“org.hibernate.id.IdentifierGenerationException:在调用 save() 之前必须手动分配此类的 ids:B”
我不知道这是否与本例相关,但我正在使用 Oracle 11g。

已更新

我认为我的方式是正确的。这是我的问题的实际状态:

@Entity
class A{ 
  @Id
  @SequenceGenerator(name = "SEQ_AID", sequenceName = "SEQ_AID")
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_AID")
  private Long aId;
  @OneToOne(fetch=FecthType.LAZY,optional=false,cascade=CascadeType.ALL)
  @PrimaryKeyJoinColumn(name="aId")
  private B b;
  ...
}
@Entity
class B{
  @Id
  @OneToOne
  @JoinColumn(name="aId)
  private A a;
  ...
}

现在的错误有点不同:

java.sql.SQLException: ORA-00904: "B"."A": invalid identifier  

它试图在表 B 中查找列 A(而不是 AID)。我不知道如何指定列名称为 B。 AID 而不是 BA

I have the following situation:

@Entity
class A{ 
  @Id
  @SequenceGenerator(name = "SEQ_AID", sequenceName = "SEQ_AID")
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_AID")
  private Long aId;
  @OneToOne(fetch=FecthType.LAZY,optional=false,cascade=CascadeType.ALL)
  private B b;
  ...
}
@Entity
class B{
  @Id
  private A a;
  ...
}

In other words: There is a OneToOne association between A and B. B is a weak entity, and its Id is derived from class A.

I've already tested some solutions as adding @PrimaryKeyJoinColumn under @OneToOne as this article mentions. But I got this error: "org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): B"
I don't know if it's relevant in this case, but I'm using Oracle 11g.

UPDATED

I think I'm in the right way. Here is the actual state of my problem:

@Entity
class A{ 
  @Id
  @SequenceGenerator(name = "SEQ_AID", sequenceName = "SEQ_AID")
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_AID")
  private Long aId;
  @OneToOne(fetch=FecthType.LAZY,optional=false,cascade=CascadeType.ALL)
  @PrimaryKeyJoinColumn(name="aId")
  private B b;
  ...
}
@Entity
class B{
  @Id
  @OneToOne
  @JoinColumn(name="aId)
  private A a;
  ...
}

The error now is a bit different:

java.sql.SQLException: ORA-00904: "B"."A": invalid identifier  

It is trying to find the column A (instead of AID) in the table B. I don't know how to specify that the column name is B.AID and not B.A.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尘世孤行 2024-10-10 05:25:31

我按照这个链接解决了我的问题

正确的答案是:

@Entity
public class A {

  @Id
  @GeneratedValue
  @Column(name = "aId")
  private Long id;

  @OneToOne(fetch = FetchType.LAZY, optional=false, cascade = CascadeType.ALL, mappedBy = "a")
  private B b;
  ... 
} 


@Entity
@org.hibernate.annotations.GenericGenerator(name="a-primarykey", strategy="foreign", parameters={@org.hibernate.annotations.Parameter(name="property", value="a")})
public class B {

  @Id
  @GeneratedValue(generator = "a-primarykey")
  @Column(name = "aId")
  private Long id;

  @OneToOne
  @PrimaryKeyJoinColumn
  private A a;
  ...
} 

I solved my problem following this link

The correct answer would be:

@Entity
public class A {

  @Id
  @GeneratedValue
  @Column(name = "aId")
  private Long id;

  @OneToOne(fetch = FetchType.LAZY, optional=false, cascade = CascadeType.ALL, mappedBy = "a")
  private B b;
  ... 
} 


@Entity
@org.hibernate.annotations.GenericGenerator(name="a-primarykey", strategy="foreign", parameters={@org.hibernate.annotations.Parameter(name="property", value="a")})
public class B {

  @Id
  @GeneratedValue(generator = "a-primarykey")
  @Column(name = "aId")
  private Long id;

  @OneToOne
  @PrimaryKeyJoinColumn
  private A a;
  ...
} 
远山浅 2024-10-10 05:25:31

你在实体B上尝试过这个吗?

@Entity class B {
  @Id @OneToOne 
  @JoinColumn(name = "table_a_id") //put the correct column name for A's pk here
  private A a;
  ....
}

Have you tried this on Entity B?

@Entity class B {
  @Id @OneToOne 
  @JoinColumn(name = "table_a_id") //put the correct column name for A's pk here
  private A a;
  ....
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文