user2660369
user2660369

Reputation: 255

Entity Class with an Id nested in a different Entity Class

I got a database table MyTable with those columns: int Id, String Type, String Value

and I got an entity Class with those fields

@Entity
@Table(name = "MyTable")
class MyClass {
    @Id
    int Id;
    String Type;
    String Value;
}

Now what I want are Classes that look like that:

@Entity
class TypeA {
    MyClass myClass;
    //some stuff
}

The Id of TypeA should be MyClass.Id. Can this be done with Hibernate? MyClass has to be a variable of TypeA, I explicitly do not want any inheritance.

Upvotes: 0

Views: 412

Answers (1)

The only option I can see and comes close to your requirement but with one caveat is:

Using annotations Embeddable on MyClass and Embedded on TypeA will solve your problem partially.

Partially because of the caveat, i.e., you should move id field from MyClass to the TypeA class. That is the close you can get without using inheritance, I think.

Would be interested to see if there really exists any solution that meets your requirement exactly.

Anyways, if inheritance is an option, that would allow you to have id in the super class and have this super class annotated as MappedSuperClass and let TypeA extend super class.

Upvotes: 1

Related Questions