更新时间:2023-09-11 23:24:10
您可以使用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: