Skip to content

Abstract Method NullSafeSet SessionImplementor S3Blob Issue #4

Description

@cmazzochi81

This error is occurring when my application is attempting to save information about a photo into a database. Just before it does that it sends the photo to Amazon S3. And that is a successful operation. But when it tries to save the information about the photo into my database, the app crashes.

Can anybody see what I need to do in order to get through this error? I'd appreciate it. Thanks, CM

Here's the stack trace:
hibernateerror

It boils down to these two methods:

@Override
public Object nullSafeGet(ResultSet rs, String[] names, Object o, 
SessionImplementor session) throws HibernateException, SQLException {
    String val = StringType.INSTANCE.nullSafeGet(rs, names[0]);
    if (val == null || val.length() == 0 || !val.contains("|")) {
        return new S3Blob();
    }
    return new S3Blob(val.split("[|]")[0], val.split("[|]")[1]);
}

@Override
public void nullSafeSet(PreparedStatement ps, Object o, int i, 
SessionImplementor session) throws HibernateException, SQLException {
    if (o != null) {
        ps.setString(i, ((S3Blob) o).bucket + "|" + ((S3Blob) o).key);
    } else {
       ps.setNull(i, Types.VARCHAR);
    }
}

Here's that entire class:

play.modules.s3blobs.S3Blob

import java.sql.SQLException;
import java.sql.Types;

import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.type.StringType;
import org.hibernate.usertype.UserType;

import play.db.Model.BinaryField;
import play.libs.Codec;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.S3Object;

public class S3Blob implements BinaryField, UserType {

static String s3Bucket;
static AmazonS3 s3Client;
private String bucket;
private String key;

public S3Blob() {
}

private S3Blob(String bucket, String s3Key) {
    this.bucket = bucket;
    this.key = s3Key;
}

@Override
public InputStream get() {
    S3Object s3Object = s3Client.getObject(bucket, key);
    return s3Object.getObjectContent();
}

@Override
public void set(InputStream is, String type) {
    this.bucket = s3Bucket;
    this.key = Codec.UUID();
    ObjectMetadata om = new ObjectMetadata();
    om.setContentType(type);
    s3Client.putObject(bucket, key, is, om);
}

@Override
public long length() {
    ObjectMetadata om = s3Client.getObjectMetadata(bucket, key);
    return om.getContentLength();
}

@Override
public String type() {
    ObjectMetadata om = s3Client.getObjectMetadata(bucket, key);
    return om.getContentType();
}

@Override
public boolean exists() {
    ObjectMetadata om = s3Client.getObjectMetadata(bucket, key);
    return om != null;
}

@Override
public int[] sqlTypes() {
    return new int[] { Types.VARCHAR };
}

@Override
public Class returnedClass() {
    return S3Blob.class;
}

@Override
public boolean equals(Object o, Object o1) throws HibernateException {
    return o == null ? false : o.equals(o1);
}

@Override
public int hashCode(Object o) throws HibernateException {
    return o.hashCode();
}

@Override
public Object nullSafeGet(ResultSet rs, String[] names, Object o, SessionImplementor session) throws 
HibernateException, SQLException {
    String val = StringType.INSTANCE.nullSafeGet(rs, names[0]);
    if (val == null || val.length() == 0 || !val.contains("|")) {
        return new S3Blob();
    }
    return new S3Blob(val.split("[|]")[0], val.split("[|]")[1]);
}

@Override
public void nullSafeSet(PreparedStatement ps, Object o, int i, SessionImplementor session) throws 
HibernateException, SQLException {
    if (o != null) {
        ps.setString(i, ((S3Blob) o).bucket + "|" + ((S3Blob) o).key);
    } else {
        ps.setNull(i, Types.VARCHAR);
    }
}

@Override
public Object deepCopy(Object o) throws HibernateException {
    if (o == null) {
        return null;
    }
    return new S3Blob(this.bucket, this.key);
}

@Override
public boolean isMutable() {
    return true;
}

@Override
public Serializable disassemble(Object o) throws HibernateException {
    throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public Object assemble(Serializable srlzbl, Object o) throws HibernateException {
    throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public Object replace(Object o, Object o1, Object o2) throws HibernateException {
    throw new UnsupportedOperationException("Not supported yet.");
}
}

Application.java around line 56

public static void doCreateItem(
    @Valid String title, 
    @Valid Integer days, 
    @Valid Float startBid, 
    @Valid Float buyNowPrice,
    @Valid Float deliveryCost,
    @Valid Boolean buyNowEnabled,
    @Valid String description, 
@Valid File photo) throws FileNotFoundException

{

if (validation.hasErrors()){
params.flash();
validation.keep();
createAuctionItem();    
}
final AuctionItem item = new AuctionItem();
item.photo = new S3Blob();
item.photo.set(new FileInputStream(photo), 
    MimeTypes.getContentType(photo.getName()));
item.createdBy = Authenticate.getLoggedInUser();
(Line56) item.save();
show(item.id);
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions