简体   繁体   中英

Persist List of Embeddable in an Entity

I want to persist a List of Embeddable Objects in an Entity. But without creating a new Table for the Objects in the List.

I want to persist the Entity in a way that the List of coordinates is serialized to a String and saved in the column named "coordinates" of the Entity Entity_A.

I know that i shouldn't do this because it's bad design. But in this case it is needed.

My Attempt to do it:

@Entity
class Entity_A implements Serializable {
    //... 

    private List<Coordinate> coordinates;
}

@Embeddable
class Coordinate implements Serializable {
    private Float lat;
    private Float lng;
    private Float alt;
}

It doesn't work. i don't get any error.

Do you have any ideas how i can do it?

Edit:

Coordinate needs to be embeddable, because i use it elsewhere.

With JPA 2.1 it is possible:

annotate coordinates field with @ElementCollection

@ElementCollection    
private List<Coordinate> coordinates;

See more on http://en.wikibooks.org/wiki/Java_Persistence/ElementCollection

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