gpt4 book ai didi

java - 如何解决java中Set的Hibernate映射异常?

转载 作者:行者123 更新时间:2023-12-02 02:16:38 24 4
gpt4 key购买 nike

我想将目录保存到数据库作为我的文件路径树,但在初始化 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)]

Here是我的文件路径树:

@Column(name = "selected_paths")
@ElementCollection(targetClass = Path.class)
private Set<Path> selectedPaths;

最佳答案

我创建了一个转换器类。之后我修改了我的字段。 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;

关于java - 如何解决java中Set<Path>的Hibernate映射异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49220739/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com