我如何嵌入一个字符串数组到一个实体(JPA) [英] How do I embed a String Array into an Entity (JPA)

查看:260
本文介绍了我如何嵌入一个字符串数组到一个实体(JPA)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设计一个实体类有一个String []属性。该字符串数组总是有两个值,我不想休眠(或相当JPA)创建此额外的表,而是直接嵌入此两个字符串值到表中。
这是可能的,如果是这样如何?

I wanna design an Entity Class which has a String[] property. This String Array always has two values and I dont want Hibernate (or rather JPA) create an extra table for this but embed this two String values directly into the table. Is this possible and if so how?

推荐答案

如果有确切总是两个值,可以使用的getter / setter和实例变量玩。事实上,你可以选择是否映射实例变量或属性与 @Column

If there is always exactly two values, you can play with getter/setter and instance variable. You can indeed choose whether you map instance variable or property with @Column.

@Column
String s1;

@Column
String s2;

public String[] getProp()
{
  return new String[]{ s1, s2 };
}

public String setProp(String[] s )
{
   s1 = s[0];
   s2 = s[1];
}

否则看 @Embedded 实体。东西在精神​​

@Entity
public class MyEntity {

    @Embedded
    public StringTuple tuple;

}

public class StringTuple {
    public String s1;
    public String s2;
}

这篇关于我如何嵌入一个字符串数组到一个实体(JPA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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