Hibernate - save return value of a method rather then the field

Jvck Ouk :

I want to save all String values of the list into the database as one String. If implemented as bellow it saves null value. Is there any way how to save a return value of method instead of the fiels itself?

@Entity
@Table(name = "commands")
public class Command {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name = "command")
private String command;

@Transient
private List<String> validResultList = new ArrayList<>();

public List<String> getValidResultList() {
    return validResultList;
}

public void setValidResultList(List<String> validResultList) {
    this.validResultList = validResultList;
}

@Column(name = "valid_commands")
public String getValidResultListAsString() {
    StringBuilder sb = new StringBuilder();
    for (String value : validResultList) {
        sb.append(value).append(" ");
    }
    return sb.toString();
}

}

The field "valid_commands" is VARCHAR2.

For example:

Command comm1 = new Command("echo test;");
comm1.setValidResultList(Arrays.asList("foo", "bar"));

And it should save as:

"foo bar "

Would be glad for any tips.

user3359139 :

Simply, define a filed named "validResultListAsString" and set its value at setValidResultList and return it by getValidResultListAsString. In addition, you shouldn't use filed and property annotations together.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=276761&siteId=1