Hibernate 是一个开源的 Java 对象关系映射(ORM,Object-Relational Mapping)框架,主要用于简化 Java 应用程序与关系型数据库之间的交互
它通过将 Java 对象映射到数据库表,使开发者能够以面向对象的方式操作数据库,而无需直接编写复杂的 SQL 语句
Hibernate1 依旧是利用 TemplatesImpl 这个类,找寻
_outputProperties的 getter 方法的调用链
JAVA环境
java version "1.7.0_80"
Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)
依赖版本
- Hibernate Core 依赖版本:3-5
检查依赖配置
确认项目中是否正确引入了
- Hibernate Core
的依赖。如果使用的是 Maven,可以在 pom.xml 文件中添加以下依赖:
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.3.10.Final</version>
</dependency>
资源下载
前置知识
BasicPropertyAccessor - 4.x版本使用
在 hibernate 中定义了一个接口 org.hibernate.property.PropertyAccessor,定义了获取一个类的属性值的相关策略
接口中有两个方法,分别是
getGetter()- 接收 Class 对象和属性名,返回
org.hibernate.property.Getter对象
- 接收 Class 对象和属性名,返回
getSetter()- 接收 Class 对象和属性名,返回
org.hibernate.property.Setter对象
- 接收 Class 对象和属性名,返回
/**
* Create a "getter" for the named attribute
*
* @param theClass The class on which the property is defined.
* @param propertyName The name of the property.
*
* @return An appropriate getter.
*
* @throws PropertyNotFoundException Indicates a problem interpretting the propertyName
*/
public Getter getGetter(Class theClass, String propertyName) throws PropertyNotFoundException;
/**
* Create a "setter" for the named attribute
*
* @param theClass The class on which the property is defined.
* @param propertyName The name of the property.
*
* @return An appropriate setter
*
* @throws PropertyNotFoundException Indicates a problem interpretting the propertyName
*/
public Setter getSetter(Class theClass, String propertyName) throws PropertyNotFoundException;
org.hibernate.property.BasicPropertyAccessor 是对 PropertyAccessor 的标准实现
在这个类中,首先定义了 BasicGetter 和 BasicSetter 两个实现类
BasicPropertyAccessor$BasicGetter
重点关注 BasicGetter 类
BasicGetter 类实例化时接收 3 个参数,分别是 Class 对象,Method 方法和属性名 propertyName
private BasicGetter(Class clazz, Method method, String propertyName) {
this.clazz=clazz;
this.method=method;
this.propertyName=propertyName;
}
BasicGetter 的 get 方法接收一个对象实例,并调用 method.invoke() 方法反射调用这个 Method 方法
public Object get(Object target) throws HibernateException {
try {
return method.invoke( target, (Object[]) null );
}
...
}
1. BasicPropertyAccessor#getGetter
BasicPropertyAccessor类的 getGetter 方法调用 createGetter 方法
public Getter getGetter(Class theClass, String propertyName) throws PropertyNotFoundException {
return createGetter(theClass, propertyName);
}
2. BasicPropertyAccessor#createGetter
createGetter 方法调用 getGetterOrNull 方法
public static Getter createGetter(Class theClass, String pr

:Hibernate1&spm=1001.2101.3001.5002&articleId=147131293&d=1&t=3&u=7cc3f7dad7664e7eb742ce7622c8f2f8)
1149

被折叠的 条评论
为什么被折叠?



