rajugaadu
rajugaadu

Reputation: 704

Grails: Serializable domain property

I am trying to have a domain property which would store a serialized object.

Example:

class AuditReportLog {

    String entityName
    Report report
    // I would ideally like to declare it as:
    // Object reportObject

    static constraints = {
        entityName nullable:true
    report nullable:true
    }
}

The idea is to persist an object in it's entirety into the AuditReportLog table in DB as a BLOB instace, of course assuming that a serialized object will be saved as a BLOB value.

When I set an object to Report property and save the instance, it doesn't persist at all. I tried to find some online references on how we can do this, but didn't find any clean instructions.

Could anyone help here please? Let me know if my question needs any more clarity.

Upvotes: 4

Views: 1862

Answers (1)

Yatin Makkad
Yatin Makkad

Reputation: 126

// Object to save in the domain

class Avatar implements Serializable {
    private static final long serialVersionUID = -319053589578336L;
    private String name
    private String extension
    private byte[] file

    public Avatar(String name, String extension, byte[] file) {
        this.name = name
        this.extension = extension
        this.file = file
    }
    public String getImageExtension(){
        return extension
    }
    public String getImageName(){
        return name
    }

    public byte[] getImage(){
        return file
    }
}


// Domain in which Avatar is storing there
class ClientAvatar {

    Avatar picture

    static constraints = {
        picture nullable: true
    }

    static mapping = {
        picture sqlType: 'LONGBLOB'
    }
}

Upvotes: -1

Related Questions