NHibernate MappingException: no persister for byte[]

I'm using NHibernate to store downloads in my MySQL database for an ASP.NET MVC website. I am using to classes. One called Download for the download itself and one called DownloadContent for the file itself (so I can load it easier when I just want to get the metadata).

The data class declarations and mappings look like this:

public class Download
{
    public virtual string Id { get; set; }
    public virtual string OutFileName { get; set; }
    public virtual DownloadContent Contents { get; set; }
    public virtual string MimeType { get; set; }
    public virtual bool DoForward { get; set; }
    public virtual string RedirectLink { get; set; }
}

public class DownloadMap : ClassMap<Download>
{
    public DownloadMap()
    {
        Id(x => x.Id);
        Map(x => x.OutFileName);
        References<DownloadContent>(x => x.Contents);
        Map(x => x.MimeType);
        Map(x => x.DoForward).Not.Nullable();
        Map(x => x.RedirectLink);
    }
}

public class DownloadContent
{
    public virtual byte[] Data { get; set; }
}

public class DownloadContentMap : ClassMap<DownloadContent>
{
    public DownloadContentMap()
    {
        Id();
        Map(x => x.Data).CustomType("BinaryBlob");
    }
}

Now, when I try to do like this:

dl.Contents = new DownloadContent { Data = content };
db.session.SaveOrUpdate(content);

I get an NHibernate.MappingException with the message "No persister for: System.Byte[]". I looked it up with the NHibernate docs and byte[] should map correctly.

What am I doing wrong?

Upvotes: 2

Views: 2179

Answers (2)

Florian Lim
Florian Lim

Reputation: 5362

If I read that correctly you are actually trying to save the byte[] to the DB, which can't work, since byte[] is not a mapped entity.

You probably want to write:

dl.Contents = new DownloadContent { Data = content };
db.session.SaveOrUpdate(dl); // content is wrong, since content is of type byte[]

Also, since you didn't specify an Inverse(), you will probably have to SaveOrUpdate the DownloadContent first, therefore:

Download dl = new Download { OutFileName = "Test", DoForward = true };
DownloadContent dlc = new DownloadContent { Data = content };
dl.Contents = dlc;
db.session.SaveOrUpdate(dlc);
db.session.SaveOrUpdate(dl);

Upvotes: 2

James Kovacs
James Kovacs

Reputation: 11661

You specified a CustomType of BinaryBlob. NHibernate will look for an IUserType called BinaryBlob to perform the persistence. I think you want CustomSqlType to say that MySQL should use its BinaryBlob type in the database.

public class DownloadContentMap : ClassMap<DownloadContent>
{
    public DownloadContentMap()
    {
        Id();
        Map(x => x.Data).CustomSqlType("BinaryBlob");
    }
}

Upvotes: 2

Related Questions