Morphia 成功支持 GridFS

I know this question has been asked on the forums before so I wanted 
to contribute an example that I just completed. 

I am using Spring to inject everything into my constructor and using 
Mule to lookup to the DAO. 
 private Morphia morphia; 
    private Mongo mongo; 
    private String dbName; 
    protected GridFS gridStore; 

    protected NemsisLoggingDAO(Mongo mongo, Morphia morphia, String 
dbName, String bucket) { 
        super(mongo, morphia, dbName); 
        gridStore = new GridFS(mongo.getDB(dbName), bucket); 
    } 

    public Object create(String fileName, String mimeType, InputStream 
fileStream) throws IOException { 
        GridFSInputFile localFile = gridStore.createFile(fileStream, 
fileName); 
        localFile.setContentType(mimeType); 
        localFile.save(); 
        return localFile.getId(); 
    } 

Now in my annotated object model: 

   
 @Property("xml_attachment") 
    private Object xmlAttachment; 

    @Property("pdf_attachment") 
    private Object pdfAttachment; 

Then when I call my DAO: 

        Object xmlFile = null; 
        Object pdfFile = null; 
        ...... 

            SimpleDateFormat sdf = new 
SimpleDateFormat("MMddyyy_HH:mm:ss_zzz"); 
            xmlFile = nlDAO.create("XML_" + xp.evaluate("//E06_02", d) 
+ "_" + xp.evaluate("//E06_01", d) + "_" + sdf.format(new Date()), 
"application/xml",  convertXMLToInputStream(d)); 
            pdfFile = nlDAO.create("PDF_" + xp.evaluate("//E06_02", d) 
+ "_" + xp.evaluate("//E06_01", d) + "_" + sdf.format(new Date()), 
"application/pdf",  runsheet.getRunsheetPDF().getInputStream()); 
            nl.setXmlAttachment(xmlFile); 
            nl.setPdfAttachment(pdfFile); 



Now when I look in MongoDB: 

attachments = 

{ "_id" : ObjectId("4d96590b318d4fce110c4dec"), "chunkSize" : 
NumberLong(262144), "length" : NumberLong(10146), "md5" : 
"9d69465005bfdf2b9c356fecdfc6eda7", "filename" : 
"XML_John_Doe_040111_19:00:27_EDT", "contentType" : "application/xml", 
"uploadDate" : ISODate("2011-04-01T23:00:27.852Z"), "aliases" : null } 
{ "_id" : ObjectId("4d96590b318d4fce110c4dee"), "chunkSize" : 
NumberLong(262144), "length" : NumberLong(170760), "md5" : 
"1851406f2dae5f2646307c47e2f54120", "filename" : 
"PDF_John_Doe_040111_19:00:27_EDT", "contentType" : "application/pdf", 
"uploadDate" : ISODate("2011-04-01T23:00:27.858Z"), "aliases" : null } 

docment = 

{ "_id" : ObjectId("4d96590b318d4fce110c4deb"), "className" : 
"com.axialexchange.model.nemsis.NemsisLogging", "direction" : 
"inbound", "source" : "ESO", "destination" : "allscripts-hmed", 
"message_id" : "11223344", "date_created" : 
ISODate("2011-04-01T23:00:27.830Z"), "date_of_birth" : "1967-08-13", 
"sex" : "M", "last_name" : "Doe", "match" : false, "xml_attachment" : 
ObjectId("4d96590b318d4fce110c4dec"), "pdf_attachment" : 
ObjectId("4d96590b318d4fce110c4dee") } 



If anyone needs additional help, please feel free to ping me.  I have 
been doing this a lot on the RubyOnRails side and this is my first go 

using Morphia on the Java side. 


原文:https://groups.google.com/d/topic/morphia/KldkcpWOOOw/discussion

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值