Wednesday, July 22, 2009

HibernateSystemException: attempted to assign id from null one-to-one property

I had a bidirectional relationship in my one-to-one mapping, however I had forgotten to set the objects in my Java code to reference each other. Once I had done that, the problem was resolved.

SpermAnalysis sa = new SpermAnalysis();
cryoSession.getControlMouse().setSpermAnalysis(sa);
// Bidirectional relationship
sa.setMouse(cryoSession.getControlMouse());

Determine Mime type from bytes

If you have an image file stored in the database but don't have the extension of the file, then you may not know what the mime type should be.

There is a handy Java library that will inspect the bytes to determine the Mime type. The library can be found here:

MimeUtil

It's fairly easy to use. An example code is shown below:

byte[] bytes;
MimeUtil.registerMimeDetector("eu.medsea.mimeutil.detector.MimeMagicMimeDetector");
MimeUtil.getMimeTypes(bytes);
MimeUtil.unregisterMimeDetector("eu.medsea.mimeutil.detector.MimeMagicMimeDetector");