0

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.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
Neuquino
  • 11,580
  • 20
  • 62
  • 76
  • If you use Hibernate 3.5, this may help: http://stackoverflow.com/questions/4027623/how-do-i-properly-cascade-save-a-one-to-one-bidirectional-relationship-on-primar – axtavt Nov 21 '10 at 14:59

2 Answers2

2

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;
  ...
} 
Neuquino
  • 11,580
  • 20
  • 62
  • 76
1

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;
  ....
}
user18943
  • 759
  • 1
  • 9
  • 18
  • yes. Thanks for the answer, but it doesn't work, same error. As the article says, it seems that it is not possible to use cascade with derived identities... – Neuquino Nov 21 '10 at 13:28