use of javax.persistence.JoinTable in project hibernate-orm by hibernate.
the class Ejb3XmlManyToOneTest method testJoinTableAllChildren.
@Test
public void testJoinTableAllChildren() throws Exception {
reader = getReader(Entity1.class, "field1", "many-to-one.orm5.xml");
assertAnnotationPresent(ManyToOne.class);
assertAnnotationNotPresent(JoinColumn.class);
assertAnnotationNotPresent(JoinColumns.class);
assertAnnotationPresent(JoinTable.class);
JoinTable joinTableAnno = reader.getAnnotation(JoinTable.class);
assertEquals("cat1", joinTableAnno.catalog());
assertEquals("table1", joinTableAnno.name());
assertEquals("schema1", joinTableAnno.schema());
// JoinColumns
JoinColumn[] joinColumns = joinTableAnno.joinColumns();
assertEquals(2, joinColumns.length);
assertEquals("", joinColumns[0].name());
assertEquals("", joinColumns[0].referencedColumnName());
assertEquals("", joinColumns[0].table());
assertEquals("", joinColumns[0].columnDefinition());
assertTrue(joinColumns[0].insertable());
assertTrue(joinColumns[0].updatable());
assertTrue(joinColumns[0].nullable());
assertFalse(joinColumns[0].unique());
assertEquals("col1", joinColumns[1].name());
assertEquals("col2", joinColumns[1].referencedColumnName());
assertEquals("table2", joinColumns[1].table());
assertEquals("int", joinColumns[1].columnDefinition());
assertFalse(joinColumns[1].insertable());
assertFalse(joinColumns[1].updatable());
assertFalse(joinColumns[1].nullable());
assertTrue(joinColumns[1].unique());
// InverseJoinColumns
JoinColumn[] inverseJoinColumns = joinTableAnno.inverseJoinColumns();
assertEquals(2, inverseJoinColumns.length);
assertEquals("", inverseJoinColumns[0].name());
assertEquals("", inverseJoinColumns[0].referencedColumnName());
assertEquals("", inverseJoinColumns[0].table());
assertEquals("", inverseJoinColumns[0].columnDefinition());
assertTrue(inverseJoinColumns[0].insertable());
assertTrue(inverseJoinColumns[0].updatable());
assertTrue(inverseJoinColumns[0].nullable());
assertFalse(inverseJoinColumns[0].unique());
assertEquals("col3", inverseJoinColumns[1].name());
assertEquals("col4", inverseJoinColumns[1].referencedColumnName());
assertEquals("table3", inverseJoinColumns[1].table());
assertEquals("int", inverseJoinColumns[1].columnDefinition());
assertFalse(inverseJoinColumns[1].insertable());
assertFalse(inverseJoinColumns[1].updatable());
assertFalse(inverseJoinColumns[1].nullable());
assertTrue(inverseJoinColumns[1].unique());
// UniqueConstraints
UniqueConstraint[] uniqueConstraints = joinTableAnno.uniqueConstraints();
assertEquals(2, uniqueConstraints.length);
assertEquals("", uniqueConstraints[0].name());
assertEquals(1, uniqueConstraints[0].columnNames().length);
assertEquals("col5", uniqueConstraints[0].columnNames()[0]);
assertEquals("uq1", uniqueConstraints[1].name());
assertEquals(2, uniqueConstraints[1].columnNames().length);
assertEquals("col6", uniqueConstraints[1].columnNames()[0]);
assertEquals("col7", uniqueConstraints[1].columnNames()[1]);
}
use of javax.persistence.JoinTable in project hibernate-orm by hibernate.
the class Ejb3XmlOneToManyTest method testJoinTableNoChildren.
@Test
public void testJoinTableNoChildren() throws Exception {
reader = getReader(Entity2.class, "field1", "one-to-many.orm16.xml");
assertAnnotationPresent(OneToMany.class);
assertAnnotationPresent(JoinTable.class);
assertAnnotationNotPresent(JoinColumns.class);
assertAnnotationNotPresent(JoinColumn.class);
JoinTable joinTableAnno = reader.getAnnotation(JoinTable.class);
assertEquals("", joinTableAnno.catalog());
assertEquals("", joinTableAnno.name());
assertEquals("", joinTableAnno.schema());
assertEquals(0, joinTableAnno.joinColumns().length);
assertEquals(0, joinTableAnno.inverseJoinColumns().length);
assertEquals(0, joinTableAnno.uniqueConstraints().length);
}
use of javax.persistence.JoinTable 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.JoinTable in project hibernate-orm by hibernate.
the class ColumnsBuilder method buildDefaultJoinColumnsForXToOne.
Ejb3JoinColumn[] buildDefaultJoinColumnsForXToOne(XProperty property, PropertyData inferredData) {
Ejb3JoinColumn[] joinColumns;
JoinTable joinTableAnn = propertyHolder.getJoinTable(property);
if (joinTableAnn != null) {
joinColumns = Ejb3JoinColumn.buildJoinColumns(joinTableAnn.inverseJoinColumns(), null, entityBinder.getSecondaryTables(), propertyHolder, inferredData.getPropertyName(), buildingContext);
if (StringHelper.isEmpty(joinTableAnn.name())) {
throw new AnnotationException("JoinTable.name() on a @ToOne association has to be explicit: " + BinderHelper.getPath(propertyHolder, inferredData));
}
} else {
OneToOne oneToOneAnn = property.getAnnotation(OneToOne.class);
String mappedBy = oneToOneAnn != null ? oneToOneAnn.mappedBy() : null;
joinColumns = Ejb3JoinColumn.buildJoinColumns(null, mappedBy, entityBinder.getSecondaryTables(), propertyHolder, inferredData.getPropertyName(), buildingContext);
}
return joinColumns;
}
use of javax.persistence.JoinTable 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