本文介绍了Hibernate映射异常无法确定以下类型:java.nio.file.Path的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个实体照片,如下所示
I have an entity Photo as below
@Entity
class Photo {
Path imagePath;
public Path getImagePath(){
return imagePath;
// setter
}
在这个实体中,我必须nio.Path我该如何解决这个问题或使db中的表接受字符串作为路径错误堆栈位于
In this entity i have to nio.Path how can i solve thiss problem or make the table in db to accept strings as paththe error stack is below
Caused by: org.hibernate.MappingException: Could not determine type for: java.nio.file.Path, at table: photo, for columns: [org.hibernate.mapping.Column(image_path)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:431)
推荐答案
您可以使用AttributeConverter
.
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
@Converter // may want to set autoApply to true
public class PathConverter implements AttributeConverter<Path, String> {
@Override
public String convertToDatabaseColumn(Path attribute) {
return attribute == null ? null : attribute.toString();
}
@Override
public Path convertToEntityAttribute(String dbData) {
return dbData == null ? null : Paths.get(dbData);
}
}
此转换器示例将仅存储Path
的路径部分.它不会保留任何其他信息,例如它属于什么FileSystem
(并且在从String
转换为Path
时将采用默认的FileSystem
).
This converter example will only store the path part of the Path
. It won't maintain any other information such as what FileSystem
it belongs to (and will assume the default FileSystem
when converting from String
to Path
).
import java.nio.file.Path;
import javax.persistence.Convert;
import javax.persistence.Entity;
@Entity
public class Photo {
@Convert(converter = PathConverter.class) // needed if autoApply isn't true
private Path imagePath;
}
有关更多信息,请参见以下文档:
See the documentation of the following for more information:
这篇关于Hibernate映射异常无法确定以下类型:java.nio.file.Path的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!