简体   繁体   中英

JPA: One entity mapped to multiple rows, is it possible?

In JPA, can I map an Entity to a table where attributes of the entity are persisted across multiple rows?

Basically, can I map a class like this:

public Document {
    String id;
    String title;
    String author;
    String size;
}

To a table like

CREATE TABLE DOC_METADATA
(
    DOC_ID NUMBER,
    ATTR_NAME VARCHAR,
    ATTR_VALUE VARCHAR
);

With content like

DOC_ID   | ATTR_NAME     | ATTR_VALUE
1          "title"         "Alice in wonderland"
1          "author"        "Lewis Carroll"
1          "size"          "500kb"
2          "title"         "Winnie the pooh"
2          "author"        "A. A. Milne"
2          "size"          "600kb"

No it's impossible. Your class Document is a domain class (java presentation of your table in db). When you try to create table from domain class so will be created a table with such fields like in your domain class.

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