final Set<String> primaryKeyFields = new HashSet<String>();
if (bean.getPrimkeyField() != null) {
final String fieldName = bean.getPrimkeyField();
final MappedSuperclass superclass = superclassByField.get(fieldName);
// this need not be here...for CMP 2.x, these are generally autogenerated fields.
if (superclass != null) {
// ok, add this field to the superclass mapping
superclass.addField(new Id(fieldName));
// the direct mapping is an over ride
mapping.addField(new AttributeOverride(fieldName));
} else {
// this is a normal generated field, it will be in the main class mapping.
mapping.addField(new Id(fieldName));
}
primaryKeyFields.add(fieldName);
} else if ("java.lang.Object".equals(bean.getPrimKeyClass())) {
// the automatically generated keys use a special property name
// and will always be in the generated superclass.
final String fieldName = "OpenEJB_pk";
final Id field = new Id(fieldName);
field.setGeneratedValue(new GeneratedValue(GenerationType.AUTO));
mapping.addField(field);
primaryKeyFields.add(fieldName);
} else if (bean.getPrimKeyClass() != null) {
final Class<?> pkClass;
try {
pkClass = classLoader.loadClass(bean.getPrimKeyClass());
MappedSuperclass idclass = null;
// now validate the primary class fields against the bean cmp fields
// to make sure everything maps correctly.
for (final Field pkField : pkClass.getFields()) {
final String pkFieldName = pkField.getName();
final int modifiers = pkField.getModifiers();
if (Modifier.isPublic(modifiers) && !Modifier.isStatic(modifiers) && allFields.contains(pkFieldName)) {
// see if the bean field is concretely defined in one of the superclasses
final MappedSuperclass superclass = superclassByField.get(pkFieldName);
if (superclass != null) {
// ok, we have an override that needs to be specified at the main class level.
superclass.addField(new Id(pkFieldName));
mapping.addField(new AttributeOverride(pkFieldName));
idclass = resolveIdClass(idclass, superclass, beanClass);
} else {
// this field will be autogenerated
mapping.addField(new Id(pkFieldName));
}
primaryKeyFields.add(pkFieldName);
}
}
// if we've located an ID class, set it as such
if (idclass != null) {
idclass.setIdClass(new IdClass(bean.getPrimKeyClass()));
} else {
// do this for the toplevel mapping
mapping.setIdClass(new IdClass(bean.getPrimKeyClass()));
}
} catch (final ClassNotFoundException e) {
throw (IllegalStateException) new IllegalStateException("Could not find entity primary key class " + bean.getPrimKeyClass()).initCause(e);
}
}
//
// basic: cmp-fields
// This again, adds all of the additional cmp-fields to the mapping
//
for (final CmpField cmpField : bean.getCmpField()) {
// only add entries for cmp fields that are not part of the primary key
if (!primaryKeyFields.contains(cmpField.getFieldName())) {
final String fieldName = cmpField.getFieldName();
// this will be here if we've already processed this
final MappedSuperclass superclass = superclassByField.get(fieldName);
// if this field is defined by one of the superclasses, then
// we need to provide a mapping for this.
if (superclass != null) {
// we need to mark this as being in one of the superclasses
superclass.addField(new Basic(fieldName));
mapping.addField(new AttributeOverride(fieldName));
} else {
// directly generated.
mapping.addField(new Basic(fieldName));
}