Hibernate Learning Notes II: Mapping Method of Table and Entity Classes

Posted by phpdev12 on Wed, 22 May 2019 00:52:07 +0200

Hibernate Learning Notes II: Mapping of Tables and Entity Classes

In the use of Hibernate, we had better try to make the entity class name and table name, entity class attribute name and table column name as same as possible, which can save a lot of trouble. However, in our actual project, we may encounter inconsistencies between the names of entity classes and tables.

What should we do when this happens?

If we use Annotation
It's easier to do that.

Look at the actual examples, as follows:

package com.hibernate.model;

    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.persistence.Table;

    @Entity
    @Table(name="_Teacher")  //When table names are inconsistent, you can specify
    public class Teacher {
    private int id;
    private String name;
    private String title;
    @Id
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    @Column(name="_name")  //When the attribute name of an entity class is inconsistent with the column name in the table, it can be specified as (_name) of the column name of the table. 
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

That is, when the name of the entity class is inconsistent with the name of the table, it can be specified directly with @table(name="tableName").

If the attribute name of the entity class is inconsistent with the column name of the table, you can specify it directly with @Column(name="columnName").

If we use the. xml Mapping file, the method is as follows:

<class name="Student" table="_Student"> <! -- If we don't write table, the default table name is the same as the class name - >"
    <! - Primary key - >
    <id name="id" column="id">
        <! - Generation Strategy of Primary Key
        <generator class="native"/>
    </id>
    <! -- Other attributes, name corresponds to the attributes of entity classes, column corresponds to the columns of relational database tables - >
    <property name="name"  column="_name"/>
    <property name="age"/>
</class>

These are two ways to solve the inconsistency between the names of entity classes and the names in tables.

What if we don't want to persist an attribute in an entity class?  
The solution is to add @transient s directly to the get method of the attribute in Annotation. Just don't write it directly in. xml file

For example, the following example is to not persist title in the Teacher class. Note: The annotation @transient added to the title's getTitle method

package com.hibernate.model;

    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.persistence.Table;
    import javax.persistence.Transient;
    @Entity
    @Table(name="_Teacher")  //When table names are inconsistent, you can specify
    public class Teacher {
    private int id;
    private String name;
    private String title;
    @Id
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    @Column(name="_name")  //When the attribute name of an entity class is inconsistent with the column name in the table, it can be specified as (_name) of the column name of the table. 
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Transient    //No persistence 
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }

} from: http://blog.csdn.net/u010412719/article/details/51264341

Topics: Attribute Hibernate xml Database