Java 如何解决集合的Hibernate映射异常<;路径>;在爪哇?

Java 如何解决集合的Hibernate映射异常<;路径>;在爪哇?,java,spring,hibernate,hibernate-mapping,Java,Spring,Hibernate,Hibernate Mapping,我想为我的文件路径树将目录保存到数据库中,但在初始化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 except

我想为我的文件路径树将目录保存到数据库中,但在初始化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;
@列(name=“选定的路径”)
@ElementCollection(targetClass=Path.class)
私有集选择路径;

是否要保存目录和子目录的路径? 列的数据类型是什么:所选路径。我猜是varchar

在这种情况下,可以在Java中将路径映射为字符串类型

我认为(如果我错了,请纠正我),Java无法确定nio路径和数据库中相应数据类型之间的映射


当从数据库中取回数据时,您可以将其作为字符串获取,并轻松地将其用作路径

您正在尝试保存目录和子目录的路径吗? 列的数据类型是什么:所选路径。我猜是varchar

在这种情况下,可以在Java中将路径映射为字符串类型

我认为(如果我错了,请纠正我),Java无法确定nio路径和数据库中相应数据类型之间的映射


当从数据库中取回数据时,可以将其作为字符串获取,并轻松地将其用作路径

您可以将字段声明为
字符串
并在getter中使用
路径

@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());
}
@列(name=“选定的路径”)
@ElementCollection(targetClass=Path.class)
私有集选择路径;
公共集GetSelectedPath(){
返回selectedPaths.stream().map(path::get.collect(Collectors.toSet());
}

您可以将字段声明为
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());
}
@列(name=“选定的路径”)
@ElementCollection(targetClass=Path.class)
私有集选择路径;
公共集GetSelectedPath(){
返回selectedPaths.stream().map(path::get.collect(Collectors.toSet());
}

我创建了一个转换器类。之后,我修改了我的字段。Hibernate创建一个表,它可以像字符串一样保存路径

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;
公共类PathConverter实现AttributeConverter{
@凌驾
公共字符串convertToDatabaseColumn(路径){
返回路径.toString();
}
@凌驾
公共路径convertToEntityAttribute(字符串路径){
返回路径。获取(路径);
}
}
@列(name=“所选路径”)
@ElementCollection(targetClass=Path.class)
@Convert(converter=PathConverter.class)
私有集选择路径;
@列(name=“未选择的路径”)
@ElementCollection(targetClass=Path.class)
@Convert(converter=PathConverter.class)
私有设置未选择的路径;

我创建了一个转换器类。之后,我修改了我的字段。Hibernate创建一个表,它可以像字符串一样保存路径

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;
公共类PathConverter实现AttributeConverter{
@凌驾
公共字符串convertToDatabaseColumn(路径){
返回路径.toString();
}
@凌驾
公共路径convertToEntityAttribute(字符串路径){
返回路径。获取(路径);
}
}
@列(name=“所选路径”)
@ElementCollection(targetClass=Path.class)
@Convert(converter=PathConverter.class)
私有集选择路径;
@列(name=“未选择的路径”)
@ElementCollection(targetClass=Path.class)
@Convert(converter=PathConverter.class)
私有设置未选择的路径;

还没有任何数据库。如果您还没有配置任何数据库,也没有表,那么它就无法将字段映射到列@列注释放在@Entity类中,该类表示DB的表。目前还没有任何DB。如果尚未配置任何数据库,并且没有表,则无法将字段映射到列@列注释放置在@Entity类中,该类表示DB的表。