简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM