Caused by: org.hibernate.Hiberna

作者: 光剑书架上的书 | 来源:发表于2019-06-30 06:41 被阅读5次

    关键解决点

    val code = FieldUtils.readDeclaredField(obj, "code", true)
    

    报错的代码

    package com.ak47.cms.cms.entity
    
    import org.hibernate.annotations.GenericGenerator
    import java.util.*
    import javax.persistence.*
    
    @Entity
    @Table(indexes = arrayOf(Index(name = "idx_category", columnList = "category")))
    class Tree {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO, generator = "custom-uuid")
        @GenericGenerator(name = "custom-uuid", strategy = "com.ak47.cms.cms.tree.CustomUUIDGenerator")
        var id: Long = 0
    
        var parentId: Long = 0
    
        @Column(length = 200)
        var name = ""
    
        @Column(length = 100)
        var category = ""
    
        var gmtCreated = Date()
        var gmtModified = Date()
        var isDeleted = 0
        @Version
        var version = 0
    }
    
    

    报错日志

    Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:970)
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:895)
        at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:57)
        at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365)
        at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:388)
        at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:377)
        at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1769)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1706)
        ... 16 common frames omitted
    Caused by: org.hibernate.MappingException: Could not instantiate id generator [entity-name=com.ak47.cms.cms.entity.Tree]
        at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.createIdentifierGenerator(DefaultIdentifierGeneratorFactory.java:124)
        at org.hibernate.mapping.SimpleValue.createIdentifierGenerator(SimpleValue.java:311)
        at org.hibernate.internal.SessionFactoryImpl.lambda$new$1(SessionFactoryImpl.java:287)
        at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184)
        at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
        at java.util.HashMap$ValueSpliterator.forEachRemaining(HashMap.java:1620)
        at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
        at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
        at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
        at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
        at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
        at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
        at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:286)
        at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:460)
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:892)
        ... 23 common frames omitted
    Caused by: org.hibernate.HibernateException: Unanticipated return type [java.lang.Long] for UUID conversion
        at org.hibernate.id.UUIDGenerator.configure(UUIDGenerator.java:96)
        at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.createIdentifierGenerator(DefaultIdentifierGeneratorFactory.java:118)
        ... 37 common frames omitted
    

    解决方案

    package com.ak47.cms.cms.entity
    
    import org.hibernate.annotations.GenericGenerator
    import java.util.*
    import javax.persistence.*
    
    @Entity
    @Table(indexes = arrayOf(Index(name = "idx_category", columnList = "category")))
    class Tree {
    
        @Id
        @Column(length = 100)
        @GeneratedValue(strategy = GenerationType.AUTO, generator = "custom-uuid")
        @GenericGenerator(name = "custom-uuid", strategy = "com.ak47.cms.cms.tree.CustomUUIDGenerator")
        var code: String = "0"
    
        @Column(length = 100)
        var parentCode: String = "0"
    
        @Column(length = 200)
        var name = ""
    
        @Column(length = 100)
        var category = ""
    
        var gmtCreated = Date()
        var gmtModified = Date()
        var isDeleted = 0
        @Version
        var version = 0
    }
    
    
    
    
    package com.ak47.cms.cms.tree
    
    import org.apache.commons.lang3.reflect.FieldUtils
    import org.hibernate.engine.spi.SharedSessionContractImplementor
    import org.hibernate.id.UUIDGenerator
    import java.io.Serializable
    
    
    class CustomUUIDGenerator : UUIDGenerator() {
    
        override fun generate(session: SharedSessionContractImplementor, obj: Any): Serializable {
            val code = FieldUtils.readDeclaredField(obj, "code", true)
            if (code != null) {
                return code as Serializable
            }
            return super.generate(session, obj)
        }
    
    }
    
    
    

    回答1:
    The root exception says it all:

    Caused by: org.hibernate.HibernateException: Unanticipated return type [java.lang.Long] for UUID
    You are using a UUID id generator on a type which it does not support. You should use String instead of Long in this case.

    回答2:
    if you use this deprecated org.hibernate.ejb.HibernatePersistence set new provider like below
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

    if your value is
    <property name="hibernate.hbm2ddl.auto" value="update" />

    set "create-drop"

    <property name="hibernate.hbm2ddl.auto" value="create-drop" />

    XML path: resources/META-INF/persistence.xml
    For mysql
    <?xml version="1.0" encoding="UTF-8"?>

    <persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">

        <persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
            <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
            <properties>
                  <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
                  <property name="hibernate.hbm2ddl.auto" value="create-drop" />
                  <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
                  <property name="hibernate.show_sql" value="true"/>
                  <property name="hibernate.connection.username" value="yourname"/>
                  <property name="hibernate.connection.password" value="yourpassword"/>
                  <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/yourschema"/>
                  <property name="hibernate.max_fetch_depth" value="3"/>
            </properties>
        </persistence-unit>
    
    </persistence>
    

    回答3:
    Firstly you should share your the complete stacktrace about the exception and the persistence.xml content. Then in my opinion based on what I see the problem could be that you don't have declared a persistence provider in your persistence.xml. In fact if you are developing in a non container managed envirorment you need to declare a persistence provider.

    <persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">
    <persistence-unit name="myunit" >
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    //Others properties
    </persistence-unit>

    回答4:
    I do not know if the problem still exists, but I thought you have to put the persistence.xml file into src/main/META-INF folder. I see you put it into src/META-INF. Maybe that is the Problem? and of course the entities you want to link belong in src/main folder, too :)


    Kotlin 开发者社区

    国内第一Kotlin 开发者社区公众号,主要分享、交流 Kotlin 编程语言、Spring Boot、Android、React.js/Node.js、函数式编程、编程思想等相关主题。
    Kotlin 开发者社区 QRCode.jpg

    相关文章

      网友评论

        本文标题:Caused by: org.hibernate.Hiberna

        本文链接:https://www.haomeiwen.com/subject/zdjvcctx.html