Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map a String[] in hibernate

How would you map the following class in hibernate:

private class Book {
    private int id;
    private String title;
    private String[] chapterTitles;

    //Constructor, Getters and Setters
}

I have mapped Collections and primitive arrays in Hibernate, but how do I do it with a String[]? My hibernate tools stops with a 'NullPointerException' thereby I am unable to generate the mappings. And I have googled but couldn't find any.

like image 870
Ragunath Jawahar Avatar asked Sep 26 '11 12:09

Ragunath Jawahar


2 Answers

I have no clue how to do it with Annotations and personally, I don't think it's good idea and you should use List<String> however you can do it using xml mapping.

You should use <array>

<array name="chapterTytles" table="Titles">
    <key column="title_ID" />
    <index column="tytle_index" />
    <element column="tytle_name" type="string" />
</array>
like image 88
danny.lesnik Avatar answered Sep 20 '22 19:09

danny.lesnik


You can do it by creating a custom value type, although I would personally prefer to change your design and use a List instead.

like image 43
Sean Patrick Floyd Avatar answered Sep 22 '22 19:09

Sean Patrick Floyd