Generating Custom Code for JPA Objects from a Database

I am asking for an easy way to add some custom code to a JPA object generated by Eclipse from a database.

Basically I want to add public String properties containing entity property names and use them when I need to specify "property name" as String and make sure there are no runtime access errors.

Something like that

@Entity
@Table(name="clients")
@NamedQuery(name="ClientModel.findAll", query="SELECT c FROM ClientModel c")
public class ClientModel implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="id_client")
    private long idClient;

    public String name;

    public ClienteModel() {
    }

    public long getIdClient() {
        return this.idClient;
    }

    public void setIdClient(long idClient) {
        this.idClient = idClient;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    //CUSTOM CODE
    public static final String idClientProperty = "idClient";
    public static final String nameProperty = "name";
}

      

So I could use a property name like

ClientModel.nameProperty

      

and be safe during compilation of its existence and in case of refactoring names after further entity education.

I am aware of the existence of Telosys Tools and co., But I was hoping there might be something simpler / faster (like a custom class provided as a plugin in generating WSDL_to_entity with JAXB)

Thank.

+3


source to share


1 answer


In the end I used Telosys Tools, even if I didn't want to add another tool to my project, How easy it is to set up, just read here https://sites.google.com/site/telosystools/getting-started/21-configure-a -project

In my particular case, I added this code to the "JPA_bean_with_links" template while creating the getters



#if ( $field.getter )    public static String ${field.getter}Property() {
        return "$field.name";
    }
#end

      

+3


source







All Articles