如何解决java中Set<Path>的Hibernate映射异常?
How to solve Hibernate mapping exception for Set<Path> in java?
我想将目录保存到我的文件路径树的数据库中,但在初始化 Hibernate 时出现此错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [hibernate-config.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: Could not determine type for: java.nio.file.Path, at table: BasePlan_selectedPaths, for columns: [org.hibernate.mapping.Column(selected_paths)]
是我的文件路径树:
@Column(name = "selected_paths")
@ElementCollection(targetClass = Path.class)
private Set<Path> selectedPaths;
您是否尝试保存目录和子目录的路径?
列的数据类型是什么:selected_paths。我的猜测是 varchar
在这种情况下,您可以将路径映射为 Java 中的字符串类型。
我认为(如果我错了请纠正我),Java 无法确定 nio 路径和数据库中相应数据类型之间的映射。
从数据库取回数据时,您可以将其作为 String 获取,并轻松将其用作 Path
您可以将字段声明为 String
并在 getter 中使用 Path
。
@Column(name = "selected_paths")
@ElementCollection(targetClass = Path.class)
private Set<String> selectedPaths;
public Set<Path> getSelectedPaths() {
return selectedPaths.stream().map(Paths::get).collect(Collectors.toSet());
}
我创建了一个转换器 class。之后我修改了我的字段。 Hibernate 创建一个 table 将像字符串一样保存路径。
public class PathConverter implements AttributeConverter<Path, String> {
@Override
public String convertToDatabaseColumn(Path path) {
return path.toString();
}
@Override
public Path convertToEntityAttribute(String path) {
return Paths.get(path);
}
}
@Column(name = "selected_paths")
@ElementCollection(targetClass = Path.class)
@Convert(converter = PathConverter.class)
private Set<Path> selectedPaths;
@Column(name = "unselected_paths")
@ElementCollection(targetClass = Path.class)
@Convert(converter = PathConverter.class)
private Set<Path> unSelectedPaths;
我想将目录保存到我的文件路径树的数据库中,但在初始化 Hibernate 时出现此错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [hibernate-config.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: Could not determine type for: java.nio.file.Path, at table: BasePlan_selectedPaths, for columns: [org.hibernate.mapping.Column(selected_paths)]
@Column(name = "selected_paths")
@ElementCollection(targetClass = Path.class)
private Set<Path> selectedPaths;
您是否尝试保存目录和子目录的路径? 列的数据类型是什么:selected_paths。我的猜测是 varchar
在这种情况下,您可以将路径映射为 Java 中的字符串类型。
我认为(如果我错了请纠正我),Java 无法确定 nio 路径和数据库中相应数据类型之间的映射。
从数据库取回数据时,您可以将其作为 String 获取,并轻松将其用作 Path
您可以将字段声明为 String
并在 getter 中使用 Path
。
@Column(name = "selected_paths")
@ElementCollection(targetClass = Path.class)
private Set<String> selectedPaths;
public Set<Path> getSelectedPaths() {
return selectedPaths.stream().map(Paths::get).collect(Collectors.toSet());
}
我创建了一个转换器 class。之后我修改了我的字段。 Hibernate 创建一个 table 将像字符串一样保存路径。
public class PathConverter implements AttributeConverter<Path, String> {
@Override
public String convertToDatabaseColumn(Path path) {
return path.toString();
}
@Override
public Path convertToEntityAttribute(String path) {
return Paths.get(path);
}
}
@Column(name = "selected_paths")
@ElementCollection(targetClass = Path.class)
@Convert(converter = PathConverter.class)
private Set<Path> selectedPaths;
@Column(name = "unselected_paths")
@ElementCollection(targetClass = Path.class)
@Convert(converter = PathConverter.class)
private Set<Path> unSelectedPaths;