use of com.manydesigns.portofino.model.database.Column in project Portofino by ManyDesigns.
the class SessionFactoryBuilder method ensurePrimaryKey.
protected boolean ensurePrimaryKey(Table table) {
List<Column> idColumns = new ArrayList<>();
for (Column column : table.getColumns()) {
column.getAnnotations().forEach(ann -> {
Class<?> annotationClass = ann.getJavaAnnotationClass();
if (Id.class.equals(annotationClass)) {
idColumns.add(column);
}
});
}
if (idColumns.isEmpty()) {
return false;
} else {
logger.info("Creating primary key on table {} according to @Id annotations", table.getQualifiedName());
PrimaryKey pk = new PrimaryKey(table);
pk.setPrimaryKeyName("synthetic_pk_" + table.getQualifiedName().replace('.', '_'));
for (Column column : idColumns) {
pk.add(column);
}
table.setPrimaryKey(pk);
return true;
}
}
use of com.manydesigns.portofino.model.database.Column in project Portofino by ManyDesigns.
the class SessionFactoryBuilder method setupColumns.
protected void setupColumns(Table table, CtClass cc, ConstPool constPool) throws CannotCompileException, NotFoundException {
List<Column> columnPKList = table.getPrimaryKey().getColumns();
Annotation annotation;
for (Column column : table.getColumns()) {
String propertyName = column.getActualPropertyName();
CtField field = new CtField(classPool.get(column.getActualJavaType().getName()), propertyName, cc);
AnnotationsAttribute fieldAnnotations = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);
annotation = new Annotation(javax.persistence.Column.class.getName(), constPool);
annotation.addMemberValue("name", new StringMemberValue(jpaEscape(column.getColumnName()), constPool));
annotation.addMemberValue("nullable", new BooleanMemberValue(column.isNullable(), constPool));
if (column.getLength() != null) {
annotation.addMemberValue("precision", new IntegerMemberValue(constPool, column.getLength()));
annotation.addMemberValue("length", new IntegerMemberValue(constPool, column.getLength()));
}
if (column.getScale() != null) {
annotation.addMemberValue("scale", new IntegerMemberValue(constPool, column.getScale()));
}
fieldAnnotations.addAnnotation(annotation);
if (columnPKList.contains(column)) {
annotation.addMemberValue("updatable", new BooleanMemberValue(false, constPool));
annotation = new Annotation(Id.class.getName(), constPool);
fieldAnnotations.addAnnotation(annotation);
if (column.isAutoincrement()) {
setupIdentityGenerator(fieldAnnotations, constPool);
} else {
PrimaryKeyColumn pkColumn = table.getPrimaryKey().findPrimaryKeyColumnByName(column.getColumnName());
Generator generator = pkColumn.getGenerator();
if (generator != null) {
setupNonIdentityGenerator(table, fieldAnnotations, generator, constPool);
}
}
}
setupColumnType(column, fieldAnnotations, constPool);
column.getAnnotations().forEach(ann -> {
Class<?> annotationClass = ann.getJavaAnnotationClass();
if (javax.persistence.Column.class.equals(annotationClass) || Id.class.equals(annotationClass) || org.hibernate.annotations.Type.class.equals(annotationClass)) {
logger.debug("@Column or @Id or @Type specified on column {}, ignoring annotation {}", column.getQualifiedName(), annotationClass);
return;
}
Annotation fieldAnn = convertAnnotation(constPool, ann);
if (fieldAnn != null) {
fieldAnnotations.addAnnotation(fieldAnn);
}
});
field.getFieldInfo().addAttribute(fieldAnnotations);
field.setModifiers(javassist.Modifier.PROTECTED);
cc.addField(field);
String accessorName = propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
cc.addMethod(CtNewMethod.getter("get" + accessorName, field));
cc.addMethod(CtNewMethod.setter("set" + accessorName, field));
}
}
use of com.manydesigns.portofino.model.database.Column in project Portofino by ManyDesigns.
the class SessionFactoryBuilder method checkValidFk.
protected boolean checkValidFk(ForeignKey foreignKey) {
Table toTable = foreignKey.getToTable();
if (toTable == null) {
logger.error("The foreign key " + foreignKey.getQualifiedName() + " does not refer to any table.");
return false;
}
if (checkInvalidPrimaryKey(toTable, false)) {
logger.error("The foreign key " + foreignKey.getQualifiedName() + " refers to a table with absent or invalid primary key.");
return false;
}
// Check that referenced columns coincide with the primary key
Set<Column> fkColumns = new HashSet<>();
Set<Column> pkColumns = new HashSet<>(toTable.getPrimaryKey().getColumns());
for (Reference reference : foreignKey.getReferences()) {
fkColumns.add(reference.getActualToColumn());
}
if (!fkColumns.equals(pkColumns)) {
logger.error("The foreign key " + foreignKey.getQualifiedName() + " does not refer to " + "the exact primary key of table " + toTable.getQualifiedName() + ", this is not supported.");
return false;
}
try {
getMappedClass(toTable);
} catch (NotFoundException e) {
logger.error("The foreign key " + foreignKey.getQualifiedName() + " refers to unmapped table " + toTable.getQualifiedName() + ", skipping.");
return false;
}
return true;
}
use of com.manydesigns.portofino.model.database.Column in project Portofino by ManyDesigns.
the class SessionFactoryBuilder method defineEqualsAndHashCode.
protected void defineEqualsAndHashCode(Table table, CtClass cc) throws CannotCompileException {
List<Column> columnPKList = table.getPrimaryKey().getColumns();
String equalsMethod = "public boolean equals(Object other) {" + " if(!(other instanceof " + cc.getName() + ")) {" + " return false;" + " }" + cc.getName() + " castOther = (" + cc.getName() + ") other;";
String hashCodeMethod = "public int hashCode() {" + " return java.util.Objects.hash(new java.lang.Object[] {";
boolean first = true;
for (Column c : columnPKList) {
equalsMethod += " if(!java.util.Objects.equals(this." + c.getActualPropertyName() + ", castOther." + c.getActualPropertyName() + ")) {" + " return false;" + " }";
if (first) {
first = false;
} else {
hashCodeMethod += ", ";
}
hashCodeMethod += c.getActualPropertyName();
}
equalsMethod += "return true; }";
hashCodeMethod += "});}";
cc.addMethod(CtNewMethod.make(equalsMethod, cc));
cc.addMethod(CtNewMethod.make(hashCodeMethod, cc));
}
use of com.manydesigns.portofino.model.database.Column in project Portofino by ManyDesigns.
the class TableAccessor method setupKeyColumns.
private void setupKeyColumns(List<Column> columns, List<Column> pkColumns) {
int i = 0;
for (Column current : pkColumns) {
int index = columns.indexOf(current);
ColumnAccessor columnAccessor = columnAccessors[index];
keyColumnAccessors[i] = columnAccessor;
i++;
}
}
Aggregations