private void buildColumnAnnotation( Selectable selectable, StringBuffer annotations, boolean insertable, boolean updatable) { if (selectable.isFormula()) { annotations .append("@") .append(importType("org.hibernate.annotations.Formula")) .append("(value=\"") .append(selectable.getText()) .append("\")"); } else { Column column = (Column) selectable; annotations .append("@" + importType("javax.persistence.Column") + "(name=\"") .append(column.getName()) .append("\""); appendCommonColumnInfo(annotations, column, insertable, updatable); if (column.getPrecision() != Column.DEFAULT_PRECISION) { // the default is actually 0 in spec annotations.append(", precision=").append(column.getPrecision()); } if (column.getScale() != Column.DEFAULT_SCALE) { // default is actually 0 in spec annotations.append(", scale=").append(column.getScale()); } else if (column.getLength() != 255) { annotations.append(", length=").append(column.getLength()); } // TODO support secondary table annotations.append(")"); } }
public String generateAnnColumnAnnotation(Property property) { StringBuffer annotations = new StringBuffer(" "); boolean insertable = property.isInsertable(); boolean updatable = property.isUpdateable(); if (property.isComposite()) { annotations.append("@" + importType("javax.persistence.AttributeOverrides") + "( {"); Component component = (Component) property.getValue(); Iterator<?> subElements = component.getPropertyIterator(); buildRecursiveAttributeOverride(subElements, null, property, annotations); annotations.setLength(annotations.length() - 2); annotations.append(" } )"); } else { if (property.getColumnSpan() == 1) { Selectable selectable = (Selectable) property.getColumnIterator().next(); buildColumnAnnotation(selectable, annotations, insertable, updatable); } else { Iterator<?> columns = property.getColumnIterator(); annotations .append("@") .append(importType("org.hibernate.annotations.Columns")) .append("( { "); while (columns.hasNext()) { Selectable selectable = (Selectable) columns.next(); if (selectable.isFormula()) { // TODO formula in multicolumns not supported by annotations // annotations.append("/* TODO formula in multicolumns not supported by annotations // */"); } else { annotations.append("\n "); buildColumnAnnotation(selectable, annotations, insertable, updatable); annotations.append(", "); } } annotations.setLength(annotations.length() - 2); annotations.append(" } )"); } } return annotations.toString(); }
private void buildRecursiveAttributeOverride( Iterator<?> subElements, String path, Property property, StringBuffer annotations) { while (subElements.hasNext()) { Property subProperty = (Property) subElements.next(); if (subProperty.isComposite()) { if (path != null) { path = path + "."; } else { path = ""; } path = path + subProperty.getName(); Component component = (Component) subProperty.getValue(); buildRecursiveAttributeOverride( component.getPropertyIterator(), path, subProperty, annotations); } else { Iterator<?> columns = subProperty.getColumnIterator(); Selectable selectable = (Selectable) columns.next(); if (selectable.isFormula()) { // TODO formula in multicolumns not supported by annotations } else { annotations .append("\n ") .append("@") .append(importType("javax.persistence.AttributeOverride")) .append("(name=\""); if (path != null) { annotations.append(path).append("."); } annotations.append(subProperty.getName()).append("\"").append(", column="); buildColumnAnnotation( selectable, annotations, subProperty.isInsertable(), subProperty.isUpdateable()); annotations.append(" ), "); } } } }