1

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?

2 Answers 2

4

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];
}

Otherwise look at @Embedded entity. Something in the spirit of

@Entity
public class MyEntity {

    @Embedded
    public StringTuple tuple;

}

public class StringTuple {
    public String s1;
    public String s2;
}
Sign up to request clarification or add additional context in comments.

Comments

1

If this array always has two elements, why not simply create a class to hold them and than map that class as Component?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.