我正在尝试将UUID用作Hibernate的主键,并让PostgreSQL或Hibernate自动生成这些ID。理想情况下,我也想拥有java.util.UUID
对象,我也可以使用它。
为此,我正在尝试将java.util.UUID
映射到Postgres中的UUID数据类型。我可以让Hibernate正确生成数据库,并将数据放入实体的表中。但是,通过Spring检索数据,我得到了这个:
Provided id of the wrong type for class com.example.somepackage.Brand. Expected: class java.util.UUID, got class java.lang.String; nested exception is java.lang.IllegalArgumentException: Provided id of the wrong type for class com.example.somepackage.Brand. Expected: class java.util.UUID, got class java.lang.String
道歉,如果这是一个简单的修复;我是Spring / Hibernate的新手。一些帮助将不胜感激。
@Entity
public class Brand {
@javax.persistence.Id
@GenericGenerator(name = "uuid-gen", strategy = "uuid2")
@GeneratedValue(generator = "uuid-gen")
@org.hibernate.annotations.Type(type="pg-uuid")
private UUID id;
private String brand;
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
}
Maven pom.xml如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>spring-rest-entity-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-rest-entity-test</name>
<description>Test</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.3.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>com.example.somepackage.Main</start-class>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-core</artifactId>
<version>2.3.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>1.10.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-webmvc</artifactId>
<version>2.3.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901-1.jdbc4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
答案 0 :(得分:8)
自己解决了。事实证明我忘了更新Repository类以使用java.util.UUID
而不是String
。
@RepositoryRestResource(collectionResourceRel = "brand", path = "brand")
public interface BrandRepo extends PagingAndSortingRepository<Brand, UUID> { // Was previously <Brand, String>
List<Brand> findByBrand(@Param("brand") String brand);
}