简体   繁体   中英

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

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM