1

Is there possibility to add new column in subclass of entity? Something like:

Base class:

@Entity
@Table(name = "users")
public class User {
    @Column
    private String login;
}

Subclass:

@Entity
@Table(name = "users")
public class UserWithField extends User {
    @Column
    private String field;
}

I don't want to change base class (User) at all. Would it work somehow?

3
  • It's not a good practice but you can add Commented Sep 3, 2016 at 8:04
  • So what would be a good practice when i want to add column to base class table without editing base class? Commented Sep 3, 2016 at 8:08
  • Is this solved your doubt Commented Sep 3, 2016 at 8:14

1 Answer 1

1

Just create new class for new entity. Generally,In hibernate inheritance is used when you have two or more related tables.

Sign up to request clarification or add additional context in comments.

5 Comments

That's the same solution, which I'm using right now. If no one will post better looking solution i will accept your answer as correct. Thanks
Ok. But I'm telling you not to extend User class, create UserWithField as separate class having all fields from User class and extra fields of UserWithField class
Yes - thats what i'm using right now. Just want to know is there any better solution :)
I don't think there is any alternative. Just wait for some time if any one has it.It will be helpful for me as well
@Abhijeet I have a base class A, Class B extends A and Class C extends B. Using discriminator, I'm able to add the properties and association of Class C in table A. Is it possible to add the props & associations in table B ?

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.