How to embed String Array in Entity (JPA)

I want to create an Entity class that has the String [] property. This String array always has two values, and I don't want Hibernate (or rather JPA) to create an additional table for it, but insert these two String values ​​directly into the table. Is this possible, and if so, how?

+1
source share
2 answers

If there are always exactly two values, you can play with getter / setter and an instance variable. You can really choose whether you map a variable or instance 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;
}
+4
+1

All Articles