Primary-key @OneToOne mapping in Hibernate

Joining two tables using Primary keys in Hibernate.

Wed, 18 May 2011

Today was trying to join two tables using its Primary keys using Hibernate. Here is what I tried to do:

@Table(name = “customer”) @Entity public class Customer { @Id @OneToOne @JoinColumn(name = “customer_id”,updatable = false) private Credentials credentials; }

I was constantly getting an error stating invalid column name. Later then I realized that its not possible have Join in the Primary-Key and bind to a custom object. This forced me to have a Auto-generated Id as a key to the table and named it as the primary key. This is how my code looked after modification.

@Table(name = “customer”) @Entity public class Customer {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

@OneToOne
@JoinColumn(name = "customer\_id",updatable = false)
private Credentials credentials;

}

Loading...
Prasanna

Prasanna is a full stack web developer, with exposure to various programming languages. Uses mostly Java, Javascript these days and got 11+ years of architecting and coding enterprise software solutions.

  • rants and writings through personal experience