使用 Hibernate 映射数组 [英] Mapping array with Hibernate

查看:30
本文介绍了使用 Hibernate 映射数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能帮我用 Hibernate 映射这个类吗?

Can you please help me to map this class using Hibernate?

public class MyClass{
    private Long id;
    private String name;
    private int[] values;
    ...
}

我使用的是PostgreSQL,表中的列类型是整数[]我的数组应该如何映射?

I'm using PostgreSQL and the column type in the table is integer[] How my array should be mapped?

推荐答案

我从未将数组映射到休眠状态.我总是使用集合.所以,我稍微改变了你的班级:

I have never mapped arrays to hibernate. I always use collections. So, I have slightly changed you class:

public class MyClass{
    private Long id;
    private String name;
    private List<Integer> values;

    @Id
    // this is only if your id is really auto generated
    @GeneratedValue(strategy=GenerationType.AUTO) 
    public Long getId() {
        return id;
    }

    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
    public List<Integer> getValues() {
        return values;
    }   
    ...

这篇关于使用 Hibernate 映射数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆