public String generateCollectionAnnotation(Property property, Configuration cfg) {
StringBuffer annotation = new StringBuffer();
Value value = property.getValue();
if ( value != null && value instanceof Collection) {
Collection collection = (Collection) value;
if ( collection.isOneToMany() ) {
String mappedBy = null;
AnnotationBuilder ab = AnnotationBuilder.createAnnotation( importType( "javax.persistence.OneToMany") );
ab.addAttribute( "cascade", getCascadeTypes( property ) );
ab.addAttribute( "fetch", getFetchType (property) );
if ( collection.isInverse() ) {
mappedBy = getOneToManyMappedBy( cfg, collection );
ab.addQuotedAttribute( "mappedBy", mappedBy );
}
annotation.append( ab.getResult() );
if (mappedBy == null) annotation.append("\n").append( generateJoinColumnsAnnotation(property, cfg) );
}
else {
//TODO do the @OneToMany @JoinTable
//TODO composite element
String mappedBy = null;
AnnotationBuilder ab = AnnotationBuilder.createAnnotation( importType( "javax.persistence.ManyToMany") );
ab.addAttribute( "cascade", getCascadeTypes( property ) );
ab.addAttribute( "fetch", getFetchType (property) );
if ( collection.isInverse() ) {
mappedBy = getManyToManyMappedBy( cfg, collection );
ab.addQuotedAttribute( "mappedBy", mappedBy );
}
annotation.append(ab.getResult());
if (mappedBy == null) {
annotation.append("\n @");
annotation.append( importType( "javax.persistence.JoinTable") ).append( "(name=\"" );
Table table = collection.getCollectionTable();
annotation.append( table.getName() );
annotation.append( "\"" );
if ( StringHelper.isNotEmpty( table.getSchema() ) ) {
annotation.append(", schema=\"").append( table.getSchema() ).append("\"");
}
if ( StringHelper.isNotEmpty( table.getCatalog() ) ) {
annotation.append(", catalog=\"").append( table.getCatalog() ).append("\"");
}
String uniqueConstraint = generateAnnTableUniqueConstraint(table);
if ( uniqueConstraint.length() > 0 ) {
annotation.append(", uniqueConstraints=").append(uniqueConstraint);
}
annotation.append( ", joinColumns = { ");
buildArrayOfJoinColumnAnnotation(
collection.getKey().getColumnIterator(),
null,
annotation,
property.isInsertable(),
property.isUpdateable()
);
annotation.append( " }");
annotation.append( ", inverseJoinColumns = { ");
buildArrayOfJoinColumnAnnotation(
collection.getElement().getColumnIterator(),
null,
annotation,
property.isInsertable(),
property.isUpdateable()
);