hypery2k
hypery2k

Reputation: 1691

Adding dynamic Attributes to JPA entities

I'm building a web app with a common base of entities for all customer

Now some customers needs additional fields, but I don't want to add all. I thought about something like that:

@Embeddable
public class AdditionalDetails {

private String label;

private String text;

public AdditionalDetails() {}


    // autogenerated getters and setters, hashCode(), equals()
} 
    
@Entity
public class BusinessObject {

    @Id
    @Column(name = "string_id")
    private long id;

    @ElementCollection(fetch=FetchType.EAGER)
    @MapKey(name = "language")
    @CollectionTable(schema = "label", name = "multilingual_string_map", 
              joinColumns = @JoinColumn(name = "string_id"))
    private Map<String, AdditionalDetails> map = new HashMap<String, AdditionalDetails>();

    public BusinessObject() {}

    // autogenerated getters and setters, hashCode(), equals()
}

No the problem is who I would I configure which properties are added to by BusinessObject?

Upvotes: 0

Views: 2165

Answers (1)

cmd
cmd

Reputation: 11841

You can remove the @Entity annotation from the base class. Then create subclasses from your base class and add the @Entity annotation to the subclasses. Now you can avoid placing all attributes in the base class, instead you can add them to an appropriate subclass.

Upvotes: 1

Related Questions