如何在 hibernate 3 中映射派生身份?
我有以下情况:
@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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我按照这个链接解决了我的问题
正确的答案是:
I solved my problem following this link
The correct answer would be:
你在实体B上尝试过这个吗?
Have you tried this on Entity B?