2

I have a Java class with a property like this

private Object myObj;

When I try to save the class using Hibernate annotations, I get the rather confusing error message "property mapping has wrong number of columns".

What is the correct approach to persisting a class containing a generic property such as this?

7
  • 1
    The only way I know of is serialization, and I don't recommend this. Everytime you compile your app, the seralized version of the object in the database could become obsolete and not retreivable. Commented Dec 21, 2009 at 16:42
  • one question is: what's forcing you to use Object as the type for this specific field? Couldn't you use a more specific type instead? Commented Dec 21, 2009 at 16:50
  • 2
    @afielden: How would you like this to work? What would be your ideal end result, in terms of the database ? Commented Dec 21, 2009 at 16:52
  • I agree, it's not ideal, and I thought Hibernate may have some problems with this. The code is not mine, I'm just implementing the persistence layer using Hibernate. The idea is that the Object property may contain a wide variety of data, including but not limited to image and video. I guess they want a very flexible solution. Commented Dec 21, 2009 at 17:01
  • 1
    Yes, but how would you like to see it represented in the database? One column or several? It's own table? What datatypes would the columns be? When solving ORM puzzles, you need to think like a relational database, not a java developer. Commented Dec 21, 2009 at 17:53

3 Answers 3

4

As you said, the object can be image, video, and more.

If it is supposed to have only binary data, you can create another class, and transfer the data from this one. In the process of transferring you can convert the Object to byte[] (depending on the concrete type), and use a Lob data type (@Lob) for mapping it.

To extend this, if not only binary data is to be supported, your new object can have 2 fields - one in case of binary, and one (or more) in case of other types.

Anyway, the object as it is now, represents a quite unintelligent design, and it cannot be persisted properly without hassle.

Sign up to request clarification or add additional context in comments.

Comments

0

Don't use a generic Object attribute if you can help it. My first brush with Hibernate meant inheriting a project with oodles of things like this, and it was a world of pain. As Zoidberg says, using Object means that your model isn't easily serialised / versioned (an understatement if I'm honest).

Comments

0

Change the type of your property:

private byte[] myObj;

This worked for me with Fluent NHibernate (albeit in C#) and an SQL field type of varbinary(max).

You can then expose this as an image, video, or whatever. An example of converting to an image in Java:

ImageIO.read(new ByteArrayInputStream(myObj));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.