use of javax.persistence.JoinColumn in project eweb4j-framework by laiweiwei.
the class InsertSqlCreator method createByFieldsIsValues.
public Sql[] createByFieldsIsValues(String[][] fields, Object[][] values) throws SqlCreateException {
Sql[] sqls = new Sql[ts.length];
for (int i = 0; i < ts.length; ++i) {
T t = ts[i];
ReflectUtil ru = new ReflectUtil(t);
Class<?> clazz = t.getClass();
String table = ORMConfigBeanUtil.getTable(clazz, false);
StringBuilder columnSB = new StringBuilder();
StringBuilder valueSB = new StringBuilder();
sqls[i] = new Sql();
for (int j = 0; j < fields[i].length; ++j) {
String name = fields[i][j];
Object _value = null;
Object value = null;
if (values == null) {
Method getter = null;
try {
getter = ru.getGetter(name);
if (getter == null)
continue;
_value = getter.invoke(t);
if (_value == null)
continue;
if (ClassUtil.isPojo(_value.getClass())) {
Field f = ru.getField(fields[i][j]);
OneToOne oneAnn = getter.getAnnotation(OneToOne.class);
if (oneAnn == null)
oneAnn = f.getAnnotation(OneToOne.class);
ManyToOne manyToOneAnn = null;
if (oneAnn == null) {
manyToOneAnn = getter.getAnnotation(ManyToOne.class);
if (manyToOneAnn == null)
manyToOneAnn = f.getAnnotation(ManyToOne.class);
}
if (oneAnn != null || manyToOneAnn != null) {
JoinColumn joinColAnn = getter.getAnnotation(JoinColumn.class);
if (joinColAnn == null)
joinColAnn = f.getAnnotation(JoinColumn.class);
if (joinColAnn != null && joinColAnn.referencedColumnName().trim().length() > 0) {
String refCol = joinColAnn.referencedColumnName();
String refField = ORMConfigBeanUtil.getField(_value.getClass(), refCol);
ReflectUtil tarRu = new ReflectUtil(_value);
Method tarFKGetter = tarRu.getGetter(refField);
value = tarFKGetter.invoke(_value);
} else {
ReflectUtil tarRu = new ReflectUtil(_value);
String tarFKField = ORMConfigBeanUtil.getIdField(_value.getClass());
if (tarFKField != null) {
Method tarFKGetter = tarRu.getGetter(tarFKField);
value = tarFKGetter.invoke(_value);
}
}
}
if (value == null)
continue;
} else {
value = _value;
}
} catch (Exception e) {
throw new SqlCreateException(getter + " invoke exception " + e.toString(), e);
}
} else {
value = values[i][j];
}
String column = ORMConfigBeanUtil.getColumn(clazz, name);
if (valueSB.length() > 0) {
columnSB.append(",");
valueSB.append(",");
}
columnSB.append(column);
// valueSB.append("'").append(value).append("'");
valueSB.append("?");
sqls[i].args.add(value);
}
sqls[i].sql = String.format("INSERT INTO %s(%s) VALUES(%s) ;", table, columnSB.toString(), valueSB.toString());
}
return sqls;
}
use of javax.persistence.JoinColumn in project eweb4j-framework by laiweiwei.
the class Model2Table method generateOne.
public static String generateOne(final Class<?> entityClass, final boolean isDrop, boolean isCreateFile) {
StringBuilder sql = new StringBuilder();
StringBuilder manyMany = new StringBuilder();
final ORMConfigBean ocb = ORMConfigBeanCache.get(entityClass.getName());
final String table = ocb.getTable();
List<Property> properties = ocb.getProperty();
StringBuilder sb = new StringBuilder();
StringBuilder fkSb = new StringBuilder();
final String fk = ", \n\tKEY %s (%s), \n\tCONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)";
StringBuilder pkSb = new StringBuilder();
final String pk = ", \n\tPRIMARY KEY (%s)";
StringBuilder uniqueSb = new StringBuilder();
String unique = ", \n\tUNIQUE KEY %s (%s)";
final String idCol = ORMConfigBeanUtil.getIdColumn(properties);
for (Property p : properties) {
if (sb.length() > 0)
sb.append(", \n");
String col = p.getColumn();
String type = p.getType();
String size = null;
if (p.getSize().trim().length() > 0 && CommonUtil.isNumeric(p.getSize().trim()))
size = String.format(" (%s) ", p.getSize());
if ("1".equals(p.getUnique()) || "true".equalsIgnoreCase(p.getUnique()))
uniqueSb.append(String.format(unique, col, col));
String notNull = "";
if ("1".equals(p.getNotNull()) || "true".equalsIgnoreCase(p.getNotNull()))
notNull = " NOT NULL ";
if ("1".equals(p.getPk()) || "true".equalsIgnoreCase(p.getPk())) {
if (pkSb.length() > 0)
pkSb.append(",");
pkSb.append(col);
}
String auto = "";
if ("1".equals(p.getAutoIncrement()) || "true".equalsIgnoreCase(p.getAutoIncrement()))
auto = " AUTO_INCREMENT ";
if (PropType.ONE_ONE.equals(p.getType()) || PropType.MANY_ONE.equals(p.getType())) {
final String relTable = ORMConfigBeanUtil.getTable(p.getRelClass(), false);
if (p.getRelProperty() == null || p.getRelProperty().trim().length() == 0)
p.setRelProperty(ORMConfigBeanUtil.getIdField(p.getRelClass()));
final String relColumn = ORMConfigBeanUtil.getColumn(p.getRelClass(), p.getRelProperty());
fkSb.append(String.format(fk, col, col, table + "_" + col, col, relTable, relColumn));
}
sb.append("\t").append(col).append(" ").append(getType(type)).append(size == null ? "" : size).append(notNull).append(auto);
}
sb.append(uniqueSb.toString());
if (pkSb.length() > 0)
sb.append(String.format(pk, pkSb.toString()));
sb.append(fkSb.toString());
String _sql = String.format(create_table_script, table, sb.toString());
if (isDrop) {
_sql = String.format(drop_table_script, table) + _sql;
;
}
sql.append(_sql);
try {
ReflectUtil ru = new ReflectUtil(entityClass);
for (Field f : ru.getFields()) {
if (!ClassUtil.isListClass(f))
continue;
String name = f.getName();
Method getter = ru.getGetter(name);
if (getter == null)
continue;
ManyToMany mmAnn = getter.getAnnotation(ManyToMany.class);
if (mmAnn == null)
mmAnn = f.getAnnotation(ManyToMany.class);
if (mmAnn != null) {
JoinTable join = getter.getAnnotation(JoinTable.class);
if (join == null) {
join = f.getAnnotation(JoinTable.class);
if (join == null)
continue;
}
JoinColumn[] froms = join.joinColumns();
if (froms == null || froms.length == 0)
continue;
JoinColumn[] tos = join.inverseJoinColumns();
if (tos == null || tos.length == 0)
continue;
final String relTable = join.name();
final String relFrom = froms[0].name();
final String relTo = tos[0].name();
final Class<?> targetClass = ClassUtil.getGenericType(f);
StringBuilder manyManyField = new StringBuilder();
//handle the many to many
manyManyField.append("\tid ").append(getType("long")).append(" (20) NOT NULL AUTO_INCREMENT,");
manyManyField.append("\n\t").append(relFrom).append(" ").append(getType("long")).append(" (20) ,");
manyManyField.append("\n\t").append(relTo).append(" ").append(getType("long")).append(" (20) ,");
manyManyField.append("\n\t").append("PRIMARY KEY (id)");
String tarTable = ORMConfigBeanUtil.getTable(targetClass, false);
String tarIdCol = ORMConfigBeanUtil.getIdColumn(targetClass);
String fk1 = String.format(fk, relFrom, relFrom, relTable + "_" + relFrom, relFrom, table, idCol);
manyManyField.append(fk1);
String fk2 = String.format(fk, relTo, relTo, relTable + "_" + relTo, relTo, tarTable, tarIdCol);
manyManyField.append(fk2);
manyMany.append(String.format(create_table_script, relTable, relTable, manyManyField.toString()));
}
}
} catch (Exception e1) {
e1.printStackTrace();
}
sql.append(manyMany.toString());
String script = String.format("SET FOREIGN_KEY_CHECKS=0;\n%s", sql.toString());
if (!isCreateFile)
return script;
try {
ConfigBean cb = (ConfigBean) SingleBeanCache.get(ConfigBean.class.getName());
File file = new File(ConfigConstant.CONFIG_BASE_PATH() + cb.getOrm().getDdl().getDs() + "-create.sql");
FileWriter writer = new FileWriter(file);
writer.write(script);
writer.flush();
writer.close();
LogFactory.getORMLogger(Model2Table.class).debug("create models sql script file success -> " + file.getAbsoluteFile());
return script;
} catch (IOException e2) {
e2.printStackTrace();
return null;
}
}
use of javax.persistence.JoinColumn in project eweb4j-framework by laiweiwei.
the class Model2Table method generateAll.
public static String generateAll(final boolean isDrop, final boolean isCreateFile) {
StringBuilder sql = new StringBuilder();
StringBuilder manyMany = new StringBuilder();
for (Iterator<Entry<Object, ORMConfigBean>> it = ORMConfigBeanCache.entrySet().iterator(); it.hasNext(); ) {
final Entry<Object, ORMConfigBean> e = it.next();
final ORMConfigBean ocb = e.getValue();
final String table = ocb.getTable();
List<Property> properties = ocb.getProperty();
StringBuilder sb = new StringBuilder();
StringBuilder fkSb = new StringBuilder();
final String fk = ", \n\tKEY %s (%s), \n\tCONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)";
StringBuilder pkSb = new StringBuilder();
final String pk = ", \n\tPRIMARY KEY (%s)";
StringBuilder uniqueSb = new StringBuilder();
String unique = ", \n\tUNIQUE KEY %s (%s)";
final String idCol = ORMConfigBeanUtil.getIdColumn(properties);
for (Property p : properties) {
if (sb.length() > 0)
sb.append(", \n");
String col = p.getColumn();
String type = p.getType();
String size = null;
if (p.getSize().trim().length() > 0 && CommonUtil.isNumeric(p.getSize().trim()))
size = String.format(" (%s) ", p.getSize());
if ("1".equals(p.getUnique()) || "true".equalsIgnoreCase(p.getUnique()))
uniqueSb.append(String.format(unique, col, col));
String notNull = "";
if ("1".equals(p.getNotNull()) || "true".equalsIgnoreCase(p.getNotNull()))
notNull = " NOT NULL ";
if ("1".equals(p.getPk()) || "true".equalsIgnoreCase(p.getPk())) {
if (pkSb.length() > 0)
pkSb.append(",");
pkSb.append(col);
}
String auto = "";
if ("1".equals(p.getAutoIncrement()) || "true".equalsIgnoreCase(p.getAutoIncrement()))
auto = " AUTO_INCREMENT ";
if (PropType.ONE_ONE.equals(p.getType()) || PropType.MANY_ONE.equals(p.getType())) {
final String relTable = ORMConfigBeanUtil.getTable(p.getRelClass(), false);
if (p.getRelProperty() == null || p.getRelProperty().trim().length() == 0)
p.setRelProperty(ORMConfigBeanUtil.getIdField(p.getRelClass()));
final String relColumn = ORMConfigBeanUtil.getColumn(p.getRelClass(), p.getRelProperty());
fkSb.append(String.format(fk, col, col, table + "_" + col, col, relTable, relColumn));
}
sb.append("\t").append(col).append(" ").append(getType(type)).append(size == null ? "" : size).append(notNull).append(auto);
}
sb.append(uniqueSb.toString());
if (pkSb.length() > 0)
sb.append(String.format(pk, pkSb.toString()));
sb.append(fkSb.toString());
String _sql = String.format(create_table_script, table, sb.toString());
if (isDrop) {
_sql = String.format(drop_table_script, table) + _sql;
;
}
sql.append(_sql);
try {
Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass((String) e.getKey());
ReflectUtil ru = new ReflectUtil(clazz);
for (Field f : ru.getFields()) {
if (!ClassUtil.isListClass(f))
continue;
String name = f.getName();
Method getter = ru.getGetter(name);
if (getter == null)
continue;
ManyToMany mmAnn = getter.getAnnotation(ManyToMany.class);
if (mmAnn == null)
mmAnn = f.getAnnotation(ManyToMany.class);
if (mmAnn != null) {
JoinTable join = getter.getAnnotation(JoinTable.class);
if (join == null) {
join = f.getAnnotation(JoinTable.class);
if (join == null)
continue;
}
JoinColumn[] froms = join.joinColumns();
if (froms == null || froms.length == 0)
continue;
JoinColumn[] tos = join.inverseJoinColumns();
if (tos == null || tos.length == 0)
continue;
final String relTable = join.name();
final String relFrom = froms[0].name();
final String relTo = tos[0].name();
final Class<?> targetClass = ClassUtil.getGenericType(f);
StringBuilder manyManyField = new StringBuilder();
//handle the many to many
manyManyField.append("\tid ").append(getType("long")).append(" (20) NOT NULL AUTO_INCREMENT,");
manyManyField.append("\n\t").append(relFrom).append(" ").append(getType("long")).append(" (20) ,");
manyManyField.append("\n\t").append(relTo).append(" ").append(getType("long")).append(" (20) ,");
manyManyField.append("\n\t").append("PRIMARY KEY (id)");
String tarTable = ORMConfigBeanUtil.getTable(targetClass, false);
String tarIdCol = ORMConfigBeanUtil.getIdColumn(targetClass);
String fk1 = String.format(fk, relFrom, relFrom, relTable + "_" + relFrom, relFrom, table, idCol);
manyManyField.append(fk1);
String fk2 = String.format(fk, relTo, relTo, relTable + "_" + relTo, relTo, tarTable, tarIdCol);
manyManyField.append(fk2);
manyMany.append(String.format(create_table_script, relTable, relTable, manyManyField.toString()));
}
}
} catch (Exception e1) {
continue;
}
}
sql.append(manyMany.toString());
String script = String.format("SET FOREIGN_KEY_CHECKS=0;\n%s", sql.toString());
if (!isCreateFile)
return script;
try {
ConfigBean cb = (ConfigBean) SingleBeanCache.get(ConfigBean.class.getName());
File file = new File(ConfigConstant.CONFIG_BASE_PATH() + cb.getOrm().getDdl().getDs() + "-create.sql");
FileWriter writer = new FileWriter(file);
writer.write(script);
writer.flush();
writer.close();
LogFactory.getORMLogger(Model2Table.class).debug("create models sql script file success -> " + file.getAbsoluteFile());
return script;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
use of javax.persistence.JoinColumn in project eweb4j-framework by laiweiwei.
the class SelectSqlCreator method selectWhere.
/**
* 各种条件查询
*
* @param fields
* 构成条件的属性名数组(填写的应该是对象属性名,不是数据库字段名)
* @param values
* 构成条件的字段值
* @param likeType
* 匹配类型 -1左匹配 0全匹配 1右匹配
* @param isLike
* 是否模糊查询
* @param orderField
* 对哪个字段进行排序
* @param oType
* 升序还是降序
* @return 字符串
* @param currentPage
* 第几页
* @param numPerPage
* 每页显示多少条
* @return
* @throws SqlCreateException
*/
public String selectWhere(String[] fields, String[] values, int likeType, boolean isLike, boolean isNot, boolean isOR, String orderField, int oType, int currentPage, int numPerPage) throws SqlCreateException {
String oper = " = ";
String connector = " AND ";
String left = " '";
String right = "' ";
if (isNot)
oper = "<>";
if (isLike) {
oper = " LIKE ";
left = " '%";
right = "%' ";
if (isNot)
oper = " NOT LIKE ";
switch(likeType) {
case LikeType.LEFT_LIKE:
left = " '";
break;
case LikeType.RIGHT_LIKE:
right = "' ";
break;
case LikeType.ALL_LIKE:
}
}
if (isOR)
connector = " OR ";
String[] type = { oper, connector, left, right };
if (orderField == null) {
orderField = idColumn;
orderField = OrderColumnUtil.getOrderColumn(orderField, dbType);
} else
orderField = ORMConfigBeanUtil.getColumn(clazz, orderField);
StringBuilder condition = new StringBuilder();
String[] columns = ORMConfigBeanUtil.getColumns(clazz, fields);
for (int i = 0; i < columns.length; ++i) {
if (values != null)
if (condition.length() > 0)
condition.append(type[1]).append(columns[i]).append(" ").append(type[0]).append(type[2]).append(values[i]).append(type[3]);
else
condition.append(columns[i]).append(" ").append(type[0]).append(type[2]).append(values[i]).append(type[3]);
else {
ReflectUtil ru = new ReflectUtil(t);
Method getter = ru.getGetter(fields[i]);
if (getter == null)
continue;
try {
Object _value = getter.invoke(t);
if (_value == null)
continue;
Object value = null;
if (ClassUtil.isPojo(_value.getClass())) {
Field f = ru.getField(fields[i]);
OneToOne oneAnn = getter.getAnnotation(OneToOne.class);
if (oneAnn == null)
oneAnn = f.getAnnotation(OneToOne.class);
ManyToOne manyToOneAnn = null;
if (oneAnn == null) {
manyToOneAnn = getter.getAnnotation(ManyToOne.class);
if (manyToOneAnn == null)
manyToOneAnn = f.getAnnotation(ManyToOne.class);
}
if (oneAnn != null || manyToOneAnn != null) {
JoinColumn joinColAnn = getter.getAnnotation(JoinColumn.class);
if (joinColAnn == null)
joinColAnn = f.getAnnotation(JoinColumn.class);
if (joinColAnn != null && joinColAnn.referencedColumnName().trim().length() > 0) {
String refCol = joinColAnn.referencedColumnName();
String refField = ORMConfigBeanUtil.getField(_value.getClass(), refCol);
ReflectUtil tarRu = new ReflectUtil(_value);
Method tarFKGetter = tarRu.getGetter(refField);
value = tarFKGetter.invoke(_value);
} else {
ReflectUtil tarRu = new ReflectUtil(_value);
String tarFKField = ORMConfigBeanUtil.getIdField(_value.getClass());
if (tarFKField != null) {
Method tarFKGetter = tarRu.getGetter(tarFKField);
value = tarFKGetter.invoke(_value);
}
}
}
if (value == null)
continue;
} else
value = _value;
if (condition.length() > 0)
condition.append(type[1]).append(columns[i]).append(" ").append(type[0]).append(type[2]).append(value).append(type[3]);
else
condition.append(columns[i]).append(" ").append(type[0]).append(type[2]).append(value).append(type[3]);
} catch (Exception e) {
e.printStackTrace();
throw new SqlCreateException(getter + " invoke exception " + e.toString());
}
}
}
String orderType = OrderType.ASC_ORDER == oType ? "ASC" : "DESC";
if (currentPage > 0 && numPerPage > 0)
return divPage(currentPage, numPerPage, orderField, oType, condition.toString());
else {
return String.format("SELECT %s FROM %s WHERE %s ORDER BY %s %s ;", selectAllColumn, table, condition.toString(), orderField, orderType);
}
}
use of javax.persistence.JoinColumn in project eweb4j-framework by laiweiwei.
the class UpdateSqlCreator method makeSQL.
private Sql makeSQL(T t) throws SqlCreateException {
Sql sql = new Sql();
Class<?> clazz = t.getClass();
String table;
String[] columns;
String[] fields;
Object[] values = null;
String idColumn;
String idField;
Object idValue = null;
HashMap<String, Object> map = null;
if (Map.class.isAssignableFrom(clazz)) {
map = (HashMap) t;
table = (String) map.get("table");
idColumn = (String) map.get("idColumn");
idField = idColumn;
idValue = map.get("idValue");
columns = (String[]) map.get("columns");
fields = columns;
values = (Object[]) map.get("values");
} else {
table = ORMConfigBeanUtil.getTable(clazz, false);
columns = ORMConfigBeanUtil.getColumns(clazz);
fields = ORMConfigBeanUtil.getFields(clazz);
idColumn = ORMConfigBeanUtil.getIdColumn(clazz);
idField = ORMConfigBeanUtil.getIdField(clazz);
}
StringBuilder valuesSb = new StringBuilder();
ReflectUtil ru = new ReflectUtil(t);
try {
if (map == null) {
Method idGetter = ru.getGetter(idField);
if (idGetter == null)
throw new SqlCreateException("can not find id getter");
idValue = idGetter.invoke(t);
}
for (int i = 0; i < columns.length; i++) {
String column = columns[i];
String field = fields[i];
Object value = null;
// id 字段不允许
if (idColumn != null && idColumn.equalsIgnoreCase(column))
continue;
if (map != null && values != null) {
value = values[i];
} else {
Method getter = ru.getGetter(field);
if (getter == null)
continue;
Object _value = getter.invoke(t);
if (_value == null)
continue;
if (ClassUtil.isPojo(_value.getClass())) {
Field f = ru.getField(field);
OneToOne oneAnn = getter.getAnnotation(OneToOne.class);
if (oneAnn == null)
oneAnn = f.getAnnotation(OneToOne.class);
ManyToOne manyToOneAnn = null;
if (oneAnn == null) {
manyToOneAnn = getter.getAnnotation(ManyToOne.class);
if (manyToOneAnn == null)
manyToOneAnn = f.getAnnotation(ManyToOne.class);
}
if (oneAnn != null || manyToOneAnn != null) {
JoinColumn joinColAnn = getter.getAnnotation(JoinColumn.class);
if (joinColAnn == null)
joinColAnn = f.getAnnotation(JoinColumn.class);
if (joinColAnn != null && joinColAnn.referencedColumnName().trim().length() > 0) {
String refCol = joinColAnn.referencedColumnName();
String refField = ORMConfigBeanUtil.getField(_value.getClass(), refCol);
ReflectUtil tarRu = new ReflectUtil(_value);
Method tarFKGetter = tarRu.getGetter(refField);
value = tarFKGetter.invoke(_value);
} else {
ReflectUtil tarRu = new ReflectUtil(_value);
String tarFKField = ORMConfigBeanUtil.getIdField(_value.getClass());
if (tarFKField != null) {
Method tarFKGetter = tarRu.getGetter(tarFKField);
value = tarFKGetter.invoke(_value);
}
}
}
if (value == null)
continue;
} else
value = _value;
}
if (valuesSb.length() > 0)
valuesSb.append(",");
// valuesSb.append(column).append(" = '").append(value).append("'");
valuesSb.append(column).append(" = ? ");
sql.args.add(value);
}
} catch (Exception e) {
throw new SqlCreateException("" + e.toString(), e);
}
// String condition = new StringBuilder().append(idColumn).append(" = ").append("'").append(idValue).append("'").toString();
String condition = new StringBuilder().append(idColumn).append(" = ? ").toString();
sql.args.add(idValue);
sql.sql = String.format("UPDATE %s SET %s WHERE %s ;", table, valuesSb, condition);
return sql;
}
Aggregations