简体   繁体   中英

Automapping DB Tables to Hibernate classes

I have my friend's EJB project that uses hibernate. I am doing my Java project with help of that project. Mine is not an EJB, it is an desktop applicaiton. I have a small hibernate class mapping issue, that is in my friend's project, when I add a new field (say varchar) to database, I can use that field in relevant java class. For example.

In database I add, telephone_no to table A. I can use it like this

class A {
    .......
    private String telephoneNo;

    public String getTelephoneNo(){
          return this.telephoneNo;
    }


    public void setTelephoneNo(String telephoneNo){
          this.telephoneNo = telephoneNo;
    }
    ........
}

but my project, I have to use annotations to set the column name.

The puzzle is, how it resolve the variable name ?. I know in hibernate if the database field and the class variable name is same, then it will automatically maps, if not we should annotations or use xml to map, but in this case (my friend's project) how it automatically resolve ?

Also in my friend's EJB project, I don't see any class mapping at all, when we add table, then it automatically maps to relevant class (I mean after adding class to the EJB).

But how can I achieve this in my Java Desktop application ?

you ask a lot of question here: 1. you can define to hibernate where to search for entities. you can define to look at the current jar additional jars ... (../MyApp.jar see configuration document ) , but classes that used as entities must to be declare either by annotation (@entity) or by xml mapping. 2. for the column name and table name . hibernate follow configuration by exception, so if you dont give a name to a table or field the column name or table name will be the same. you can override the default naming of hibernate (see property name="hibernate.naming_strategy" ).

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