use of javax.persistence.ManyToOne in project eweb4j-framework by laiweiwei.
the class PojoAnnotationConfig method getProperties.
private static List<Property> getProperties(Class<?> clazz, final List<Property> pList, final boolean requireSuper, Log log) throws Throwable {
List<Property> result = new ArrayList<Property>();
ReflectUtil ru;
try {
ru = new ReflectUtil(clazz);
ru.setRequiredSuper(requireSuper);
} catch (Throwable e) {
log.warn(e.toString(), e);
throw e;
}
for (Field f : ru.getFields()) {
if (Collection.class.isAssignableFrom(f.getType()))
continue;
String name = f.getName();
Method getter = ru.getGetter(name);
if (getter == null)
continue;
Ignore igAnn = f.getAnnotation(Ignore.class);
if (igAnn == null)
igAnn = getter.getAnnotation(Ignore.class);
if (igAnn != null)
continue;
Transient trans = f.getAnnotation(Transient.class);
if (trans == null)
trans = getter.getAnnotation(Transient.class);
if (trans != null)
continue;
OneToMany manyAnn = getter.getAnnotation(OneToMany.class);
if (manyAnn != null)
continue;
else {
manyAnn = f.getAnnotation(OneToMany.class);
if (manyAnn != null)
continue;
}
ManyToMany manyManyAnn = getter.getAnnotation(ManyToMany.class);
if (manyManyAnn != null)
continue;
else {
manyManyAnn = f.getAnnotation(ManyToMany.class);
if (manyManyAnn != null)
continue;
}
Property p = new Property();
if (Long.class.isAssignableFrom(f.getType()) || long.class.isAssignableFrom(f.getType()))
p.setSize("20");
else if (Integer.class.isAssignableFrom(f.getType()) || int.class.isAssignableFrom(f.getType()))
p.setSize("8");
else if (String.class.isAssignableFrom(f.getType()))
p.setSize("255");
else if (Boolean.class.isAssignableFrom(f.getType()) || boolean.class.isAssignableFrom(f.getType()))
p.setSize("");
else if (Float.class.isAssignableFrom(f.getType()) || float.class.isAssignableFrom(f.getType()))
p.setSize("8");
Id idAnn = getter.getAnnotation(Id.class);
if (idAnn == null)
idAnn = f.getAnnotation(Id.class);
if (idAnn != null) {
if (pList != null && hasIdProperty(pList))
continue;
p.setAutoIncrement("1");
p.setPk("1");
p.setSize("20");
}
Column colAnn = getter.getAnnotation(Column.class);
if (colAnn == null) {
colAnn = f.getAnnotation(Column.class);
}
String column = colAnn == null ? "" : colAnn.name();
column = "".equals(column.trim()) ? name : column;
p.setName(name);
p.setColumn(column);
p.setType(f.getType().getName());
p.setNotNull("false");
if (colAnn != null) {
// int size = colAnn.length();
p.setNotNull(String.valueOf(!colAnn.nullable()));
p.setUnique(String.valueOf(colAnn.unique()));
}
if (ClassUtil.isPojo(f.getType())) {
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) {
if (oneAnn != null)
p.setType(PropType.ONE_ONE);
else
p.setType(PropType.MANY_ONE);
JoinColumn joinColumn = getter.getAnnotation(JoinColumn.class);
if (joinColumn == null)
joinColumn = f.getAnnotation(JoinColumn.class);
if (joinColumn != null && joinColumn.name().trim().length() > 0)
p.setColumn(joinColumn.name());
else
p.setColumn(f.getName() + "_id");
p.setRelProperty(null);
String refCol = null;
if (joinColumn != null && joinColumn.referencedColumnName().trim().length() > 0) {
refCol = joinColumn.referencedColumnName();
if (refCol != null && refCol.trim().length() > 0) {
String relField = ORMConfigBeanUtil.getField(f.getType(), refCol);
if (relField != null && relField.trim().length() > 0)
p.setRelProperty(relField);
}
}
p.setRelClass(f.getType());
p.setSize("20");
}
}
result.add(p);
}
return result;
}
use of javax.persistence.ManyToOne in project eweb4j-framework by laiweiwei.
the class UpdateSqlCreator method makeSQL.
private Sql makeSQL(T t, String[] fields) throws SqlCreateException {
Sql sql = new Sql();
Class<?> clazz = t.getClass();
//if fields is empty
if (fields == null || fields.length == 0) {
fields = ORMConfigBeanUtil.getFields(clazz);
}
String table = ORMConfigBeanUtil.getTable(clazz, false);
StringBuilder values = new StringBuilder();
ReflectUtil ru = new ReflectUtil(t);
String[] columns = ORMConfigBeanUtil.getColumns(clazz, fields);
String idColumn = ORMConfigBeanUtil.getIdColumn(clazz);
String idField = ORMConfigBeanUtil.getIdField(clazz);
Method idGetter = ru.getGetter(idField);
if (idGetter == null)
throw new SqlCreateException("can not find id getter.");
Object idValue = null;
try {
idValue = idGetter.invoke(t);
} catch (Exception e) {
throw new SqlCreateException(idGetter + " invoke exception " + e.toString(), e);
}
for (int i = 0; i < fields.length; i++) {
String field = fields[i];
String column = columns[i];
Method getter = ru.getGetter(field);
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(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 (values.length() > 0)
values.append(", ");
// values.append(column).append(" = '").append(value).append("'");
values.append(column).append(" = ? ");
sql.args.add(value);
} catch (Exception e) {
throw new SqlCreateException(idGetter + " invoke exception " + 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, values, condition);
return sql;
}
Aggregations