我有一个春天@Documentobject Profile
我想像这样引用 GridFSFile :
@DbRef
private GridFSFile file;
该文件被写入另一个集合类型GridFS。
我总是有一个java.lang.StackOverflowError
当我设定profile.setFile(file);
java.lang.StackOverflowError
at org.springframework.util.ObjectUtils.nullSafeHashCode(ObjectUtils.java:336)
at org.springframework.data.util.TypeDiscoverer.hashCode(TypeDiscoverer.java:365)
at org.springframework.data.util.ClassTypeInformation.hashCode(ClassTypeInformation.java:39)
at org.springframework.util.ObjectUtils.nullSafeHashCode(ObjectUtils.java:336)
at org.springframework.data.util.ParentTypeAwareTypeInformation.hashCode(ParentTypeAwareTypeInformation.java:79)
at org.springframework.util.ObjectUtils.nullSafeHashCode(ObjectUtils.java:336)
我不明白,如果有人有想法引用我感兴趣的文件
谢谢,
泽维尔
我想要类似的东西,但没有找到方法,所以我做了这个解决方法。
在您的 @Document 类中,放置一个ObjectId
field
@Document
public class MyDocument {
//...
private ObjectId file;
}
然后在您的存储库中,添加自定义方法以将文件链接到此 MyDocument,如下奥利弗·吉尔克的建议 https://stackoverflow.com/a/17010650/2452245并使用GridFsTemplate
:
public class MyDocumentRepositoryImpl implements MyDocumentRepositoryCustom {
public static final String MONGO_ID = "_id";
@Autowired
GridFsTemplate gridFsTemplate;
@Override
public void linkFileToMyDoc(MyDocument myDocument, InputStream stream, String fileName) {
GridFSFile fsFile = gridFsTemplate.store(stream, fileName);
myDocument.setFile( (ObjectId) fsFile.getId());
}
@Override
public void unLinkFileToMyDoc(MyDocument myDocument)
{
ObjectId objectId = myDocument.getFile();
if (null != objectId) {
gridFsTemplate.delete( Query.query(Criteria.where(MONGO_ID).is(objectId)) );
myDocument.setFile(null);
}
}
}
顺便说一句,您需要声明您的GridFsTemplate
在你的 JavaConf 中自动装配它
@Bean
public GridFsTemplate gridFsTemplate() throws Exception {
return new GridFsTemplate(mongoDbFactory(), mappingMongoConverter());
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)