简体   繁体   中英

Creating and extending JPA entity classes at runtime from config file

I have 3 entity classes and more stuff. I want to create a tool in which the user can create new entity classes and/or add additional fields to those existing 3 entity classes including JPA annotations (eg. extend them). The tool should use some kind of textual format (like xml) and generate the classes from that config file.

Is JAXB the right tool for this?

Since I'm completely new to this could someone point me to a tutorial for this? Note that I do not want to serialize objects but create a new class from an xml file.

If your tool can use xml then JAXB is a good choice to generate your java classes. JAXB can convert an xml to schema and then generate classes. Here is a useful article to understand how JAXB works and that may help you to understand how it can solve your problem:

http://www.javaworld.com/javaworld/jw-06-2006/jw-0626-jaxb.html

Note: I am the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

EclipseLink JPA offers extensible entities can be added via its XML mapping file. The virtual properties are accessed via generic get/set methods that take a property name.

@Entity
@VirtualAccessMethods
public class Customer{

    // Real Properties

    ...

    // Code for Virtual Properties

    @Transient
    private Map<String, Object> extensions;

    public <T> T get(String name) {
        return (T) extentions.get(name);
    }

    public Object set(String name, Object value) {
        return extensions.put(name, value);
    }

}

For More Information

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