0

I have a Java class that is mapped to a database table using JPA. Inside this class I have a List<Code> that I need to store in database.

So the Code class is not mapped into Hibernate. Is there a way to Serialize the List<Code> without mapping the Code class into Hibernate? Thanks in advance.

Error Message:

org.hibernate.exception.SQLGrammarException: could not insert collection [com.app.Account.codes#2]

Problem is that I'm getting error when Hibernate attempts to Serialize my List.

@Entity
@Table (name="account", catalog="database1")
public class Account{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column (name = "id")
    private String id

    @Column (name = "type")
    private String type;

    @CollectionOfElements
    @Column (name = "codes")
    List<Code> codes;
    ...
    ...
}


public class Code implements Serializable{
//This is a basic POJO that is not mapped into Hibernate. I just want this object
//to be stored in database.
}
0

1 Answer 1

3

You need to annotate the codes field with @Lob, not with @CollectionOfElements.

See http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#d0e6099 and the following paragraph.

This is a very fragile way of persisting a list of objects, though, because it uses binary serialization, which might quickly become non-backward compatible if the list or the Code class changes. It's also impossible to query or inspect using database tools.

I would rather map the Code class as an entity or as an embeddable.

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

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.