@SecondaryTable causes not-null property references a null or transient value exception

Viewed 274

I have double reference between two entities Person and Account. I can have Person entries that are having no accounts to login.

Person table has an account_id and it is nullable. Account was referenced from Person. Person was referenced from Account.

I wanted to break this double reference, so I used @SecondaryTable.

but whenever I create a person without an account I get not-null property references a null or transient value : Person.Account

@Entity
@Table(name = "ACCOUNT")
@SequenceGenerator(name = "GENERATE_PKS_SEQ", sequenceName = "PKS_ACCOUNT_SEQ", allocationSize = 1)
@SecondaryTable(name = "PERSON", pkJoinColumns = @PrimaryKeyJoinColumn(name = "ACCOUNT_ID"))
@org.hibernate.annotations.Table(appliesTo = "PERSON", optional = false)
public class Account extends AbstractEntityWithCreateUpdateInfo {

  ...

  @Column(name = "FIRST_NAME", table = "PERSON")
  private String firstName;

  @Column(name = "LAST_NAME", table = "PERSON")
  private String lastName;

  @Column(name = "E_MAIL", table = "PERSON")
  private String email;

  ...

}


@Entity
@Table(name = "PERSON")
@SequenceGenerator(name = "GENERATE_PKS_SEQ", sequenceName = "PKS_PERSON_SEQ", allocationSize = 1)
public class Person extends AbstractEntityWithCreateUpdateInfo {

  @Valid
  @OneToOne(targetEntity = Account.class, fetch = FetchType.EAGER, cascade = 
  CascadeType.ALL)
  @JoinColumn(name = "ACCOUNT_ID", nullable = true)
  private Account account;

}

but if I remove the pkJoinColumns the issue goes away but of course the generated join clause won't be correct

0 Answers
Related