We are doing exactly that with the "original" Java Hibernate3. You just map a byte array property of your persistable class to an column of Type "image".
package com.hibernate.pdf.sample;
public class TPDFDocument implements java.io.Serializable {
private Integer pdfDocumentId;
private byte[] document;
public Integer getPdfDocumentId() {
return this.pdfDocumentId;
}
public void setPdfDocumentId(Integer pdfDocumentId) {
this.pdfDocumentId = pdfDocumentId;
}
public byte[] getDocument() {
return this.document;
}
public void setDocument(byte[] document) {
this.document = document;
}
}
Hibernate Mapping:
<hibernate-mapping>
<class name="com.hibernate.pdf.sample.TPDFDocument" table="T_PDFDocument">
<id name="pdfDocumentId" type="integer">
<column name="pdfDocumentId" />
<generator class="identity" />
</id>
<property name="document" type="binary">
<column name="document" not-null="true" />
</property>
</class>
</hibernate-mapping>
Table creation:
CREATE TABLE [dbo].[T_PDFDocument](
[pdfDocumentId] [int] IDENTITY(1,1) NOT NULL,
[document] [image] NOT NULL,
CONSTRAINT [PK_PDFDocument] PRIMARY KEY CLUSTERED
(
[pdfDocumentId] ASC
)
All you have to do is to read the documents raw bytes into the array and persist it. In our situation the documents will get hardly larger than 1MB , so putting the whole thing into the byte-array won't cause performance issues. Maybe this solution isn't feasable for very large documents.
I guess with NHibernate implementation and C# the solution will look very similar.