I want to save a GeoJSON to database by using Spring Boot and JPA
Example JSON object:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"type": "polyline"
},
"geometry": {
"type": "LineString",
"coordinates": [
[
21.011127,
52.230914
],
[
21.013166,
52.229978
],
[
21.011331,
52.22911
],
[
21.00845,
52.228953
],
[
21.009062,
52.230011
],
[
21.009513,
52.230287
]
]
}
},
{
"type": "Feature",
"properties": {
"type": "polyline"
},
"geometry": {
"type": "LineString",
"coordinates": [
[
21.022489,
52.226406
],
[
21.023004,
52.228995
],
[
21.018069,
52.230007
],
[
21.018755,
52.225618
],
[
21.016202,
52.22454
],
[
21.013906,
52.226735
]
]
}
},
{
"type": "Feature",
"properties": {
"type": "polyline"
},
"geometry": {
"type": "LineString",
"coordinates": [
[
21.013627,
52.232005
],
[
21.018165,
52.231933
],
[
21.019228,
52.232537
],
[
21.019989,
52.232925
],
[
21.019109,
52.233201
]
]
}
}
]
}
I tried to configure the entity by using @ElementCollection and @Embedded.
GeoJson entity:
@Getter
@Setter
@ToString
@RequiredArgsConstructor
@Entity
public class GeoJson {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String type;
@ElementCollection(targetClass = Feature.class)
private List<Feature> features;
}
Feature:
@Getter
@Setter
@ToString
@RequiredArgsConstructor
@Embeddable
public class Feature {
private String type;
@Embedded
private Properties properties;
@Embedded
private Geometry geometry;
}
Properties:
@Getter
@Setter
@ToString
@RequiredArgsConstructor
@Embeddable
public class Properties {
private String type;
}
Geometry:
@Getter
@Setter
@ToString
@RequiredArgsConstructor
@Embeddable
public class Geometry {
private String type;
@ElementCollection(targetClass = Coordinate.class)
private List<Coordinate> coordinates;
}
Coordinate:
@Getter
@Setter
@ToString
@RequiredArgsConstructor
@Embeddable
public class Coordinate {
private BigDecimal lat;
private BigDecimal lng;
}
This configuration gives error:
Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not determine type for: java.util.List, at table: geo_json_features, for columns: [org.hibernate.mapping.Column(coordinates)] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.3.15.jar:5.3.15]
I tried other mapping methods like @ManytoOne or @ManytoMany, but none of them works. How can I configure GeoJson entity to persist? Maybe i should add @CollectionTable annotation in both ElementCollection but i don't know how to do it correctly