use of javax.persistence.ManyToOne in project jirm by agentgt.
the class SqlParameterDefinition method parameterDef.
static SqlParameterDefinition parameterDef(SqlObjectConfig config, Class<?> objectType, String parameterName, Class<?> parameterType, int order) {
final SqlParameterDefinition definition;
String sn = null;
ManyToOne manyToOne = getAnnotation(objectType, parameterName, ManyToOne.class);
if (manyToOne != null) {
Class<?> subK = checkNotNull(manyToOne.targetEntity(), "targetEntity not set");
JoinColumn joinColumn = getAnnotation(objectType, parameterName, JoinColumn.class);
SqlObjectDefinition<?> od = SqlObjectDefinition.fromClass(subK, config);
checkState(!od.getIdParameters().isEmpty(), "No id parameters");
if (joinColumn != null)
sn = joinColumn.name();
if (sn == null)
sn = config.getNamingStrategy().propertyToColumnName(parameterName);
FetchType fetch = manyToOne.fetch();
int depth;
if (FetchType.LAZY == fetch) {
depth = 1;
} else {
depth = config.getMaximumLoadDepth();
}
SqlParameterObjectDefinition sod = new SqlParameterObjectDefinition(od, depth);
definition = SqlParameterDefinition.newComplexInstance(config.getConverter(), parameterName, sod, order, sn);
} else {
Column col = getAnnotation(objectType, parameterName, Column.class);
if (col != null && !isNullOrEmpty(col.name()))
sn = col.name();
Id id = getAnnotation(objectType, parameterName, Id.class);
Version version = getAnnotation(objectType, parameterName, Version.class);
GeneratedValue generated = getAnnotation(objectType, parameterName, GeneratedValue.class);
Enumerated enumerated = getAnnotation(objectType, parameterName, Enumerated.class);
boolean idFlag = id != null;
boolean versionFlag = version != null;
boolean generatedFlag = generated != null;
if (sn == null)
sn = config.getNamingStrategy().propertyToColumnName(parameterName);
definition = SqlParameterDefinition.newSimpleInstance(config.getConverter(), parameterName, parameterType, order, sn, idFlag, versionFlag, generatedFlag, Optional.fromNullable(enumerated));
}
return definition;
}
use of javax.persistence.ManyToOne in project eweb4j-framework by laiweiwei.
the class OneToManyDAO method delete.
/**
*
* 一对多(主从)级联删除
* 1.前提条件必须主对象要存在于数据库中
* 2.检查当前主对象中的关联对象,如果关联对象为空,则删除所有与主对象有关的关联关系。
* 3.如果当前主对象中含有关联对象,则删除这些关联对象与主对象的关系
* 4.不会删除主对象
*/
public void delete() throws DAOException {
if (this.fields == null || this.fields.size() == 0)
return;
final Class<?> ownClass = ru.getObject().getClass();
Transaction.execute(new Trans() {
@Override
public void run(Object... args) throws Exception {
for (Field f : fields) {
Method tarGetter = ru.getGetter(f.getName());
if (tarGetter == null)
continue;
OneToMany ann = tarGetter.getAnnotation(OneToMany.class);
if (ann == null) {
ann = f.getAnnotation(OneToMany.class);
if (ann == null)
continue;
}
String mappedBy = ann.mappedBy();
Class<?> tarClass = ann.targetEntity();
if (void.class.isAssignableFrom(tarClass))
tarClass = ClassUtil.getGenericType(f);
List<?> tarList = null;
try {
tarList = (List<?>) tarGetter.invoke(t);
} catch (Exception e) {
throw new DAOException(tarGetter + " invoke exception ", e);
}
if (tarList == null || tarList.size() == 0) {
// 当关联对象为空的时候,删除所有关联对象
ReflectUtil tarRu = new ReflectUtil(tarClass);
if (mappedBy == null || mappedBy.trim().length() == 0) {
for (Field tarObjField : tarRu.getFields()) {
if (!tarObjField.getType().getName().equals(ownClass.getName()))
continue;
Method tarObjFieldGetter = tarRu.getGetter(tarObjField.getName());
if (tarObjFieldGetter == null)
continue;
ManyToOne manyToOne = tarObjField.getAnnotation(ManyToOne.class);
if (manyToOne == null)
manyToOne = tarObjFieldGetter.getAnnotation(ManyToOne.class);
if (manyToOne == null)
continue;
mappedBy = tarObjField.getName();
String fromRefCol = null;
JoinColumn joinCol = tarObjField.getAnnotation(JoinColumn.class);
if (joinCol == null)
joinCol = tarObjFieldGetter.getAnnotation(JoinColumn.class);
if (joinCol != null)
fromRefCol = joinCol.referencedColumnName();
if (fromRefCol == null || fromRefCol.trim().length() == 0)
fromRefCol = ORMConfigBeanUtil.getIdColumn(t);
String fromRefField = ORMConfigBeanUtil.getField(ownClass, fromRefCol);
Method fromRefFieldGetter = ru.getGetter(fromRefField);
if (fromRefFieldGetter == null)
throw new Exception("can not find the 'from ref field field -> " + fromRefField + "' of " + ownClass + " 's getter method");
String fromRefVal = null;
Object _obj = fromRefFieldGetter.invoke(t);
if (_obj != null)
fromRefVal = String.valueOf(_obj);
DAOFactory.getDeleteDAO(dsName).deleteByFieldIsValue(tarClass, new String[] { tarObjField.getName() }, new String[] { fromRefVal });
break;
}
}
} else {
// 当关联对象不为空的时候,删除这些关联对象
for (int i = 0; i < tarList.size(); i++) {
Object tarObj = tarList.get(i);
if (tarObj == null)
continue;
//如果这些对象没有ID值,跳过
Object tarObjIdVal = ORMConfigBeanUtil.getIdVal(tarObj);
if (tarObjIdVal == null)
continue;
ReflectUtil tarRu = new ReflectUtil(tarObj);
if (mappedBy != null && mappedBy.trim().length() > 0) {
Method ownFieldSetter = tarRu.getSetter(mappedBy);
if (ownFieldSetter == null)
continue;
// finished
DAOFactory.getDeleteDAO(dsName).deleteById(tarObj);
} else {
JoinTable joinTable = null;
if (f.isAnnotationPresent(JoinTable.class)) {
joinTable = f.getAnnotation(JoinTable.class);
} else if (tarGetter.isAnnotationPresent(JoinTable.class)) {
joinTable = tarGetter.getAnnotation(JoinTable.class);
} else {
// find ownclass in tarObj fields
for (Field tarObjField : tarRu.getFields()) {
if (!tarObjField.getType().getName().equals(ownClass.getName()))
continue;
Method tarObjFieldGetter = tarRu.getGetter(tarObjField.getName());
if (tarObjFieldGetter == null)
continue;
ManyToOne manyToOne = tarObjField.getAnnotation(ManyToOne.class);
if (manyToOne == null)
manyToOne = tarObjFieldGetter.getAnnotation(ManyToOne.class);
if (manyToOne == null)
continue;
String fromRefCol = null;
JoinColumn joinCol = tarObjField.getAnnotation(JoinColumn.class);
if (joinCol == null)
joinCol = tarObjFieldGetter.getAnnotation(JoinColumn.class);
if (joinCol != null)
fromRefCol = joinCol.referencedColumnName();
if (fromRefCol == null || fromRefCol.trim().length() == 0)
fromRefCol = ORMConfigBeanUtil.getIdColumn(t);
String fromRefField = ORMConfigBeanUtil.getField(ownClass, fromRefCol);
Method fromRefFieldGetter = ru.getGetter(fromRefField);
if (fromRefFieldGetter == null)
throw new Exception("can not find the 'from ref field field -> " + fromRefField + "' of " + ownClass + " 's getter method");
String fromRefVal = null;
Object _obj = fromRefFieldGetter.invoke(t);
if (_obj != null)
fromRefVal = String.valueOf(_obj);
DAOFactory.getDeleteDAO(dsName).deleteByFieldIsValue(tarClass, new String[] { tarObjField.getName() }, new String[] { fromRefVal });
break;
}
}
if (joinTable != null) {
JoinColumn[] froms = joinTable.joinColumns();
if (froms == null || froms.length == 0)
continue;
String from = froms[0].name();
JoinColumn[] tos = joinTable.inverseJoinColumns();
if (tos == null || tos.length == 0)
continue;
String to = tos[0].name();
String relTable = joinTable.name();
String fromRefCol = froms[0].referencedColumnName();
if (fromRefCol == null || fromRefCol.trim().length() == 0)
fromRefCol = ORMConfigBeanUtil.getIdColumn(t);
String fromRefField = ORMConfigBeanUtil.getField(t.getClass(), fromRefCol);
Method fromRefFieldGetter = ru.getGetter(fromRefField);
if (fromRefFieldGetter == null)
throw new Exception("can not find the 'from ref field -> " + fromRefField + "' of " + t.getClass() + " 's getter method");
Object _obj = fromRefFieldGetter.invoke(t);
if (_obj == null)
continue;
String fromRefVal = String.valueOf(_obj);
String toRefCol = tos[0].referencedColumnName();
if (toRefCol == null || toRefCol.trim().length() == 0)
toRefCol = ORMConfigBeanUtil.getIdColumn(tarClass);
String toRefField = ORMConfigBeanUtil.getField(tarClass, toRefCol);
Method toRefFieldGetter = tarRu.getGetter(toRefField);
if (toRefFieldGetter == null)
throw new Exception("can not find the 'to ref field -> " + toRefField + "' of " + tarClass + " 's getter method");
Object _obj2 = toRefFieldGetter.invoke(tarObj);
if (_obj2 == null)
continue;
String toRefVal = String.valueOf(_obj2);
// delete from relTable where from = ? and to = ? ;
String format = "delete from %s where %s = ? and %s = ? ;";
String sql = String.format(format, relTable, from, to);
// finished
DAOFactory.getUpdateDAO(dsName).updateBySQLWithArgs(sql, fromRefVal, toRefVal);
}
}
}
}
}
}
});
}
use of javax.persistence.ManyToOne in project eweb4j-framework by laiweiwei.
the class OneToManyDAO method select.
/**
* 一对多(主从)级联查询
*/
public void select() throws DAOException {
if (this.fields == null || this.fields.size() == 0)
return;
Class<?> ownClass = ru.getObject().getClass();
String fromRefVal = null;
for (Field f : fields) {
Method tarGetter = ru.getGetter(f.getName());
if (tarGetter == null)
continue;
OneToMany ann = tarGetter.getAnnotation(OneToMany.class);
if (ann == null) {
ann = f.getAnnotation(OneToMany.class);
if (ann == null)
continue;
}
OrderBy orderAnn = tarGetter.getAnnotation(OrderBy.class);
if (orderAnn == null)
orderAnn = f.getAnnotation(OrderBy.class);
Class<?> tarClass = ann.targetEntity();
if (void.class.isAssignableFrom(tarClass))
tarClass = ClassUtil.getGenericType(f);
String orderBy = "";
if (orderAnn != null && orderAnn.value().trim().length() > 0)
orderBy = " ORDER BY " + orderAnn.value().replace("t.", tarClass.getSimpleName().toLowerCase() + ".");
String mappedBy = ann.mappedBy();
try {
ReflectUtil tarRu = new ReflectUtil(tarClass);
List<?> tarList = null;
JoinTable joinTable = null;
if (f.isAnnotationPresent(JoinTable.class)) {
joinTable = f.getAnnotation(JoinTable.class);
} else if (tarGetter.isAnnotationPresent(JoinTable.class)) {
joinTable = tarGetter.getAnnotation(JoinTable.class);
}
// 如果用户填写了 JoinTable注解,说明是第三方表建立的关联关系,需要查询第三方表和字段才能获取到targetList
if (joinTable != null) {
JoinColumn[] froms = joinTable.joinColumns();
if (froms == null || froms.length == 0)
continue;
String tarTable = joinTable.name();
String from = froms[0].name();
String fromRefCol = froms[0].referencedColumnName();
if (fromRefCol == null || fromRefCol.trim().length() == 0)
fromRefCol = ORMConfigBeanUtil.getIdColumn(t);
String fromRefField = ORMConfigBeanUtil.getField(t.getClass(), fromRefCol);
Method fromRefFieldGetter = ru.getGetter(fromRefField);
if (fromRefFieldGetter == null)
throw new Exception("can not find the 'from ref field -> " + fromRefField + "' of " + t.getClass() + " 's getter method");
Object _obj = fromRefFieldGetter.invoke(t);
if (_obj == null)
continue;
fromRefVal = String.valueOf(_obj);
String format = "select %s from %s where %s = ? ;";
String sql = String.format(format, ORMConfigBeanUtil.getSelectAllColumn(tarClass), tarTable, from) + orderBy;
// finished
tarList = DAOFactory.getSelectDAO(dsName).selectBySQL(tarClass, sql, fromRefVal);
} else {
// 否则的话按照ManyToOne去查询
// 如果给定了 mappedBy,直接用这个mappedBy来获取filed,否则遍历。
Field mappedField = null;
if (mappedBy != null && mappedBy.trim().length() > 0) {
mappedField = tarRu.getField(mappedBy);
} else {
for (Field field : tarRu.getFields()) {
if (!field.getType().getName().equals(ownClass.getName()))
continue;
mappedField = field;
mappedBy = mappedField.getName();
break;
}
}
if (mappedField == null)
throw new Exception("mapped field of " + tarClass + " not found");
Method tarObjFieldGetter = tarRu.getGetter(mappedBy);
if (tarObjFieldGetter == null)
continue;
ManyToOne manyToOne = mappedField.getAnnotation(ManyToOne.class);
if (manyToOne == null)
manyToOne = tarObjFieldGetter.getAnnotation(ManyToOne.class);
if (manyToOne == null)
continue;
String fromRefCol = null;
JoinColumn joinCol = mappedField.getAnnotation(JoinColumn.class);
if (joinCol == null)
joinCol = tarObjFieldGetter.getAnnotation(JoinColumn.class);
if (joinCol != null)
fromRefCol = joinCol.referencedColumnName();
if (fromRefCol == null || fromRefCol.trim().length() == 0)
fromRefCol = ORMConfigBeanUtil.getIdColumn(t);
String fromRefField = ORMConfigBeanUtil.getField(ownClass, fromRefCol);
Method fromRefFieldGetter = ru.getGetter(fromRefField);
if (fromRefFieldGetter == null)
throw new Exception("can not find the 'from ref field field -> " + fromRefField + "' of " + ownClass + " 's getter method");
Object _obj = fromRefFieldGetter.invoke(t);
if (_obj != null)
fromRefVal = String.valueOf(_obj);
String format = "select %s from %s where %s = ? ;";
String sql = String.format(format, ORMConfigBeanUtil.getSelectAllColumn(tarClass), ORMConfigBeanUtil.getTable(tarClass, true), ORMConfigBeanUtil.getColumn(tarClass, mappedBy)) + orderBy;
// finished
tarList = DAOFactory.getSelectDAO(dsName).selectBySQL(tarClass, sql, fromRefVal);
}
if (tarList == null)
continue;
Method tarSetter = ru.getSetter(f.getName());
if (tarSetter == null)
continue;
tarSetter.invoke(t, tarList);
} catch (Exception e) {
throw new DAOException("", e);
}
}
}
use of javax.persistence.ManyToOne in project eweb4j-framework by laiweiwei.
the class DAOImpl method queryBySql.
public <T> Collection<T> queryBySql(final String sql) {
Class<T> mappingCls = null;
if (this.targetEntity == null)
mappingCls = (Class<T>) this.clazz;
else
mappingCls = (Class<T>) this.targetEntity;
List<T> result = null;
try {
if (Map.class.isAssignableFrom(mappingCls)) {
Connection con = ds.getConnection();
if (args != null && args.size() > 0) {
result = (List<T>) JdbcUtil.getListWithArgs(con, mappingCls, sql, args.toArray(new Object[] {}));
} else {
result = (List<T>) JdbcUtil.getList(con, mappingCls, sql);
}
} else {
if (args != null && args.size() > 0) {
result = (List<T>) DAOFactory.getSelectDAO(dsName).selectBySQL(mappingCls, sql, args.toArray(new Object[] {}));
} else {
result = (List<T>) DAOFactory.getSelectDAO(dsName).selectBySQL(mappingCls, sql);
}
}
//this.clear();
if (result != null && result.size() > 0) {
for (T t : result) {
// ToOne relation class cascade select
ReflectUtil ru = new ReflectUtil(t);
for (Field field : ru.getFields()) {
String f = field.getName();
OneToOne o2o = field.getAnnotation(OneToOne.class);
ManyToOne m2o = field.getAnnotation(ManyToOne.class);
OneToMany o2m = field.getAnnotation(OneToMany.class);
ManyToMany m2m = field.getAnnotation(ManyToMany.class);
FetchType fetchType = null;
boolean is2One = false;
if (o2o != null) {
fetchType = o2o.fetch();
is2One = true;
}
if (m2o != null) {
fetchType = m2o.fetch();
is2One = true;
}
if (o2m != null) {
fetchType = o2m.fetch();
}
if (m2m != null) {
fetchType = m2m.fetch();
}
if (fetchType == null)
continue;
String beanId = field.getType().getName();
if (!is2One)
beanId = ClassUtil.getGenericType(field).getName();
boolean isEntity = ORMConfigBeanCache.containsKey(beanId);
if (!isEntity)
continue;
if (unFetch.contains(f))
continue;
if (fetch.contains(f)) {
log.debug("cascade select -> " + t.getClass().getName() + "." + f);
DAOFactory.getCascadeDAO(dsName).select(t, f);
continue;
}
if (FetchType.LAZY.equals(fetchType))
continue;
log.debug("cascade select -> " + t.getClass().getName() + "." + f);
DAOFactory.getCascadeDAO(dsName).select(t, f);
}
}
}
return result;
} catch (Exception e) {
log.error("sql-->" + sql, e);
throw new DAOException(sql + " execute exception", e);
}
}
use of javax.persistence.ManyToOne in project orientdb by orientechnologies.
the class OObjectEntitySerializer method getSpecifiedLinkedType.
public static Class<?> getSpecifiedLinkedType(final Field f) {
final ManyToOne m = f.getAnnotation(ManyToOne.class);
if (m != null && !m.targetEntity().equals(void.class))
return m.targetEntity();
final OneToOne m2 = f.getAnnotation(OneToOne.class);
if (m2 != null && !m2.targetEntity().equals(void.class))
return m2.targetEntity();
return null;
}
Aggregations