use of org.apache.openejb.jee.jpa.Entity in project tomee by apache.
the class CmpJpaConversion method processEntityBean.
/**
* Generate the CMP mapping data for an individual
* EntityBean.
*
* @param ejbModule The module containing the bean.
* @param entityMappings The accumulated set of entity mappings.
* @param bean The been we're generating the mapping for.
*/
private void processEntityBean(final EjbModule ejbModule, final EntityMappings entityMappings, final EntityBean bean) {
// try to add a new persistence-context-ref for cmp
if (!addPersistenceContextRef(bean)) {
// which means it has a mapping, so skip this bean
return;
}
// get the real bean class
final Class ejbClass = loadClass(ejbModule.getClassLoader(), bean.getEjbClass());
// and generate a name for the subclass that will be generated and handed to the JPA
// engine as the managed class.
final String jpaEntityClassName = CmpUtil.getCmpImplClassName(bean.getAbstractSchemaName(), ejbClass.getName());
// We don't use this mapping directly, instead we pull entries from it
// the reason being is that we intend to support mappings that aren't
// exactly correct. i.e. users should be able to write mappings completely
// ignorant of the fact that we subclass. The fact that we subclass means
// these user supplied mappings might need to be adjusted as the jpa orm.xml
// file is extremely subclass/supperclass aware and mappings specified in it
// need to be spot on.
final EntityMappings userMappings = getUserEntityMappings(ejbModule);
// chain of the bean looking for any that have user defined mappings.
for (Class clazz = ejbClass; clazz != null; clazz = clazz.getSuperclass()) {
final MappedSuperclass mappedSuperclass = removeMappedSuperclass(userMappings, clazz.getName());
// that the mapping is correct. Copy it from their mappings to ours
if (mappedSuperclass != null) {
entityMappings.getMappedSuperclass().add(mappedSuperclass);
}
}
// Look for an existing mapping using the openejb generated subclass name
Entity entity = removeEntity(userMappings, jpaEntityClassName);
// because we are going to ignore all other xml metadata.
if (entity != null) {
// XmlMetadataComplete is an OpenEJB specific flag that
// tells all other legacy descriptor converters to keep
// their hands off.
entity.setXmlMetadataComplete(true);
entityMappings.getEntity().add(entity);
return;
}
if (entity == null) {
entity = new Entity(jpaEntityClassName);
}
// have to check for null everywhere.
if (entity.getAttributes() == null) {
entity.setAttributes(new Attributes());
}
// add the entity
entityMappings.getEntity().add(entity);
// OVERWRITE: description: contains the name of the entity bean
entity.setDescription(ejbModule.getModuleId() + "#" + bean.getEjbName());
// PRESERVE has queries: name: the name of the entity in queries
final String entityName = bean.getAbstractSchemaName();
entity.setName(entityName);
entity.setEjbName(bean.getEjbName());
final ClassLoader classLoader = ejbModule.getClassLoader();
final Collection<MappedSuperclass> mappedSuperclasses;
if (bean.getCmpVersion() == CmpVersion.CMP2) {
// perform the 2.x class mapping. This really just identifies the primary key and
// other cmp fields that will be generated for the concrete class and identify them
// to JPA.
mappedSuperclasses = mapClass2x(entity, bean, classLoader);
} else {
// map the cmp class, but if we are using a mapped super class,
// generate attribute-override instead of id and basic
mappedSuperclasses = mapClass1x(bean.getEjbClass(), entity, bean, classLoader);
}
// configuration. f
if (mappedSuperclasses != null) {
// that will get passed to the JPA engine.
for (final MappedSuperclass mappedSuperclass : mappedSuperclasses) {
entityMappings.getMappedSuperclass().add(mappedSuperclass);
}
}
// process queries
for (final Query query : bean.getQuery()) {
final NamedQuery namedQuery = new NamedQuery();
final QueryMethod queryMethod = query.getQueryMethod();
// todo deployment id could change in one of the later conversions... use entity name instead, but we need to save it off
final StringBuilder name = new StringBuilder();
name.append(entityName).append(".").append(queryMethod.getMethodName());
if (queryMethod.getMethodParams() != null && !queryMethod.getMethodParams().getMethodParam().isEmpty()) {
name.append('(');
boolean first = true;
for (final String methodParam : queryMethod.getMethodParams().getMethodParam()) {
if (!first) {
name.append(",");
}
name.append(methodParam);
first = false;
}
name.append(')');
}
namedQuery.setName(name.toString());
namedQuery.setQuery(query.getEjbQl());
entity.getNamedQuery().add(namedQuery);
}
// todo: there should be a common interface between ejb query object and openejb query object
final OpenejbJar openejbJar = ejbModule.getOpenejbJar();
final EjbDeployment ejbDeployment = openejbJar.getDeploymentsByEjbName().get(bean.getEjbName());
if (ejbDeployment != null) {
for (final org.apache.openejb.jee.oejb3.Query query : ejbDeployment.getQuery()) {
final NamedQuery namedQuery = new NamedQuery();
final org.apache.openejb.jee.oejb3.QueryMethod queryMethod = query.getQueryMethod();
// todo deployment id could change in one of the later conversions... use entity name instead, but we need to save it off
final StringBuilder name = new StringBuilder();
name.append(entityName).append(".").append(queryMethod.getMethodName());
if (queryMethod.getMethodParams() != null && !queryMethod.getMethodParams().getMethodParam().isEmpty()) {
name.append('(');
boolean first = true;
for (final String methodParam : queryMethod.getMethodParams().getMethodParam()) {
if (!first) {
name.append(",");
}
name.append(methodParam);
first = false;
}
name.append(')');
}
namedQuery.setName(name.toString());
namedQuery.setQuery(query.getObjectQl());
entity.getNamedQuery().add(namedQuery);
}
}
}
use of org.apache.openejb.jee.jpa.Entity in project tomee by apache.
the class CmpJpaConversion method removeEntity.
private Entity removeEntity(final EntityMappings userMappings, final String className) {
final Entity entity;
entity = userMappings.getEntityMap().get(className);
if (entity != null) {
userMappings.getEntityMap().remove(entity.getKey());
}
return entity;
}
use of org.apache.openejb.jee.jpa.Entity in project tomee by apache.
the class SunCmpConversionTest method convert.
private EntityMappings convert(final String ejbJarFileName, final String sunEjbJarFileName, final String sunCmpMappingsFileName, final String expectedFileName) throws Exception {
InputStream in = getClass().getClassLoader().getResourceAsStream(ejbJarFileName);
final EjbJar ejbJar = (EjbJar) JaxbJavaee.unmarshalJavaee(EjbJar.class, new ByteArrayInputStream(readContent(in).getBytes()));
// create and configure the module
final EjbModule ejbModule = new EjbModule(getClass().getClassLoader(), "TestModule", null, ejbJar, new OpenejbJar());
final InitEjbDeployments initEjbDeployments = new InitEjbDeployments();
initEjbDeployments.deploy(ejbModule);
final AppModule appModule = new AppModule(getClass().getClassLoader(), "TestModule");
appModule.getEjbModules().add(ejbModule);
// add the altDD
ejbModule.getAltDDs().put("sun-cmp-mappings.xml", getClass().getClassLoader().getResource(sunCmpMappingsFileName));
ejbModule.getAltDDs().put("sun-ejb-jar.xml", getClass().getClassLoader().getResource(sunEjbJarFileName));
// convert the cmp declarations into jpa entity declarations
final CmpJpaConversion cmpJpaConversion = new CmpJpaConversion();
cmpJpaConversion.deploy(appModule);
// EntityMappings entityMappings = cmpJpaConversion.generateEntityMappings(ejbModule);
// // load the sun-cmp-mappings.xml file
// String sunCmpMappingsXml = readContent(getClass().getClassLoader().getResourceAsStream(sunCmpMappingsFileName));
// SunCmpMappings sunCmpMappings = (SunCmpMappings) JaxbSun.unmarshal(SunCmpMappings.class, new ByteArrayInputStream(sunCmpMappingsXml.getBytes()));
// fill in the jpa entity declarations with database mappings from the sun-cmp-mappings.xml file
final SunConversion sunConversion = new SunConversion();
// sunCmpConversion.mergeEntityMappings(ejbModule, entityMappings);
sunConversion.deploy(appModule);
// compare the results to the expected results
if (expectedFileName != null) {
in = getClass().getClassLoader().getResourceAsStream(expectedFileName);
final String expected = readContent(in);
// Sun doen't really support generated primary keys, so we need to add them by hand here
final Set<String> generatedPks = new HashSet<String>(Arrays.asList("BasicCmp2", "AOBasicCmp2", "EncCmp2", "Cmp2RmiIiop"));
final EntityMappings cmpMappings = appModule.getCmpMappings();
for (final Entity entity : cmpMappings.getEntity()) {
if (generatedPks.contains(entity.getName())) {
entity.getAttributes().getId().get(0).setGeneratedValue(new GeneratedValue(GenerationType.IDENTITY));
}
}
final String actual = toString(cmpMappings);
XMLUnit.setIgnoreWhitespace(true);
try {
final Diff myDiff = new DetailedDiff(new Diff(expected, actual));
assertTrue("Files are not similar " + myDiff, myDiff.similar());
} catch (final AssertionFailedError e) {
assertEquals(expected, actual);
}
}
return appModule.getCmpMappings();
}
use of org.apache.openejb.jee.jpa.Entity in project tomee by apache.
the class CmpJpaConversion method deploy.
public AppModule deploy(final AppModule appModule) throws OpenEJBException {
if (!hasCmpEntities(appModule)) {
return appModule;
}
// todo scan existing persistence module for all entity mappings and don't generate mappings for them
// create mappings if no mappings currently exist
EntityMappings cmpMappings = appModule.getCmpMappings();
if (cmpMappings == null) {
cmpMappings = new EntityMappings();
cmpMappings.setVersion("1.0");
appModule.setCmpMappings(cmpMappings);
}
// app mapping data
for (final EjbModule ejbModule : appModule.getEjbModules()) {
final EjbJar ejbJar = ejbModule.getEjbJar();
// scan for CMP entity beans and merge the data into the collective set
for (final EnterpriseBean enterpriseBean : ejbJar.getEnterpriseBeans()) {
if (isCmpEntity(enterpriseBean)) {
processEntityBean(ejbModule, cmpMappings, (EntityBean) enterpriseBean);
}
}
// if there are relationships defined in this jar, get a list of the defined
// entities and process the relationship maps.
final Relationships relationships = ejbJar.getRelationships();
if (relationships != null) {
final Map<String, Entity> entitiesByEjbName = new TreeMap<String, Entity>();
for (final Entity entity : cmpMappings.getEntity()) {
entitiesByEjbName.put(entity.getEjbName(), entity);
}
for (final EjbRelation relation : relationships.getEjbRelation()) {
processRelationship(entitiesByEjbName, relation);
}
}
// Let's warn the user about any declarations we didn't end up using
// so there can be no misunderstandings.
final EntityMappings userMappings = getUserEntityMappings(ejbModule);
for (final Entity mapping : userMappings.getEntity()) {
logger.warning("openejb-cmp-orm.xml mapping ignored: module=" + ejbModule.getModuleId() + ": <entity class=\"" + mapping.getClazz() + "\">");
}
for (final MappedSuperclass mapping : userMappings.getMappedSuperclass()) {
logger.warning("openejb-cmp-orm.xml mapping ignored: module=" + ejbModule.getModuleId() + ": <mapped-superclass class=\"" + mapping.getClazz() + "\">");
}
}
if (!cmpMappings.getEntity().isEmpty()) {
final PersistenceUnit persistenceUnit = getCmpPersistenceUnit(appModule);
persistenceUnit.getMappingFile().add("META-INF/openejb-cmp-generated-orm.xml");
for (final Entity entity : cmpMappings.getEntity()) {
persistenceUnit.getClazz().add(entity.getClazz());
}
}
// causes some of the unit tests to fail. Not sure why. Should be fixed.
for (final Entity entity : appModule.getCmpMappings().getEntity()) {
if (entity.getAttributes() != null && entity.getAttributes().isEmpty()) {
entity.setAttributes(null);
}
}
return appModule;
}
use of org.apache.openejb.jee.jpa.Entity in project tomee by apache.
the class CmpJpaConversion method processRelationship.
private void processRelationship(final Map<String, Entity> entitiesByEjbName, final EjbRelation relation) throws OpenEJBException {
final List<EjbRelationshipRole> roles = relation.getEjbRelationshipRole();
// if we don't have two roles, the relation is bad so we skip it
if (roles.size() != 2) {
return;
}
// get left entity
final EjbRelationshipRole leftRole = roles.get(0);
final RelationshipRoleSource leftRoleSource = leftRole.getRelationshipRoleSource();
final String leftEjbName = leftRoleSource == null ? null : leftRoleSource.getEjbName();
final Entity leftEntity = entitiesByEjbName.get(leftEjbName);
// get right entity
final EjbRelationshipRole rightRole = roles.get(1);
final RelationshipRoleSource rightRoleSource = rightRole.getRelationshipRoleSource();
final String rightEjbName = rightRoleSource == null ? null : rightRoleSource.getEjbName();
final Entity rightEntity = entitiesByEjbName.get(rightEjbName);
// neither left or right have a mapping which is fine
if (leftEntity == null && rightEntity == null) {
return;
}
// left not found?
if (leftEntity == null) {
throw new OpenEJBException("Role source " + leftEjbName + " defined in relationship role " + relation.getEjbRelationName() + "::" + leftRole.getEjbRelationshipRoleName() + " not found");
}
// right not found?
if (rightEntity == null) {
throw new OpenEJBException("Role source " + rightEjbName + " defined in relationship role " + relation.getEjbRelationName() + "::" + rightRole.getEjbRelationshipRoleName() + " not found");
}
final Attributes rightAttributes = rightEntity.getAttributes();
final Map<String, RelationField> rightRelationships = rightAttributes.getRelationshipFieldMap();
final Attributes leftAttributes = leftEntity.getAttributes();
final Map<String, RelationField> leftRelationships = leftAttributes.getRelationshipFieldMap();
String leftFieldName = null;
boolean leftSynthetic = false;
if (leftRole.getCmrField() != null) {
leftFieldName = leftRole.getCmrField().getCmrFieldName();
} else {
leftFieldName = rightEntity.getName() + "_" + rightRole.getCmrField().getCmrFieldName();
leftSynthetic = true;
}
final boolean leftIsOne = leftRole.getMultiplicity() == Multiplicity.ONE;
String rightFieldName = null;
boolean rightSynthetic = false;
if (rightRole.getCmrField() != null) {
rightFieldName = rightRole.getCmrField().getCmrFieldName();
} else {
rightFieldName = leftEntity.getName() + "_" + leftRole.getCmrField().getCmrFieldName();
rightSynthetic = true;
}
final boolean rightIsOne = rightRole.getMultiplicity() == Multiplicity.ONE;
if (leftIsOne && rightIsOne) {
//
// one-to-one
//
// left
OneToOne leftOneToOne = null;
leftOneToOne = new OneToOne();
leftOneToOne.setName(leftFieldName);
leftOneToOne.setSyntheticField(leftSynthetic);
setCascade(rightRole, leftOneToOne);
addRelationship(leftOneToOne, leftRelationships, leftAttributes.getOneToOne());
// right
OneToOne rightOneToOne = null;
rightOneToOne = new OneToOne();
rightOneToOne.setName(rightFieldName);
rightOneToOne.setSyntheticField(rightSynthetic);
rightOneToOne.setMappedBy(leftFieldName);
setCascade(leftRole, rightOneToOne);
addRelationship(rightOneToOne, rightRelationships, rightAttributes.getOneToOne());
// link
leftOneToOne.setRelatedField(rightOneToOne);
rightOneToOne.setRelatedField(leftOneToOne);
} else if (leftIsOne && !rightIsOne) {
//
// one-to-many
//
// left
OneToMany leftOneToMany = null;
leftOneToMany = new OneToMany();
leftOneToMany.setName(leftFieldName);
leftOneToMany.setSyntheticField(leftSynthetic);
leftOneToMany.setMappedBy(rightFieldName);
setCascade(rightRole, leftOneToMany);
addRelationship(leftOneToMany, leftRelationships, leftAttributes.getOneToMany());
// right
ManyToOne rightManyToOne = null;
rightManyToOne = new ManyToOne();
rightManyToOne.setName(rightFieldName);
rightManyToOne.setSyntheticField(rightSynthetic);
setCascade(leftRole, rightManyToOne);
addRelationship(rightManyToOne, rightRelationships, rightAttributes.getManyToOne());
// link
leftOneToMany.setRelatedField(rightManyToOne);
rightManyToOne.setRelatedField(leftOneToMany);
} else if (!leftIsOne && rightIsOne) {
//
// many-to-one
//
// left
ManyToOne leftManyToOne = null;
leftManyToOne = new ManyToOne();
leftManyToOne.setName(leftFieldName);
leftManyToOne.setSyntheticField(leftSynthetic);
setCascade(rightRole, leftManyToOne);
addRelationship(leftManyToOne, leftRelationships, leftAttributes.getManyToOne());
// right
OneToMany rightOneToMany = null;
rightOneToMany = new OneToMany();
rightOneToMany.setName(rightFieldName);
rightOneToMany.setSyntheticField(rightSynthetic);
rightOneToMany.setMappedBy(leftFieldName);
setCascade(leftRole, rightOneToMany);
addRelationship(rightOneToMany, rightRelationships, rightAttributes.getOneToMany());
// link
leftManyToOne.setRelatedField(rightOneToMany);
rightOneToMany.setRelatedField(leftManyToOne);
} else if (!leftIsOne && !rightIsOne) {
//
// many-to-many
//
// left
ManyToMany leftManyToMany = null;
leftManyToMany = new ManyToMany();
leftManyToMany.setName(leftFieldName);
leftManyToMany.setSyntheticField(leftSynthetic);
setCascade(rightRole, leftManyToMany);
addRelationship(leftManyToMany, leftRelationships, leftAttributes.getManyToMany());
// right
ManyToMany rightManyToMany = null;
rightManyToMany = new ManyToMany();
rightManyToMany.setName(rightFieldName);
rightManyToMany.setSyntheticField(rightSynthetic);
rightManyToMany.setMappedBy(leftFieldName);
setCascade(leftRole, rightManyToMany);
addRelationship(rightManyToMany, rightRelationships, rightAttributes.getManyToMany());
// link
leftManyToMany.setRelatedField(rightManyToMany);
rightManyToMany.setRelatedField(leftManyToMany);
}
}
Aggregations