Vik Gamov
Vik Gamov

Reputation: 5456

Join entity with composite key

I have 2 entities for legacy db with composite keys, one of them has a composite key with @EmbeddedId annotation.

// first entity
@Entity
public class Product {

 @Id
 private Integer productId;

 // lookup table contains code-description pairs
 @OneToOne
 private ProductDefects defects;

 //getters and setters and other code omitted 

}

// lookup entity
@Entity
public class ProductDefects {

 @EmbededId
 private ProductDefectsPK id;

 //getters and setters and other code omitted 

} 

//composite key
@Embedable
 public class ProductDefectsPk{
  private Integer realId;
  private String  category;
 }

How should I define the @OneToOne relation to join as in the following example:

select p.Id, pd.description
from Product p
inner join p.defects pd

Upvotes: 0

Views: 4123

Answers (1)

Vik Gamov
Vik Gamov

Reputation: 5456

I figure out that @MapsId annotation helps in my case http://download.oracle.com/javaee/6/api/javax/persistence/MapsId.html

Upvotes: 3

Related Questions