用jpa在对象中保留一个对象

时间:2015-03-19 10:13:58

标签: java hibernate jpa orm jboss

我有这个对象

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

public class Fur {
  String color1;
  String color2;
}

如何将其映射到:

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

我只有JPA 2.1

1 个答案:

答案 0 :(得分:3)

您可以使用@Embeddable@Embedded JPA注释。

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

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