我想将MySQL JSON列映射到JPA。我做了一些研究,然后使用
@Type( type = "json" )
@Column( columnDefinition = "json" )
但这会引发异常
Exception in thread "main" org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [json]
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:348)
at org.hibernate.boot.internal.ClassLoaderAccessImpl.classForName(ClassLoaderAccessImpl.java:62)
at org.hibernate.cfg.annotations.SimpleValueBinder.fillSimpleValue(SimpleValueBinder.java:538)
at org.hibernate.cfg.SetSimpleValueTypeSecondPass.doSecondPass(SetSimpleValueTypeSecondPass.java:25)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1621)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1579)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:858)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:885)
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:58)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
at com.savemonk.ce.utility.hibernateAndJPA.JPAUtility.getEntityManagerFactory(JPAUtility.java:12)
at com.savemonk.ce.v2.pricegraph.manager.AddNewPriceTrackRecord.addNewPriceTrackRecordTest(AddNewPriceTrackRecord.java:29)
at com.savemonk.ce.v2.pricegraph.manager.AddNewPriceTrackRecord.main(AddNewPriceTrackRecord.java:23)
Caused by: java.lang.ClassNotFoundException: Could not load requested class : json
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl$AggregatedClassLoader.findClass(ClassLoaderServiceImpl.java:336)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:345)
... 14 more
我知道@Type是休眠的org.hibernate.annotations.Type 在JPA中映射列的正确方法是什么?
如果需要,我可以发布实体类。 任何解释JPA + JSON-type-column的链接和资源都会有很大帮助。
答案 0 :(得分:0)
在找到有关此问题的更多资源后,我碰到了vladmihalcea的这篇文章。
https://vladmihalcea.com/how-to-map-json-objects-using-generic-hibernate-types/
他的Hibernate类型库解决了JSON和JPA的各种问题。
答案 1 :(得分:0)
除了依赖以外,您还需要添加
@TypeDef(name = "json", typeClass = JsonStringType.class)
到您的实体类声明之一