0

I have a question about how to Hibernate save arraylist of string(or object). I read here, so they said Hibernate will try call to toString method of arraylist to persist it.

I wrote a small code for test that, but it's not working like what they said.

class CustomizeArrayList extends ArrayList<String> 
    private static final long serialVersionUID = 1L;

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        System.out.println("To String of customize arraylist");
        return super.toString();
    }
}

I'm using this type as so:

@Column("array")
private CustomizrArrayList array

It's not calling toString method, is it using reflection to persist or serialization rather or anything else?

Thanks in advance!

10
  • 3
    "I read somewhere" is never a good starting point. Please reference the exact article. Commented Apr 26, 2013 at 7:58
  • stackoverflow.com/a/16033833/1492882. Here is a reference link. Commented Apr 26, 2013 at 7:59
  • 3
    In that question, they talk about arrays, not lists. Hibernate knows how to handle lists. Commented Apr 26, 2013 at 8:03
  • ... and that's why it's always worth including the reference. Commented Apr 26, 2013 at 8:04
  • 1
    And how do you want to have it persisted in the database - one row per String of the List, or one column containing all strings concatenated by a separation character? Commented Apr 26, 2013 at 9:14

1 Answer 1

0

Hibernate knows many internal Java types and how to convert them. The List type is one of them. For all list types (linked, array based or your custom one), Hibernate will use the same list-based approach which basically tries to put all elements of the list into another table.

You don't want that. One way to achieve what you want is to use String[] (i.e. a basic array type instead of a collection) instead of your custom type but that would create new problems elsewhere.

The correct solution is to tell Hibernate that you need special handling of this type. Implement the interface UserType (docs). In the methods nullSafeGet() and nullSafeSet(), you can convert between the DB column (VARCHAR, I guess) and your CustomizeArrayList.

To register the type, you can use the "Type Registry" (see here) or the @Type annotation (blog post)

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

2 Comments

Thanks for reply, but if I save a String[] or ArrayList, Hibernate will append a strange string to my record in the table, it's hex string and similar to memory address of computer, it make me thing it call to toString method.
Note that Hibernate usually wraps your class in a proxy, so it might not call the toString() that you expect. Also, how do you plan to read the value from the database? There is no "fromString()" method...

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.