Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java findbyuid()不';使用Hibernate时不能在MySQL数据库中工作_Java_Mysql_Spring_Hibernate_Spring Data Jpa - Fatal编程技术网

Java findbyuid()不';使用Hibernate时不能在MySQL数据库中工作

Java findbyuid()不';使用Hibernate时不能在MySQL数据库中工作,java,mysql,spring,hibernate,spring-data-jpa,Java,Mysql,Spring,Hibernate,Spring Data Jpa,我正在开发一个SpringREST服务(使用SpringDataJPA),我的实体包含java.util.UUID类型的属性。我正在使用MySQL作为数据库,这会导致问题。到目前为止,除了UUID是查询的一部分的存储库方法外,其他方法都可以正常工作,例如:entityRepository.findByUuid(UUID UUID) 默认情况下,数据存储在二进制(255)列中。从存储库获取UUID工作正常,唯一的问题是在查询中使用UUID,如FindByUID()。它总是告诉我在数据库中找不到特定

我正在开发一个SpringREST服务(使用SpringDataJPA),我的实体包含java.util.UUID类型的属性。我正在使用MySQL作为数据库,这会导致问题。到目前为止,除了UUID是查询的一部分的存储库方法外,其他方法都可以正常工作,例如:
entityRepository.findByUuid(UUID UUID)

默认情况下,数据存储在二进制(255)列中。从存储库获取UUID工作正常,唯一的问题是在查询中使用UUID,如FindByUID()。它总是告诉我在数据库中找不到特定的UUID。同样的问题也发生在MariaDB身上

我的服务与H2数据库正常工作。你知道为什么MySQL(和MariaDB)会有这个问题吗

DB配置:

spring.datasource.url=jdbc:mysql://localhost/abc123
spring.datasource.username=alfkmakfaf
spring.datasource.password=aafkmafmlaf
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database=mysql
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
实体中的UUID

@Entity
public class Thema {
    // without any annotations, works fine with H2 Database
    private UUID uuid;
...

我发现最简单的方法就是将UUID转换成字符串插入db。毫无疑问,在db中占据了更多的空间,但我找不到任何其他可行的方法

为了避免必须在每个UUID字段上放置
@Convert
,请通过
@converter(autoApply=true)

@转换器(autoApply=true)
公共类UUIDConverter实现AttributeConverter{
@凌驾
公共字符串convertToDatabaseColumn(UUID UUID){
返回uuid.toString();
}
@凌驾
公共UUID convertToEntityAttribute(字符串s){
从字符串返回UUID.fromString;
}
} 

如果我在模型中添加此addnotation(将binary更改为String列)并开始工作,则二进制UUID(默认列为binary类型)存在此问题

@Type(type="org.hibernate.type.UUIDCharType")

您需要查看日志文件并调查JPA使用的SQL查询中的问题。尝试直接在DB-client中运行提取的查询。您是否参考了此内容。这可能会有所帮助。
@Type(type="org.hibernate.type.UUIDCharType")