目前要求有一个对象 - “应用程序”,它具有以下文件:“名称”、“上传时间”和“执行文件”。
目前数据库是mongodb。我想使用文档
Class Application {
String id;
String name;
String type;
Date uploadTime;
GridFSDBFile executeFile;
}
Dao层,我写save的时候发现不能直接关联GridFSDBfile到文档。我需要先使用 GridFsTemplate.store 保存 GridDBFile,然后使用 GridFsTemplate.findOne 按名称查找,并将对象嵌套在应用程序文档中。然后使用 MongoOperation 保存应用程序文件。
try {
inputStream = new FileInputStream("C:/testing.app");
gridFsOperations.store(inputStream, "test.app", "jar");
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Query query = query(where("filename").is("test.app"));
GridFSDBFile gridFsDBFile = gridFsOperations.findOne(query);
Application application = new Application ();
application.setName( "test-app");
application.setType( "type", "Java");
application.setexecuteFile (gridFsDBFile);
applicationDao.save(application);
有什么方法可以调用 MongoOperation 吗?如果没有,我认为调用 GridFsTemplate.store 和 MongoOperation.save 需要在事务中执行。我记得 Mongo 不支持事务。这个怎么做?只检查 Java 代码中的事务?