Skip to content

Commit faff522

Browse files
committed
add comments
1 parent d0af892 commit faff522

12 files changed

Lines changed: 139 additions & 114 deletions

File tree

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
/**
22
* 数据库数据模型包
3+
* Database data models
34
*/
45
package openthinks.libs.sql.data;
56

67
import openthinks.libs.sql.dhibernate.support.ColumnAttribute;
78

89
/**
910
* 数据表的一行实体记录接口
11+
* Represent the structure of row in database table
1012
* @author dmj
1113
*
1214
*/
@@ -17,43 +19,43 @@ public interface Row {
1719
* @return Object 该列的值
1820
*/
1921
public Object get(int index);
20-
22+
2123
/**
2224
* 设置index列的值
2325
* @param index 第index列
2426
* @param e 设置新列的值
2527
*/
26-
public void set(int index,Object e);
27-
28+
public void set(int index, Object e);
29+
2830
/**
2931
* 返回该行的列数
3032
* @return int 列数
3133
*/
3234
public int size();
33-
35+
3436
/**
3537
* 返回所有列的名称
3638
* @return ColumnAttribute[] 列名称字符串数组
3739
*/
3840
public ColumnAttribute[] getColumnAttributes();
39-
41+
4042
/**
4143
* 返回该行的所有列的集合
4244
* @return Column[]
4345
*/
4446
public Column[] getColumns();
45-
47+
4648
/**
4749
* 根据列名称返回该列的值
4850
* @param columnName 列名称
4951
* @return Object 该列的值
5052
*/
5153
public Object get(String columnName);
52-
54+
5355
/**
5456
* 设置列名称为columnName的列值为e
5557
* @param columnName 列名称
5658
* @param e
5759
*/
58-
public void set(String columnName,Object e);
60+
public void set(String columnName, Object e);
5961
}

src/openthinks/libs/sql/dhibernate/support/ColumnAttribute.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@
2525
*/
2626
package openthinks.libs.sql.dhibernate.support;
2727

28-
28+
/**
29+
* The information which holds the attribute name and column name in entity and table
30+
* @author dailey
31+
*
32+
*/
2933
public class ColumnAttribute {
3034
private String columnName;
3135
private String attributeName;
@@ -105,10 +109,8 @@ public void setAttributeName(String attributeName) {
105109
public int hashCode() {
106110
final int prime = 31;
107111
int result = 1;
108-
result = prime * result
109-
+ ((attributeName == null) ? 0 : attributeName.hashCode());
110-
result = prime * result
111-
+ ((columnName == null) ? 0 : columnName.hashCode());
112+
result = prime * result + ((attributeName == null) ? 0 : attributeName.hashCode());
113+
result = prime * result + ((columnName == null) ? 0 : columnName.hashCode());
112114
return result;
113115
}
114116

src/openthinks/libs/sql/dhibernate/support/IDType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@
2626
package openthinks.libs.sql.dhibernate.support;
2727

2828
/**
29+
* The primary key type for database table
2930
* @author dailey
3031
*
3132
*/
3233
public enum IDType {
33-
AUTO,MANUAL
34+
AUTO, MANUAL
3435
}

src/openthinks/libs/sql/entity/Entity.java

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

1717
/**
1818
* 对应数据库表的实体类
19-
*
19+
* The simple entity which reference the table in database;<BR>
20+
* required:<BR>
21+
* <li>the attribute(property) name in this entity need same as the column name in database table
22+
* <li>the first attribute in this entity map to the primary key column in database table
2023
* @author dmj
2124
*/
2225
public abstract class Entity extends AbstractRow {
@@ -41,13 +44,11 @@ public ColumnAttribute[] getColumnAttributes() {
4144
propertyNames = new String[fields.length];
4245
for (int i = 0; i < propertyNames.length; i++) {
4346
propertyNames[i] = fields[i].getName();
44-
ColumnAttribute columnAttribute = new ColumnAttribute(
45-
propertyNames[i], propertyNames[i]);
47+
ColumnAttribute columnAttribute = new ColumnAttribute(propertyNames[i], propertyNames[i]);
4648
if (i == 0) {
4749
columnAttribute.setIdType(IDType.MANUAL);
4850
Class<?> type = fields[i].getType();
49-
if (type == int.class || type == long.class
50-
|| type == Integer.class || type == Long.class) {
51+
if (type == int.class || type == long.class || type == Integer.class || type == Long.class) {
5152
columnAttribute.setIdType(IDType.AUTO);
5253
}
5354
}
@@ -62,13 +63,10 @@ public ColumnAttribute[] getColumnAttributes() {
6263
return columnAttributeMapping.toArray();
6364
}
6465

65-
protected PropertyDescriptor getPropertyDescriptor(String propertyName)
66-
throws IntrospectionException {
67-
PropertyDescriptor propertyDescriptor = propertyDescMap
68-
.get(propertyName);
66+
protected PropertyDescriptor getPropertyDescriptor(String propertyName) throws IntrospectionException {
67+
PropertyDescriptor propertyDescriptor = propertyDescMap.get(propertyName);
6968
if (propertyDescriptor == null) {
70-
propertyDescriptor = new PropertyDescriptor(propertyName,
71-
getClass());
69+
propertyDescriptor = new PropertyDescriptor(propertyName, getClass());
7270
propertyDescMap.put(propertyName, propertyDescriptor);
7371
}
7472
return propertyDescriptor;
@@ -93,14 +91,13 @@ public Object get(String propertyName) {
9391
} catch (Exception e) {
9492
isThrow = true;
9593
}
96-
if (isThrow) {
94+
if (isThrow) {//get field value directly by access the filed
9795
try {
9896
Field filed = this.getClass().getDeclaredField(propertyName);
9997
filed.setAccessible(true);
10098
ret = filed.get(this);
10199
} catch (Exception e) {
102-
throw new EntityReflectException(propertyName + "属性",
103-
e.getCause());
100+
throw new EntityReflectException(propertyName + "属性", e.getCause());
104101
}
105102
}
106103
return ret;
@@ -123,8 +120,7 @@ public void set(String propertyName, Object e) {
123120
Method method = propertyDescriptor.getWriteMethod();
124121
Class<?>[] parameterTypes = method.getParameterTypes();
125122
if (parameterTypes != null && parameterTypes.length == 1) {
126-
method.invoke(this,
127-
Converter.source(e).convertToSingle(parameterTypes[0]));
123+
method.invoke(this, Converter.source(e).convertToSingle(parameterTypes[0]));
128124
} else {
129125
method.invoke(this, e);
130126
}
@@ -134,15 +130,13 @@ public void set(String propertyName, Object e) {
134130
// + "方法", ex.getCause());
135131
isThrow = true;
136132
}
137-
if (isThrow) {
133+
if (isThrow) {//set field value directly by access the filed
138134
try {
139135
Field filed = getClass().getDeclaredField(propertyName);
140136
filed.setAccessible(true);
141-
filed.set(this,
142-
Converter.source(e).convertToSingle(filed.getType()));
137+
filed.set(this, Converter.source(e).convertToSingle(filed.getType()));
143138
} catch (Exception ex) {
144-
throw new EntityReflectException(propertyName + "属性",
145-
ex.getCause());
139+
throw new EntityReflectException(propertyName + "属性", ex.getCause());
146140
}
147141
}
148142
}

src/openthinks/libs/sql/entity/jpa/EntityReflectHandler.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,15 @@
3333

3434
import org.apache.log4j.Logger;
3535

36+
/**
37+
* The simple {@link IReflectHandler} implementation for {@link Entity}
38+
* @author dailey
39+
*
40+
*/
3641
public class EntityReflectHandler extends JPAReflectHandler implements IReflectHandler {
3742

3843
Logger logger = Logger.getLogger(getClass());
3944

40-
/*
41-
* (non-Javadoc)
42-
*
43-
* @see sql.lang.reflect.IReflectHandler#handField(java.lang.Object,
44-
* java.lang.String, java.lang.Object)
45-
*/
4645
@Override
4746
public <T> boolean handColumnField(T entity, String columnName, Object columnValue) {
4847
// TODO Checker.require(entity).isExtendsFrom(Entity.class);
@@ -71,11 +70,6 @@ public <T> String getEntityTableName(Class<T> entityClazz) {
7170
return tableName;
7271
}
7372

74-
/*
75-
* (non-Javadoc)
76-
*
77-
* @see sql.entity.jpa.JPAReflectHandler#getEntityIDName(java.lang.Class)
78-
*/
7973
@Override
8074
public <T> String getEntityIDName(Class<T> entityClazz) {
8175
String idName = super.getEntityIDName(entityClazz);

src/openthinks/libs/sql/entity/jpa/IReflectHandler.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,40 +28,47 @@
2828
import openthinks.libs.sql.dhibernate.support.ColumnAttributeMapping;
2929

3030
/**
31+
* The interface to handle different entity object
3132
* @author dailey
3233
*
3334
*/
3435
public interface IReflectHandler {
35-
36+
3637
/**
3738
* parse entity class to {@link ColumnAttributeMapping}
3839
* @param entityClass Class<T><BR>
3940
* 1. JPA annotation<BR>
4041
* 2. Subclass of {@link Entity}
41-
* @return
42+
* @return {@link ColumnAttributeMapping}
4243
*/
43-
public <T> ColumnAttributeMapping parseEntityClass(Class<T> entityClass);
44+
public <T> ColumnAttributeMapping parseEntityClass(Class<T> entityClass);
4445

4546
/**
4647
* set column value to corresponding entity object
4748
* @param entity entity instance
49+
* 1. JPA annotation<BR>
50+
* 2. Subclass of {@link Entity}
4851
* @param columnName column name in database table
4952
* @param columnValue column name value in database table row
50-
* @return boolean
53+
* @return boolean handle success or not
5154
*/
5255
public <T> boolean handColumnField(T entity, String columnName, Object columnValue);
5356

5457
/**
5558
* get entity corresponding table name
5659
* @param entityClazz Class<T>
57-
* @return String
60+
* 1. JPA annotation<BR>
61+
* 2. Subclass of {@link Entity}
62+
* @return String table name
5863
*/
5964
public <T> String getEntityTableName(Class<T> entityClazz);
6065

6166
/**
6267
* get entity corresponding table primary key
6368
* @param entityClazz Class<T>
64-
* @return String
69+
* 1. JPA annotation<BR>
70+
* 2. Subclass of {@link Entity}
71+
* @return String primary key name
6572
*/
6673
public <T> String getEntityIDName(Class<T> entityClazz);
6774

0 commit comments

Comments
 (0)