javascript
如何使用JPA和Hibernate映射JSON集合
介紹
開源的hibernate-types項目允許您將Java對象或Jackson JsonNode為JPA實體屬性。
最近,感謝我們的杰出貢獻者,我們添加了對類型安全集合的支持,該集合也可以作為JSON持久化。 在本文中,您將了解如何實現此目標。
Maven依賴
首先,您需要在項目pom.xml配置文件中設置以下Maven依賴項:
<dependency><groupId>com.vladmihalcea</groupId><artifactId>hibernate-types-52</artifactId><version>${hibernate-types.version}</version> </dependency>如果您使用的是較早版本的Hibernate,請查看hibernate-types GitHub存儲庫 ,以獲取有關當前Hibernate版本的匹配依賴項的更多信息。
領域模型
假設我們具有以下Location Java對象類型。
public class Location implements Serializable {private String country;private String city;//Getters and setters omitted for brevity@Overridepublic String toString() {return "Location{" +"country='" + country + '\'' +", city='" + city + '\'' +'}';} }并且,一個Event實體:
@Entity(name = "Event") @Table(name = "event") public class Event extends BaseEntity {@Type(type = "jsonb")@Column(columnDefinition = "jsonb")private Location location;@Type(type = "jsonb",parameters = {@org.hibernate.annotations.Parameter(name = TypeReferenceFactory.FACTORY_CLASS,value = "com.vladmihalcea.hibernate.type.json.PostgreSQLGenericJsonBinaryTypeTest$AlternativeLocationsTypeReference")})@Column(columnDefinition = "jsonb")private List<Location> alternativeLocations = new ArrayList<Location>();//Getters and setters omitted for brevity }BaseEntity定義了一些基本屬性(例如@Id @Version , @Id @Version )和幾種海關Hibernate類型,其中,我們對JsonBinaryType感興趣。
@TypeDefs({@TypeDef(name = "string-array", typeClass = StringArrayType.class),@TypeDef(name = "int-array", typeClass = IntArrayType.class),@TypeDef(name = "json", typeClass = JsonStringType.class),@TypeDef(name = "jsonb", typeClass = JsonBinaryType.class),@TypeDef(name = "jsonb-node", typeClass = JsonNodeBinaryType.class),@TypeDef(name = "json-node", typeClass = JsonNodeStringType.class), }) @MappedSuperclass public class BaseEntity {@Idprivate Long id;@Versionprivate Integer version;//Getters and setters omitted for brevity }有關使用@MappedSuperclass更多詳細信息,請@MappedSuperclass 本文 。
TypeReferenceFactory
要將Location對象存儲在jsonb PostgreSQL列中,我們只需要使用@Type(type = "jsonb")注釋location屬性。
但是,對于alternativeLocations集合,我們需要提供關聯的Jackson TypeReference以便在從關系數據庫中讀取JSON對象時可以重建非常相同的類型安全的Java集合。
為此,我們提供TypeReferenceFactory實現的完全限定的類,如下所示:
public static class AlternativeLocationsTypeReference implements TypeReferenceFactory {@Overridepublic TypeReference<?> newTypeReference() {return new TypeReference<List<Location>>() {};} }而已!
測試時間
保存以下Event實體時:
Location cluj = new Location(); cluj.setCountry("Romania"); cluj.setCity("Cluj-Napoca");Location newYork = new Location(); newYork.setCountry("US"); newYork.setCity("New-York");Location london = new Location(); london.setCountry("UK"); london.setCity("London");Event event = new Event(); event.setId(1L); event.setLocation(cluj); event.setAlternativeLocations(Arrays.asList(newYork, london) );entityManager.persist(event);Hibernate將生成以下SQL INSERT語句:
INSERT INTO event (version, alternativeLocations, location, id ) VALUES (0, [{"country":"US","city":"New-York"},{"country":"UK","city":"London"}], {"country":"Romania","city":"Cluj-Napoca"}, 1 )此外,檢索回時Event實體,無論是location ,并the alternativeLocations`屬性是正確的獲取:
事件event = entityManager.find(Event.class,eventId);
assertEquals("Cluj-Napoca", event.getLocation().getCity() );assertEquals(2, event.getAlternativeLocations().size());assertEquals("New-York", event.getAlternativeLocations().get(0).getCity() ); assertEquals("London", event.getAlternativeLocations().get(1).getCity() );酷吧?
翻譯自: https://www.javacodegeeks.com/2017/12/map-json-collections-using-jpa-hibernate.html
總結
以上是生活随笔為你收集整理的如何使用JPA和Hibernate映射JSON集合的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (win10ddos)
- 下一篇: java ssl证书_Java安全教程–