简体   繁体   中英

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.

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>

您可以通过创建自定义值类型来实现 ,但我个人更愿意更改您的设计并使用List。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM