Hibernate: how to automatically match a Java object on two columns?

Imagine that I have one common functionality: a series and a number (string and integer) of some document. My object (insurance policy) contains information about the series and the number of different documents, so I would like to group this series and number into one Java object and allow the sleeping storage to store two fields on each object in one table.

See an example:

    class Polis {
        private DocInfo kaskoNumber;
        private DocInfo osagoNumber;
        private DocInfo tsNumber;
    }
    class DocInfo {
        private String series;
        private Integer number;
    }
    table:
    polis(kaskoSeries varchar2, 
          kaskoNumber numeric, 
          osagoSeries varchar2, 
          osagoNumber numeric..... )

- . , - Polis DocInfo. java, Hibernate, , , ManyToOne (doc_info). !

.

+3
2

@Embeddable @AttributeOverrides:

@Entity
class Polis {
    @AttributeOverrides( {
        @AttributeOverride(name="series", column = @Column(name="kaskoSeries") ),
        @AttributeOverride(name="number", column = @Column(name="kaskoNumber") )
    })
    private DocInfo kaskoNumber;

    @AttributeOverrides( {
        @AttributeOverride(name="series", column = @Column(name="osagoSeries") ),
        @AttributeOverride(name="number", column = @Column(name="osagoNumber") )
    })
    private DocInfo osagoNumber;
    ...
}

@Embeddable
class DocInfo {
    private String series;
    private Integer number;
}

. :

+7

"Hibernate", JPA "" . / , /. .

0

Source: https://habr.com/ru/post/1793392/


All Articles