I've got a question to see if there's a better way to design the following simple example (I've got more complex tables, but this is a good example):
I have a User database table with the following columns :
id
first_name
last_name
And what I want to have is a UserBase class, and a User class. The User class will have accessor methods that are used by the UI. Following is an example of the code :
Code:
@Entity
@Table(name = "USER")
public abstract class UserBase implements java.io.Serializable {
protected String firstName ;
protected String lastName;
// appropriate getFirstName and getLastName methods
}
public class User extends UserBase {
// Put any transient methods / variables here
public String getFullName() {
return firstName + ", " + lastName;
}
}
I have tried using @MappedSuperclass on my UserBase class, and then using @Entity on my User class, the problem is that then Hibernate expects me to have a database column associated with the fullName field on my User class.
I've also tried marking my getFullName() with @Transient, which seems to work fine, but my question is whether or not there's a better way to implement this pattern. I'd prefer not to have to mark all my methods in my User class with @Transient, it just doesn't seem like a clean design.
Any help / suggestions would be greatly appreciated.