2

Let's assume I have the String property fullName but I want to represent it as two separate strings as shown in the example below.

Is it possible to set up Hibernate so that it stores the full name in a single column and uses the accessor methods (getFullName, setFullName) to do that ?

The problem is that I do not want to declare - just to make Hibernate happy - an unnecessary String field fullName which will not be used because that would decrease the cleanness of the code.

String lastName;
String firstName;
public String getFullName() {
     return firstName+" "+lastName;
}
public void setFullName(String n) {
     firstName=extractFirstName(n);
     lastName=extractLastName(n);
}
jhegedus
  • 20,244
  • 16
  • 99
  • 167

2 Answers2

1

You can use transient annotation to make Hibernate ignore methods as you like:

@Transient
public String getFullName() {
     return firstName+" "+lastName;
}

@Transient
public void setFullName(String n) {
     firstName=extractFirstName(n);
     lastName=extractLastName(n);
}
Ean V
  • 5,091
  • 5
  • 31
  • 39
1

Yes you could annotate the getFullname method to store it to a column in database.

Check this link:

Hibernate Annotations - Which is better, field or property access?

Community
  • 1
  • 1
Henrik
  • 1,797
  • 4
  • 22
  • 46