gurghet
gurghet

Reputation: 7707

Persist an object within an object with jpa

I have this object

@Entity
public class Cat {
  @Id String name;
  Fur fur;
}

public class Fur {
  String color1;
  String color2;
}

How do I map it to:

 Name      Color1 Color2
+---------+------+------+
|SnowBall |red   |green |
+---------+------+------+
|Snowball2|white |black |
+---------+------+------+

I only have JPA 2.1

Upvotes: 2

Views: 1007

Answers (1)

S. Pauk
S. Pauk

Reputation: 5318

You could use @Embeddable and @Embedded JPA annotations.

@Entity
public class Cat {
  @Id String name;
  @Embedded
  Fur fur;
}

@Embeddable
public class Fur {
  String color1;
  String color2;
}

Upvotes: 4

Related Questions