开发者

How to map derived identities in hibernate 3?

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.sq开发者_如何学运维l.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.


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;
  ...
} 


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;
  ....
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜