Programing

지도 저장

lottogame 2020. 8. 26. 08:20
반응형

지도 저장 JPA 사용


주석을 attributes사용하여 JPA2를 사용하여 다음 클래스에서지도 를 유지할 수 있는지 궁금합니다.

public class Example {
    long id;
    // ....
    Map<String, String> attributes = new HashMap<String, String>();
    // ....
}

이미 기존 프로덕션 데이터베이스가 있으므로의 값이 attributes다음 기존 테이블에 매핑 될 수 있습니다.

create table example_attributes {
    example_id bigint,
    name varchar(100),
    value varchar(100));

JPA 2.0은 컬렉션 지원 @ElementCollection과 함께 사용할 수 있는 주석을 통해 기본 요소 컬렉션을 지원합니다 java.util.Map. 다음과 같이 작동합니다.

@Entity
public class Example {
    @Id long id;
    // ....
    @ElementCollection
    @MapKeyColumn(name="name")
    @Column(name="value")
    @CollectionTable(name="example_attributes", joinColumns=@JoinColumn(name="example_id"))
    Map<String, String> attributes = new HashMap<String, String>(); // maps from attribute name to value

}

참조 (JPA 2.0 사양)

  • 2.6-포함 가능한 클래스 및 기본 유형 컬렉션
  • 2.7지도 컬렉션
  • 10.1.11-ElementCollection 주석
  • 11.1.29 MapKeyColumn 주석

  @ElementCollection(fetch = FetchType.LAZY)
  @CollectionTable(name = "raw_events_custom", joinColumns = @JoinColumn(name =     "raw_event_id"))
  @MapKeyColumn(name = "field_key", length = 50)
  @Column(name = "field_val", length = 100)
  @BatchSize(size = 20)
  private Map<String, String> customValues = new HashMap<String, String>();

열 및 테이블 이름과 필드 길이를 제어하여 맵을 설정하는 방법에 대한 예입니다.

참고 URL : https://stackoverflow.com/questions/3393649/storing-a-mapstring-string-using-jpa

반응형