use of javax.persistence.OneToMany in project hibernate-orm by hibernate.
the class ColumnsBuilder method extractMetadata.
public ColumnsBuilder extractMetadata() {
columns = null;
joinColumns = buildExplicitJoinColumns(property, inferredData);
if (property.isAnnotationPresent(Column.class) || property.isAnnotationPresent(Formula.class)) {
Column ann = property.getAnnotation(Column.class);
Formula formulaAnn = property.getAnnotation(Formula.class);
columns = Ejb3Column.buildColumnFromAnnotation(new Column[] { ann }, formulaAnn, nullability, propertyHolder, inferredData, entityBinder.getSecondaryTables(), buildingContext);
} else if (property.isAnnotationPresent(Columns.class)) {
Columns anns = property.getAnnotation(Columns.class);
columns = Ejb3Column.buildColumnFromAnnotation(anns.columns(), null, nullability, propertyHolder, inferredData, entityBinder.getSecondaryTables(), buildingContext);
}
//set default values if needed
if (joinColumns == null && (property.isAnnotationPresent(ManyToOne.class) || property.isAnnotationPresent(OneToOne.class))) {
joinColumns = buildDefaultJoinColumnsForXToOne(property, inferredData);
} else if (joinColumns == null && (property.isAnnotationPresent(OneToMany.class) || property.isAnnotationPresent(ElementCollection.class))) {
OneToMany oneToMany = property.getAnnotation(OneToMany.class);
String mappedBy = oneToMany != null ? oneToMany.mappedBy() : "";
joinColumns = Ejb3JoinColumn.buildJoinColumns(null, mappedBy, entityBinder.getSecondaryTables(), propertyHolder, inferredData.getPropertyName(), buildingContext);
} else if (joinColumns == null && property.isAnnotationPresent(org.hibernate.annotations.Any.class)) {
throw new AnnotationException("@Any requires an explicit @JoinColumn(s): " + BinderHelper.getPath(propertyHolder, inferredData));
}
if (columns == null && !property.isAnnotationPresent(ManyToMany.class)) {
//useful for collection of embedded elements
columns = Ejb3Column.buildColumnFromAnnotation(null, null, nullability, propertyHolder, inferredData, entityBinder.getSecondaryTables(), buildingContext);
}
if (nullability == Nullability.FORCED_NOT_NULL) {
//force columns to not null
for (Ejb3Column col : columns) {
col.forceNotNull();
}
}
return this;
}
use of javax.persistence.OneToMany in project hibernate-orm by hibernate.
the class PersistentAttributesHelper method getMappedByFromAnnotation.
private static String getMappedByFromAnnotation(CtField persistentField) {
OneToOne oto = PersistentAttributesHelper.getAnnotation(persistentField, OneToOne.class);
if (oto != null) {
return oto.mappedBy();
}
OneToMany otm = PersistentAttributesHelper.getAnnotation(persistentField, OneToMany.class);
if (otm != null) {
return otm.mappedBy();
}
// For @ManyToOne associations, mappedBy must come from the @OneToMany side of the association
ManyToMany mtm = PersistentAttributesHelper.getAnnotation(persistentField, ManyToMany.class);
return mtm == null ? "" : mtm.mappedBy();
}
use of javax.persistence.OneToMany in project eweb4j-framework by laiweiwei.
the class OneToManyDAO method insert.
/**
* 一对多(主从)级联插入 。
* 1. 如果主对象id值没有,将主对象插入数据库,否则不插入
* 2. 遍历从对象,找到mappedBy
* 3. 注入主对象,插入关系
* 4. 如果找不到mappedBy,则先找@JoinTable,然后判断次对象是否id值,如果没有就插入次对象,否则不插入,
* 8. 检查下是否有重复关系,接着插入关系表
* 5. 如果找不到@JoinTable,则根据主对象 class 在从对象属性中找。然后注入主对象,插入关系。
*/
public void insert() 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 {
//检查主对象是否有ID值,若没有、插入数据库否则不用
if (idVal == null || "0".equals(idVal) || "".equals(idVal)) {
Number _idVal = DAOFactory.getInsertDAO(dsName).insert(t);
if (_idVal == null || _idVal.intValue() <= 0)
throw new Exception("can not inster the main obj into db");
} else if (DAOFactory.getSelectDAO(dsName).selectOneById(t) == null) {
throw new Exception("the main object'id val is invalid!");
}
String fromRefVal = null;
for (Field f : fields) {
Method fGetter = ru.getGetter(f.getName());
if (fGetter == null)
continue;
OneToMany oneToMany = null;
if (f.isAnnotationPresent(OneToMany.class)) {
oneToMany = f.getAnnotation(OneToMany.class);
} else if (fGetter.isAnnotationPresent(OneToMany.class)) {
oneToMany = fGetter.getAnnotation(OneToMany.class);
} else {
continue;
}
Class<?> tarClass = oneToMany.targetEntity();
if (void.class.isAssignableFrom(tarClass)) {
tarClass = ClassUtil.getGenericType(f);
}
List<?> fList = null;
try {
fList = (List<?>) fGetter.invoke(t);
} catch (Exception e) {
throw new DAOException(fGetter + " invoke exception ", e);
}
if (fList == null)
continue;
for (int i = 0; i < fList.size(); i++) {
Object tarObj = fList.get(i);
if (tarObj == null)
continue;
ReflectUtil tarRu = new ReflectUtil(tarObj);
String mappedBy = oneToMany.mappedBy();
if (mappedBy != null && mappedBy.trim().length() > 0) {
Method ownFieldSetter = tarRu.getSetter(mappedBy);
if (ownFieldSetter == null)
continue;
// finished
ownFieldSetter.invoke(tarObj, ru.getObject());
Models.inst(tarObj).create();
} else {
JoinTable joinTable = null;
if (f.isAnnotationPresent(JoinTable.class)) {
joinTable = f.getAnnotation(JoinTable.class);
} else if (fGetter.isAnnotationPresent(JoinTable.class)) {
joinTable = fGetter.getAnnotation(JoinTable.class);
} else {
// find ownclass in tarObj fields
for (Field tarObjField : tarRu.getFields()) {
if (tarObjField.getType().getName().equals(ownClass.getName())) {
Method ownFieldSetter = tarRu.getSetter(tarObjField.getName());
if (ownFieldSetter == null)
continue;
// finished
ownFieldSetter.invoke(tarObj, ru.getObject());
Models.inst(tarObj).create();
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;
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(t);
if (_obj2 == null)
continue;
String toRefVal = String.valueOf(_obj2);
// 插入到关系表中
// 先检查下是否有重复记录
// "select {from},{to} from {relTable} where {from} = {fromRefVal} and {to} = {toRefVal} "
String _format = "select %s, %s from %s where %s = ? and %s = ? ";
String _sql = String.format(_format, from, to, relTable, from, to);
if (DAOFactory.getSelectDAO(dsName).selectBySQL(Map.class, _sql, fromRefVal, toRefVal) != null)
continue;
// insert into relTable (from, to) values(?, ?) ;
String format = "insert into %s(%s, %s) values(?, ?) ;";
String sql = String.format(format, relTable, from, to);
DAOFactory.getUpdateDAO(dsName).updateBySQLWithArgs(sql, fromRefVal, toRefVal);
}
}
}
}
}
});
}
use of javax.persistence.OneToMany 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.OneToMany in project orientdb by orientechnologies.
the class OObjectEntitySerializer method getSpecifiedMultiLinkedType.
public static Class<?> getSpecifiedMultiLinkedType(final Field f) {
final OneToMany m1 = f.getAnnotation(OneToMany.class);
if (m1 != null && !m1.targetEntity().equals(void.class))
return m1.targetEntity();
final ManyToMany m3 = f.getAnnotation(ManyToMany.class);
if (m3 != null && !m3.targetEntity().equals(void.class))
return m3.targetEntity();
return null;
}
Aggregations