Before we read with a few minor problems to read the content of the following text, the paper Hibernate
framework for understanding ORM
implement the principles framework map.
ORM
Mapping frameworkentity
(entity classes) name and the table name is inconsistent with how the corresponding database table names?entity
(Entity classes) in the property takes驼峰命名
to:userName
, and database tableuser_Name
is how the corresponding?- Why
Hibernate
byentity
(entity classes) will be able to get a completeSQL
sentence?
Here we have completed a ORM
framework entity
(entity classes) is inconsistent with the table field, was able to generate a complete SQL statement of the case.
In use Hibernate
the frame, we will create an entity class, create database tables and fields to the corresponding fields will be used to the core of the two notes, @Table
and @Column
other @Entity
, @Id
and other notes we first ignore, focusing on the principle.
@Table Notes
The notes make when using the entity class name corresponds to the table, @Table("user_table")
this is equivalent to comment fact played the role of an alias, we can try to customize a comment imitate restore the annotations.
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyTable {
String value();
}
About custom annotations , I do not do too much explanation.
@Column Annotations
The annotation for the entity class field, the field corresponding to the alias, and so the database table columns ( Column
ie: column) corresponds. We went to two and a custom @Column
annotation considerable comment.
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyColumn {
String columnName();
int columnLength();
String columnType();
}
Entity class
@MyTable("user_table")
public class User {
@MyColumn(columnName="user_name",columnLength=5,columnType="String")
private String userName;
@MyColumn(columnName="user_age",columnLength=5,columnType="int")
private int userAge;
@MyColumn(columnName="user_id",columnLength=5,columnType="int")
private int userId;
@MyColumn(columnName="user_gender",columnLength=1,columnType="int")
private int gender;
// 省略get/set/构造/toString等方法
}
Now we have completed the two core annotated entity classes and also the corresponding database table, then the next step is to generate entity classes SQL
.
SQL generation
want to generate a complete SQL, you need to get the table names, field names. In just two notes and custom database tables that we have done a mapped. So how do we get it? We can 反射
!
import java.lang.reflect.Field;
public class MyOrmTest {
public static void main(String[] args) throws ClassNotFoundException {
System.out.println(selectUserTable());
}
public static String selectUserTable() throws ClassNotFoundException {
// 通过反射获取到Class
Class<?> classForName = Class.forName("orm.test.User");
// 获取注解@MyTable
MyTable table = classForName.getAnnotation(MyTable.class);
// 获取到类的所有字段
Field[] Fields = classForName.getDeclaredFields();
// 拼接SQL
StringBuffer sb = new StringBuffer();
sb.append(" select ");
// 获取注解@MyTable的值
String tableName = table.value();
// 迭代类中所有字段,获取字段上注解@MyColumn的值并拼接SQL
for (int i = 0; i < Fields.length; i++) {
Field field = Fields[i];
MyColumn column = field.getAnnotation(MyColumn.class);
sb.append(" " + column.columnName() + " ");
if (i == Fields.length - 1) {
sb.append(" from ");
} else {
sb.append(",");
}
}
sb.append(" " + tableName);
return sb.toString();
}
}
ORM
Many details, such as the field type of treatment and so on, not to understand, and we understand ORM
and core principles can be.