专栏首页一个会写诗的程序员的博客Caused by: org.hibernate.HibernateException: Unanticipated return type [java.lang.Long] for UUID ...

Caused by: org.hibernate.HibernateException: Unanticipated return type [java.lang.Long] for UUID ...

代码

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
}


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 id = FieldUtils.readDeclaredField(obj, "id")
        if (id != null) {
            return id as Serializable
        }
        return super.generate(session, obj)
    }

}

报错日志

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.readField(obj, "code")
        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 :)

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • SpringBoot 集成Kotlin : org.hibernate.InstantiationException: No default constructor for entity: : com

    一个会写诗的程序员
  • Spring Boot集成Security使用数据库用户角色权限用户名问题问题描述原因分析解决方案

    sql语法手误。1?这地方写错了,应该是?1。这在敲代码的时候,手速一旦稍有不慎,就会导致前后顺序颠倒,而导致输入错误。这个虽然说是“低级错误”,但是错误搞起来...

    一个会写诗的程序员
  • 【Kotlin Spring Boot 服务端开发: 问题集锦】Spring Security 5 : There is no PasswordEncoder mapped for the id "

    【Kotlin Spring Boot 服务端开发: 问题集锦】Spring Security 5 : There is no PasswordEncoder...

    一个会写诗的程序员
  • MysqlDataTruncation: Data truncation: Incorrect string value: '\xF0\x9D\x90\xB6"#...' for column

    版权声明:本文为博主原创文章,欢迎转载。 ...

    程裕强
  • Spring Cloud Config Server迁移节点或容器化带来的问题

    为了说明下面的内容,我们可以先尝试重现一下问题:在一个测试环境中,将Spring Cloud Config的配置中心迁移到另外一个节点上,即配置中心的IP地址发...

    程序猿DD
  • Hibernate 5 在保存数据的时候提示数据库没有选择

    09:06:40.277 [http-nio-8080-exec-1] ERROR o.h.id.enhanced.TableStructure - could...

    HoneyMoose
  • org.springframework.orm.hibernate3.HibernateQueryException: unexpected token: where near line 1, col

    org.springframework.orm.hibernate3.HibernateQueryException: unexpected token: wh...

    qubianzhong
  • com.mysql.jdbc.PacketTooBigException: Packet for query is too large (1146177 > 1048576).

    版权声明:本文为博主原创文章,欢迎转载。 ...

    程裕强
  • .MysqlDataTruncation: Data truncation: Data too long for column 'content' at row 1

    版权声明:本文为博主原创文章,欢迎转载。 ...

    程裕强
  • IP不能作为rocketmq 的topic

    MickyInvQ

扫码关注云+社区

领取腾讯云代金券