use of javax.persistence.ManyToMany in project hibernate-orm by hibernate.
the class Ejb3XmlManyToManyTest method testCascadeSomeWithDefaultPersist.
@Test
public void testCascadeSomeWithDefaultPersist() throws Exception {
reader = getReader(Entity2.class, "field1", "many-to-many.orm19.xml");
assertAnnotationPresent(ManyToMany.class);
ManyToMany relAnno = reader.getAnnotation(ManyToMany.class);
assertEquals(4, relAnno.cascade().length);
assertEquals(CascadeType.REMOVE, relAnno.cascade()[0]);
assertEquals(CascadeType.REFRESH, relAnno.cascade()[1]);
assertEquals(CascadeType.DETACH, relAnno.cascade()[2]);
assertEquals(CascadeType.PERSIST, relAnno.cascade()[3]);
}
use of javax.persistence.ManyToMany in project hibernate-orm by hibernate.
the class Ejb3XmlManyToManyTest method testAllAttributes.
@Test
public void testAllAttributes() throws Exception {
reader = getReader(Entity2.class, "field1", "many-to-many.orm21.xml");
assertAnnotationPresent(ManyToMany.class);
assertAnnotationNotPresent(OrderBy.class);
assertAnnotationNotPresent(OrderColumn.class);
assertAnnotationNotPresent(MapKey.class);
assertAnnotationNotPresent(MapKeyClass.class);
assertAnnotationNotPresent(MapKeyTemporal.class);
assertAnnotationNotPresent(MapKeyEnumerated.class);
assertAnnotationNotPresent(MapKeyColumn.class);
assertAnnotationNotPresent(MapKeyJoinColumns.class);
assertAnnotationNotPresent(MapKeyJoinColumn.class);
assertAnnotationNotPresent(JoinTable.class);
assertAnnotationPresent(Access.class);
ManyToMany relAnno = reader.getAnnotation(ManyToMany.class);
assertEquals(0, relAnno.cascade().length);
assertEquals(FetchType.EAGER, relAnno.fetch());
assertEquals("field2", relAnno.mappedBy());
assertEquals(Entity3.class, relAnno.targetEntity());
assertEquals(AccessType.PROPERTY, reader.getAnnotation(Access.class).value());
}
use of javax.persistence.ManyToMany in project hibernate-orm by hibernate.
the class JPAOverriddenAnnotationReader method overridesDefaultsInJoinTable.
private JoinTable overridesDefaultsInJoinTable(Annotation annotation, XMLContext.Default defaults) {
//no element but might have some default or some annotation
boolean defaultToJoinTable = !(isPhysicalAnnotationPresent(JoinColumn.class) || isPhysicalAnnotationPresent(JoinColumns.class));
final Class<? extends Annotation> annotationClass = annotation.annotationType();
defaultToJoinTable = defaultToJoinTable && ((annotationClass == ManyToMany.class && StringHelper.isEmpty(((ManyToMany) annotation).mappedBy())) || (annotationClass == OneToMany.class && StringHelper.isEmpty(((OneToMany) annotation).mappedBy())) || (annotationClass == ElementCollection.class));
final Class<JoinTable> annotationType = JoinTable.class;
if (defaultToJoinTable && (StringHelper.isNotEmpty(defaults.getCatalog()) || StringHelper.isNotEmpty(defaults.getSchema()))) {
AnnotationDescriptor ad = new AnnotationDescriptor(annotationType);
if (defaults.canUseJavaAnnotations()) {
JoinTable table = getPhysicalAnnotation(annotationType);
if (table != null) {
ad.setValue("name", table.name());
ad.setValue("schema", table.schema());
ad.setValue("catalog", table.catalog());
ad.setValue("uniqueConstraints", table.uniqueConstraints());
ad.setValue("joinColumns", table.joinColumns());
ad.setValue("inverseJoinColumns", table.inverseJoinColumns());
}
}
if (StringHelper.isEmpty((String) ad.valueOf("schema")) && StringHelper.isNotEmpty(defaults.getSchema())) {
ad.setValue("schema", defaults.getSchema());
}
if (StringHelper.isEmpty((String) ad.valueOf("catalog")) && StringHelper.isNotEmpty(defaults.getCatalog())) {
ad.setValue("catalog", defaults.getCatalog());
}
return AnnotationFactory.create(ad);
} else if (defaults.canUseJavaAnnotations()) {
return getPhysicalAnnotation(annotationType);
} else {
return null;
}
}
use of javax.persistence.ManyToMany 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.ManyToMany in project eweb4j-framework by laiweiwei.
the class ManyToManyDAO method update.
/**
* 一对多级联更新
*/
public void update(Long newFromRefVal) {
if (this.fields == null || this.fields.size() == 0)
return;
// "update {table} set {fromRefCol} = {newFromRefVal} where {fromRefCol} = {fromRefVal}
// ; update {relTable} set {from} = {newFromRefVal} where {from} = {fromRefVal}"
String format = "update %s set %s = %s where %s = %s ;";
for (Field f : fields) {
Method tarGetter = ru.getGetter(f.getName());
if (tarGetter == null)
continue;
ManyToMany ann = tarGetter.getAnnotation(ManyToMany.class);
if (ann == null) {
ann = f.getAnnotation(ManyToMany.class);
if (ann == null)
continue;
}
JoinTable join = tarGetter.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;
// 第三方关系表
String relTable = join.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);
try {
Method fromRefFieldGetter = ru.getGetter(fromRefField);
if (fromRefFieldGetter == null)
throw new DAOException("can not find the 'from ref field -> " + fromRefField + "' of " + t.getClass() + " 's getter method", null);
Object _obj = fromRefFieldGetter.invoke(t);
if (_obj == null)
continue;
String fromRefVal = String.valueOf(_obj);
// "update {table} set {fromRefCol} = {newFromRefVal} where {fromRefCol} = {fromRefVal}
// ; update {relTable} set {from} = {newFromRefVal} where {from} = {fromRefVal}"
final String sql1 = String.format(format, table, fromRefCol, newFromRefVal, fromRefCol, fromRefVal);
final String sql2 = String.format(format, relTable, from, newFromRefVal, from, fromRefVal);
Transaction.execute(new Trans() {
@Override
public void run(Object... args) throws Exception {
DAOFactory.getUpdateDAO(dsName).updateBySQL(sql1);
DAOFactory.getUpdateDAO(dsName).updateBySQL(sql2);
}
});
} catch (Exception e) {
throw new DAOException("", e);
}
}
}
Aggregations