blob: 4be0ae3e785a586a62541673e272cb006f634ae5 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2008, 2011 Oracle. All rights reserved.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0, which accompanies this distribution
* and is available at http://www.eclipse.org/legal/epl-v10.html.
*
* Contributors:
* Oracle - initial API and implementation
*******************************************************************************/
package org.eclipse.jpt.jpa.eclipselink.core.tests.internal.context.orm;
import java.util.Iterator;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jpt.common.core.tests.internal.projects.TestJavaProject.SourceWriter;
import org.eclipse.jpt.common.utility.internal.iterators.ArrayIterator;
import org.eclipse.jpt.jpa.core.MappingKeys;
import org.eclipse.jpt.jpa.core.context.Cascade;
import org.eclipse.jpt.jpa.core.context.FetchType;
import org.eclipse.jpt.jpa.core.context.JoinTable;
import org.eclipse.jpt.jpa.core.context.OneToManyMapping;
import org.eclipse.jpt.jpa.core.context.java.JavaOneToManyMapping;
import org.eclipse.jpt.jpa.core.context.orm.OrmPersistentAttribute;
import org.eclipse.jpt.jpa.core.context.orm.OrmPersistentType;
import org.eclipse.jpt.jpa.core.context.orm.OrmReadOnlyPersistentAttribute;
import org.eclipse.jpt.jpa.core.resource.java.JPA;
import org.eclipse.jpt.jpa.core.resource.orm.OrmFactory;
import org.eclipse.jpt.jpa.eclipselink.core.context.EclipseLinkJoinFetchType;
import org.eclipse.jpt.jpa.eclipselink.core.context.EclipseLinkOneToManyMapping;
import org.eclipse.jpt.jpa.eclipselink.core.context.EclipseLinkOneToManyRelationship2_0;
import org.eclipse.jpt.jpa.eclipselink.core.context.EclipseLinkRelationshipMapping;
import org.eclipse.jpt.jpa.eclipselink.core.internal.context.orm.OrmEclipseLinkOneToManyMapping;
import org.eclipse.jpt.jpa.eclipselink.core.resource.java.EclipseLink;
import org.eclipse.jpt.jpa.eclipselink.core.resource.orm.XmlEntity;
import org.eclipse.jpt.jpa.eclipselink.core.resource.orm.XmlJoinFetchType;
import org.eclipse.jpt.jpa.eclipselink.core.resource.orm.XmlOneToMany;
import org.eclipse.jpt.jpa.eclipselink.core.tests.internal.context.EclipseLinkContextModelTestCase;
@SuppressWarnings("nls")
public class EclipseLinkOrmOneToManyMappingTests
extends EclipseLinkContextModelTestCase
{
public EclipseLinkOrmOneToManyMappingTests(String name) {
super(name);
}
private void createTestDepartment() throws Exception {
SourceWriter sourceWriter = new SourceWriter() {
public void appendSourceTo(StringBuilder sb) {
sb.append(CR);
sb.append("import ").append(JPA.ENTITY).append(";");
sb.append(CR);
sb.append("import ").append(JPA.ID).append(";");
sb.append(CR);
sb.append("import java.util.Collection;");
sb.append(CR);
sb.append("import ").append(JPA.JOIN_TABLE).append(";");
sb.append(CR);
sb.append("import ").append(JPA.JOIN_COLUMN).append(";");
sb.append("@Entity");
sb.append(CR);
sb.append("public class ").append("Department").append(" ");
sb.append("{").append(CR);
sb.append(CR);
sb.append(" @Id").append(CR);
sb.append(" private int id;").append(CR);
sb.append(CR);
sb.append(" @JoinTable(name=\"DEP_EMP\", joinColumns=@JoinColumn(name=\"DEPT_ID\"), inverseJoinColumns=@JoinColumn(name=\"EMP_ID\"))").append(CR);
sb.append(" private Collection<Employee> employees;").append(CR);
sb.append(CR);
sb.append("}").append(CR);
}
};
this.javaProject.createCompilationUnit(PACKAGE_NAME, "Department.java", sourceWriter);
}
private void createTestEmployee() throws Exception {
SourceWriter sourceWriter = new SourceWriter() {
public void appendSourceTo(StringBuilder sb) {
sb.append(CR);
sb.append("import ").append(JPA.ENTITY).append(";");
sb.append(CR);
sb.append("import ").append(JPA.ID).append(";");
sb.append(CR);
sb.append("@Entity");
sb.append(CR);
sb.append("public class ").append("Employee").append(" ");
sb.append("{").append(CR);
sb.append(CR);
sb.append(" @Id").append(CR);
sb.append(" private int empId;").append(CR);
sb.append(CR);
sb.append("}").append(CR);
}
};
this.javaProject.createCompilationUnit(PACKAGE_NAME, "Employee.java", sourceWriter);
}
private void createTestTargetEntityAddress() throws Exception {
SourceWriter sourceWriter = new SourceWriter() {
public void appendSourceTo(StringBuilder sb) {
sb.append(CR);
sb.append("import ");
sb.append(JPA.ENTITY);
sb.append(";");
sb.append(CR);
sb.append("import ");
sb.append(JPA.ID);
sb.append(";");
sb.append(CR);
sb.append(CR);
sb.append("@Entity");
sb.append(CR);
sb.append("public class ").append("Address").append(" ");
sb.append("{").append(CR);
sb.append(CR);
sb.append(" @Id").append(CR);
sb.append(" private int id;").append(CR);
sb.append(CR);
sb.append(" private String city;").append(CR);
sb.append(CR);
sb.append(" private State state;").append(CR);
sb.append(CR);
sb.append(" private int zip;").append(CR);
sb.append(CR);
sb.append("}").append(CR);
}
};
this.javaProject.createCompilationUnit(PACKAGE_NAME, "Address.java", sourceWriter);
}
private void createTestEntityWithOneToManyMapping() throws Exception {
createTestType(new DefaultAnnotationWriter() {
@Override
public Iterator<String> imports() {
return new ArrayIterator<String>(JPA.ENTITY, JPA.ONE_TO_MANY);
}
@Override
public void appendTypeAnnotationTo(StringBuilder sb) {
sb.append("@Entity").append(CR);
}
@Override
public void appendIdFieldAnnotationTo(StringBuilder sb) {
sb.append("@OneToMany").append(CR);
}
});
OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME);
for (OrmReadOnlyPersistentAttribute each : ormPersistentType.getAttributes()) {
each.convertToSpecified();
}
}
private ICompilationUnit createTestEntityWithValidMapOneToManyMapping() throws Exception {
return this.createTestType(new DefaultAnnotationWriter() {
@Override
public Iterator<String> imports() {
return new ArrayIterator<String>(JPA.ENTITY, JPA.ONE_TO_MANY, JPA.ID);
}
@Override
public void appendTypeAnnotationTo(StringBuilder sb) {
sb.append("@Entity").append(CR);
}
@Override
public void appendIdFieldAnnotationTo(StringBuilder sb) {
sb.append(CR);
sb.append(" @OneToMany").append(CR);
sb.append(" private java.util.Map<String, Address> addresses;").append(CR);
sb.append(CR);
sb.append(" @Id").append(CR);
}
});
}
private ICompilationUnit createTestEntityWithValidNonGenericMapOneToManyMapping() throws Exception {
return this.createTestType(new DefaultAnnotationWriter() {
@Override
public Iterator<String> imports() {
return new ArrayIterator<String>(JPA.ENTITY, JPA.ONE_TO_MANY, JPA.ID);
}
@Override
public void appendTypeAnnotationTo(StringBuilder sb) {
sb.append("@Entity").append(CR);
}
@Override
public void appendIdFieldAnnotationTo(StringBuilder sb) {
sb.append(CR);
sb.append(" @OneToMany").append(CR);
sb.append(" private java.util.Map addresses;").append(CR);
sb.append(CR);
sb.append(" @Id").append(CR);
}
});
}
private void createTestEmbeddableState() throws Exception {
SourceWriter sourceWriter = new SourceWriter() {
public void appendSourceTo(StringBuilder sb) {
sb.append(CR);
sb.append("import ");
sb.append(JPA.EMBEDDABLE);
sb.append(";");
sb.append(CR);
sb.append("@Embeddable");
sb.append(CR);
sb.append("public class ").append("State").append(" ");
sb.append("{").append(CR);
sb.append(CR);
sb.append(" private String foo;").append(CR);
sb.append(CR);
sb.append(" private Address address;").append(CR);
sb.append(CR);
sb.append("}").append(CR);
}
};
this.javaProject.createCompilationUnit(PACKAGE_NAME, "State.java", sourceWriter);
}
private ICompilationUnit createTestEntityOneToManyMapping() throws Exception {
return this.createTestType(new DefaultAnnotationWriter() {
@Override
public Iterator<String> imports() {
return new ArrayIterator<String>(JPA.ENTITY, JPA.ONE_TO_MANY, JPA.FETCH_TYPE, JPA.CASCADE_TYPE, JPA.ORDER_BY, EclipseLink.JOIN_FETCH, EclipseLink.JOIN_FETCH_TYPE);
}
@Override
public void appendTypeAnnotationTo(StringBuilder sb) {
sb.append("@Entity");
}
@Override
public void appendIdFieldAnnotationTo(StringBuilder sb) {
sb.append(CR);
sb.append(" @OneToMany(fetch=FetchType.EAGER, targetEntity=Address.class, cascade={CascadeType.ALL, CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE, CascadeType.REFRESH})");
sb.append(CR);
sb.append(" @OrderBy(\"city\"");
sb.append(CR);
sb.append(" @JoinFetch(JoinFetchType.INNER)");
sb.append(" private java.util.Collection<Address> address;").append(CR);
sb.append(CR);
sb.append(" @Id");
}
});
}
public void testUpdatePrivateOwned() throws Exception {
OrmPersistentType ormPersistentType =
getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME);
OrmPersistentAttribute ormPersistentAttribute =
ormPersistentType.addSpecifiedAttribute(MappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY, "oneToMany");
EclipseLinkOneToManyMapping contextOneToMany =
(EclipseLinkOneToManyMapping) ormPersistentAttribute.getMapping();
XmlEntity resourceEntity =
(XmlEntity) getXmlEntityMappings().getEntities().get(0);
XmlOneToMany resourceOneToMany =
(XmlOneToMany) resourceEntity.getAttributes().getOneToManys().get(0);
// check defaults
assertFalse(resourceOneToMany.isPrivateOwned());
assertFalse(contextOneToMany.getPrivateOwned().isPrivateOwned());
// set xml private owned to true, check context
resourceOneToMany.setPrivateOwned(true);
assertTrue(resourceOneToMany.isPrivateOwned());
assertTrue(contextOneToMany.getPrivateOwned().isPrivateOwned());
// set xml private owned back to false, check context
resourceOneToMany.setPrivateOwned(false);
assertFalse(resourceOneToMany.isPrivateOwned());
assertFalse(contextOneToMany.getPrivateOwned().isPrivateOwned());
}
public void testModifyPrivateOwned() throws Exception {
OrmPersistentType ormPersistentType =
getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME);
OrmPersistentAttribute ormPersistentAttribute =
ormPersistentType.addSpecifiedAttribute(MappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY, "oneToMany");
EclipseLinkOneToManyMapping contextOneToMany =
(EclipseLinkOneToManyMapping) ormPersistentAttribute.getMapping();
XmlEntity resourceEntity =
(XmlEntity)getXmlEntityMappings().getEntities().get(0);
XmlOneToMany resourceOneToMany =
(XmlOneToMany) resourceEntity.getAttributes().getOneToManys().get(0);
// check defaults
assertFalse(resourceOneToMany.isPrivateOwned());
assertFalse(contextOneToMany.getPrivateOwned().isPrivateOwned());
// set context private owned to true, check resource
contextOneToMany.getPrivateOwned().setPrivateOwned(true);
assertTrue(resourceOneToMany.isPrivateOwned());
assertTrue(contextOneToMany.getPrivateOwned().isPrivateOwned());
// set context private owned back to false, check resource
contextOneToMany.getPrivateOwned().setPrivateOwned(false);
assertFalse(resourceOneToMany.isPrivateOwned());
assertFalse(contextOneToMany.getPrivateOwned().isPrivateOwned());
}
public void testUpdateJoinFetch() throws Exception {
OrmPersistentType ormPersistentType =
getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME);
OrmPersistentAttribute ormPersistentAttribute =
ormPersistentType.addSpecifiedAttribute(MappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY, "oneToMany");
EclipseLinkOneToManyMapping contextOneToMany =
(EclipseLinkOneToManyMapping) ormPersistentAttribute.getMapping();
XmlEntity resourceEntity =
(XmlEntity) getXmlEntityMappings().getEntities().get(0);
XmlOneToMany resourceOneToMany =
(XmlOneToMany) resourceEntity.getAttributes().getOneToManys().get(0);
// check defaults
assertNull(resourceOneToMany.getJoinFetch());
assertNull(contextOneToMany.getJoinFetch().getValue());
// set xml join fetch to INNER, check context
resourceOneToMany.setJoinFetch(XmlJoinFetchType.INNER);
assertEquals(XmlJoinFetchType.INNER, resourceOneToMany.getJoinFetch());
assertEquals(EclipseLinkJoinFetchType.INNER, contextOneToMany.getJoinFetch().getValue());
// set xml join fetch to OUTER, check context
resourceOneToMany.setJoinFetch(XmlJoinFetchType.OUTER);
assertEquals(XmlJoinFetchType.OUTER, resourceOneToMany.getJoinFetch());
assertEquals(EclipseLinkJoinFetchType.OUTER, contextOneToMany.getJoinFetch().getValue());
// set xml join fetch to null, check context
resourceOneToMany.setJoinFetch(null);
assertNull(resourceOneToMany.getJoinFetch());
assertNull(contextOneToMany.getJoinFetch().getValue());
}
public void testModifyJoinFetch() throws Exception {
OrmPersistentType ormPersistentType =
getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME);
OrmPersistentAttribute ormPersistentAttribute =
ormPersistentType.addSpecifiedAttribute(MappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY, "oneToMany");
EclipseLinkOneToManyMapping contextOneToMany =
(EclipseLinkOneToManyMapping) ormPersistentAttribute.getMapping();
XmlEntity resourceEntity =
(XmlEntity)getXmlEntityMappings().getEntities().get(0);
XmlOneToMany resourceOneToMany =
(XmlOneToMany) resourceEntity.getAttributes().getOneToManys().get(0);
// check defaults
assertNull(resourceOneToMany.getJoinFetch());
assertNull(contextOneToMany.getJoinFetch().getValue());
// set context join fetch to INNER, check resource
contextOneToMany.getJoinFetch().setValue(EclipseLinkJoinFetchType.INNER);
assertEquals(XmlJoinFetchType.INNER, resourceOneToMany.getJoinFetch());
assertEquals(EclipseLinkJoinFetchType.INNER, contextOneToMany.getJoinFetch().getValue());
// set context join fetch to OUTER, check resource
contextOneToMany.getJoinFetch().setValue(EclipseLinkJoinFetchType.OUTER);
assertEquals(XmlJoinFetchType.OUTER, resourceOneToMany.getJoinFetch());
assertEquals(EclipseLinkJoinFetchType.OUTER, contextOneToMany.getJoinFetch().getValue());
// set context join fetch to null, check resource
contextOneToMany.getJoinFetch().setValue(null);
assertNull(resourceOneToMany.getJoinFetch());
assertNull(contextOneToMany.getJoinFetch().getValue());
}
public void testJoinFetchDefault() throws Exception {
createTestEmployee();
createTestDepartment();
getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, PACKAGE_NAME + ".Department");
getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, PACKAGE_NAME + ".Employee");
OrmPersistentType departmentPersistentType = getEntityMappings().getPersistentTypes().iterator().next();
EclipseLinkOneToManyMapping oneToMany = (EclipseLinkOneToManyMapping) departmentPersistentType.getAttributeNamed("employees").getMapping();
assertNull(oneToMany.getJoinFetch().getValue());
getEntityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(true);
oneToMany = (EclipseLinkOneToManyMapping) departmentPersistentType.getAttributeNamed("employees").getMapping();
assertNull(oneToMany.getJoinFetch().getValue());
EclipseLinkRelationshipMapping javaRelationshipMapping = (EclipseLinkRelationshipMapping) departmentPersistentType.getJavaPersistentType().getAttributeNamed("employees").getMapping();
javaRelationshipMapping.getJoinFetch().setValue(EclipseLinkJoinFetchType.OUTER);
assertNull(oneToMany.getJoinFetch().getValue());
getEntityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(false);
oneToMany = (EclipseLinkOneToManyMapping) departmentPersistentType.getAttributeNamed("employees").getMapping();
assertEquals(EclipseLinkJoinFetchType.OUTER, oneToMany.getJoinFetch().getValue());
}
public void testDefaultJoinTable() throws Exception {
createTestEmployee();
createTestDepartment();
getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, PACKAGE_NAME + ".Department");
getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, PACKAGE_NAME + ".Employee");
OrmPersistentType departmentPersistentType = getEntityMappings().getPersistentTypes().iterator().next();
OrmReadOnlyPersistentAttribute attribute = departmentPersistentType.getAttributeNamed("employees");
OneToManyMapping oneToMany = (OneToManyMapping) attribute.getMapping();
assertEquals(true, attribute.isVirtual());
JoinTable ormJoinTable = oneToMany.getRelationship().getJoinTableStrategy().getJoinTable();
assertEquals("DEP_EMP", ormJoinTable.getName());
assertEquals("DEPT_ID", ormJoinTable.getSpecifiedJoinColumns().iterator().next().getName());
assertEquals("id", ormJoinTable.getSpecifiedJoinColumns().iterator().next().getReferencedColumnName());
assertEquals("EMP_ID", ormJoinTable.getSpecifiedInverseJoinColumns().iterator().next().getName());
assertEquals("empId", ormJoinTable.getSpecifiedInverseJoinColumns().iterator().next().getReferencedColumnName());
//set metadata-complete and verify JoinTable info is not taken from the java
departmentPersistentType.getMapping().setSpecifiedMetadataComplete(Boolean.TRUE);
departmentPersistentType.addSpecifiedAttribute(MappingKeys.ID_ATTRIBUTE_MAPPING_KEY, "id");
attribute = departmentPersistentType.getAttributeNamed("employees");
oneToMany = (OneToManyMapping) attribute.getMapping();
assertEquals(true, attribute.isVirtual());
ormJoinTable = oneToMany.getRelationship().getJoinTableStrategy().getJoinTable();
assertEquals("Department_Employee", ormJoinTable.getName());
assertEquals("Department_id", ormJoinTable.getDefaultJoinColumn().getName());
assertEquals("id", ormJoinTable.getDefaultJoinColumn().getReferencedColumnName());
assertEquals("employees_empId", ormJoinTable.getDefaultInverseJoinColumn().getName());
assertEquals("empId", ormJoinTable.getDefaultInverseJoinColumn().getReferencedColumnName());
//set metadata-complete to false, add mapping to orm.xml verify JoinTable info is not taken from the java
departmentPersistentType.getMapping().setSpecifiedMetadataComplete(Boolean.FALSE);
attribute = departmentPersistentType.getAttributeNamed("employees");
oneToMany = (OneToManyMapping) attribute.getMapping();
assertEquals(true, attribute.isVirtual());
ormJoinTable = oneToMany.getRelationship().getJoinTableStrategy().getJoinTable();
assertEquals("DEP_EMP", ormJoinTable.getName());
assertEquals("DEPT_ID", ormJoinTable.getSpecifiedJoinColumns().iterator().next().getName());
assertEquals("id", ormJoinTable.getSpecifiedJoinColumns().iterator().next().getReferencedColumnName());
assertEquals("EMP_ID", ormJoinTable.getSpecifiedInverseJoinColumns().iterator().next().getName());
assertEquals("empId", ormJoinTable.getSpecifiedInverseJoinColumns().iterator().next().getReferencedColumnName());
departmentPersistentType.addSpecifiedAttribute(MappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY, "employees");
attribute = departmentPersistentType.getAttributeNamed("employees");
oneToMany = (OneToManyMapping) attribute.getMapping();
assertEquals(false, attribute.isVirtual());
ormJoinTable = oneToMany.getRelationship().getJoinTableStrategy().getJoinTable();
assertEquals("Department_Employee", ormJoinTable.getName());
assertEquals(0, ormJoinTable.getSpecifiedJoinColumnsSize());
assertEquals("Department_id", ormJoinTable.getDefaultJoinColumn().getName());
assertEquals("id", ormJoinTable.getDefaultJoinColumn().getReferencedColumnName());
assertEquals(0, ormJoinTable.getSpecifiedInverseJoinColumnsSize());
assertEquals("employees_empId", ormJoinTable.getDefaultInverseJoinColumn().getName());
assertEquals("empId", ormJoinTable.getDefaultInverseJoinColumn().getReferencedColumnName());
}
public void testUpdateVirtualMapKey() throws Exception {
createTestEntityWithValidMapOneToManyMapping();
createTestTargetEntityAddress();
createTestEmbeddableState();
OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME);
getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, PACKAGE_NAME + ".Address");
getEntityMappings().addPersistentType(MappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY, PACKAGE_NAME + ".State");
OneToManyMapping ormOneToManyMapping = (OneToManyMapping) ormPersistentType.getAttributeNamed("addresses").getMapping();
JavaOneToManyMapping javaOneToManyMapping = (JavaOneToManyMapping) ormPersistentType.getJavaPersistentType().getAttributeNamed("addresses").getMapping();
assertNull(ormOneToManyMapping.getSpecifiedMapKey());
assertNull(ormOneToManyMapping.getMapKey());
assertFalse(ormOneToManyMapping.isPkMapKey());
assertFalse(ormOneToManyMapping.isCustomMapKey());
assertTrue(ormOneToManyMapping.isNoMapKey());
//set pk mapKey in the java, verify virtual orm mapping updates
javaOneToManyMapping.setPkMapKey(true);
assertEquals("id", ormOneToManyMapping.getMapKey());
assertTrue(ormOneToManyMapping.isPkMapKey());
assertFalse(ormOneToManyMapping.isCustomMapKey());
assertFalse(ormOneToManyMapping.isNoMapKey());
//set custom specified mapKey in the java, verify virtual orm mapping updates
javaOneToManyMapping.setCustomMapKey(true);
javaOneToManyMapping.setSpecifiedMapKey("city");
assertEquals("city", ormOneToManyMapping.getSpecifiedMapKey());
assertEquals("city", ormOneToManyMapping.getMapKey());
assertFalse(ormOneToManyMapping.isPkMapKey());
assertTrue(ormOneToManyMapping.isCustomMapKey());
assertFalse(ormOneToManyMapping.isNoMapKey());
//set metadata complete and verify that the orm model ignores the java annotations
ormPersistentType.getMapping().setSpecifiedMetadataComplete(Boolean.TRUE);
ormOneToManyMapping = (OneToManyMapping) ormPersistentType.getAttributeNamed("addresses").getMapping();
assertEquals(null, ormOneToManyMapping.getSpecifiedMapKey());
assertEquals(null, ormOneToManyMapping.getMapKey());
assertFalse(ormOneToManyMapping.isPkMapKey());
assertFalse(ormOneToManyMapping.isCustomMapKey());
assertTrue(ormOneToManyMapping.isNoMapKey());
}
public void testCandidateMapKeyNames() throws Exception {
createTestEntityWithValidMapOneToManyMapping();
createTestTargetEntityAddress();
createTestEmbeddableState();
OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME);
getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, PACKAGE_NAME + ".Address");
getEntityMappings().addPersistentType(MappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY, PACKAGE_NAME + ".State");
OneToManyMapping oneToManyMapping = (OneToManyMapping) ormPersistentType.getAttributeNamed("addresses").getMapping();
Iterator<String> mapKeyNames =
oneToManyMapping.getCandidateMapKeyNames().iterator();
assertEquals("id", mapKeyNames.next());
assertEquals("city", mapKeyNames.next());
assertEquals("state", mapKeyNames.next());
assertEquals("zip", mapKeyNames.next());
assertFalse(mapKeyNames.hasNext());
}
public void testCandidateMapKeyNames2() throws Exception {
createTestEntityWithValidNonGenericMapOneToManyMapping();
createTestTargetEntityAddress();
createTestEmbeddableState();
OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME);
getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, PACKAGE_NAME + ".Address");
getEntityMappings().addPersistentType(MappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY, PACKAGE_NAME + ".State");
OneToManyMapping ormOneToManyMapping = (OneToManyMapping) ormPersistentType.getAttributeNamed("addresses").getMapping();
JavaOneToManyMapping javaOneToManyMapping = (JavaOneToManyMapping) ormPersistentType.getJavaPersistentType().getAttributeNamed("addresses").getMapping();
Iterator<String> mapKeyNames = ormOneToManyMapping.getCandidateMapKeyNames().iterator();
assertEquals(false, mapKeyNames.hasNext());
javaOneToManyMapping.setSpecifiedTargetEntity("test.Address");
mapKeyNames = ormOneToManyMapping.getCandidateMapKeyNames().iterator();
assertEquals("id", mapKeyNames.next());
assertEquals("city", mapKeyNames.next());
assertEquals("state", mapKeyNames.next());
assertEquals("zip", mapKeyNames.next());
assertFalse(mapKeyNames.hasNext());
ormPersistentType.getAttributeNamed("addresses").convertToSpecified();
ormOneToManyMapping = (OneToManyMapping) ormPersistentType.getAttributeNamed("addresses").getMapping();
mapKeyNames = ormOneToManyMapping.getCandidateMapKeyNames().iterator();
assertEquals(false, mapKeyNames.hasNext());
ormOneToManyMapping.setSpecifiedTargetEntity("test.Address");
mapKeyNames = ormOneToManyMapping.getCandidateMapKeyNames().iterator();
assertEquals("id", mapKeyNames.next());
assertEquals("city", mapKeyNames.next());
assertEquals("state", mapKeyNames.next());
assertEquals("zip", mapKeyNames.next());
assertFalse(mapKeyNames.hasNext());
ormOneToManyMapping.setSpecifiedTargetEntity("String");
mapKeyNames = ormOneToManyMapping.getCandidateMapKeyNames().iterator();
assertEquals(false, mapKeyNames.hasNext());
}
public void testModifyPredominantJoiningStrategy() throws Exception {
createTestEntityWithOneToManyMapping();
OrmPersistentType contextType = getEntityMappings().getPersistentType(FULLY_QUALIFIED_TYPE_NAME);
OrmReadOnlyPersistentAttribute contextAttribute = contextType.getAttributeNamed("id");
OrmEclipseLinkOneToManyMapping contextMapping = (OrmEclipseLinkOneToManyMapping) contextAttribute.getMapping();
EclipseLinkOneToManyRelationship2_0 rel = contextMapping.getRelationship();
XmlEntity resourceEntity = (XmlEntity) contextType.getMapping().getXmlTypeMapping();
XmlOneToMany resourceMapping = (XmlOneToMany) resourceEntity.getAttributes().getOneToManys().get(0);
assertTrue(resourceMapping.getJoinColumns().isEmpty());
assertNull(resourceMapping.getJoinTable());
assertNull(resourceMapping.getMappedBy());
assertFalse(rel.strategyIsJoinColumn());
assertTrue(rel.strategyIsJoinTable());
assertFalse(rel.strategyIsMappedBy());
rel.setStrategyToJoinColumn();
assertFalse(resourceMapping.getJoinColumns().isEmpty());
assertNull(resourceMapping.getJoinTable());
assertNull(resourceMapping.getMappedBy());
assertTrue(rel.strategyIsJoinColumn());
assertFalse(rel.strategyIsJoinTable());
assertFalse(rel.strategyIsMappedBy());
rel.setStrategyToMappedBy();
assertTrue(resourceMapping.getJoinColumns().isEmpty());
assertNull(resourceMapping.getJoinTable());
assertNotNull(resourceMapping.getMappedBy());
assertFalse(rel.strategyIsJoinColumn());
assertFalse(rel.strategyIsJoinTable());
assertTrue(rel.strategyIsMappedBy());
rel.setStrategyToJoinTable();
assertTrue(resourceMapping.getJoinColumns().isEmpty());
assertNull(resourceMapping.getJoinTable());
assertNull(resourceMapping.getMappedBy());
assertFalse(rel.strategyIsJoinColumn());
assertTrue(rel.strategyIsJoinTable());
assertFalse(rel.strategyIsMappedBy());
}
public void testUpdatePredominantJoiningStrategy() throws Exception {
createTestEntityWithOneToManyMapping();
OrmPersistentType contextType = getEntityMappings().getPersistentType(FULLY_QUALIFIED_TYPE_NAME);
OrmReadOnlyPersistentAttribute contextAttribute = contextType.getAttributeNamed("id");
OrmEclipseLinkOneToManyMapping contextMapping = (OrmEclipseLinkOneToManyMapping) contextAttribute.getMapping();
EclipseLinkOneToManyRelationship2_0 rel = contextMapping.getRelationship();
XmlEntity resourceEntity = (XmlEntity) contextType.getMapping().getXmlTypeMapping();
XmlOneToMany resourceMapping = (XmlOneToMany) resourceEntity.getAttributes().getOneToManys().get(0);
assertTrue(resourceMapping.getJoinColumns().isEmpty());
assertNull(resourceMapping.getJoinTable());
assertNull(resourceMapping.getMappedBy());
assertFalse(rel.strategyIsJoinColumn());
assertTrue(rel.strategyIsJoinTable());
assertFalse(rel.strategyIsMappedBy());
resourceMapping.setMappedBy("foo");
getJpaProject().synchronizeContextModel();
assertTrue(resourceMapping.getJoinColumns().isEmpty());
assertNull(resourceMapping.getJoinTable());
assertNotNull(resourceMapping.getMappedBy());
assertFalse(rel.strategyIsJoinColumn());
assertFalse(rel.strategyIsJoinTable());
assertTrue(rel.strategyIsMappedBy());
resourceMapping.setJoinTable(OrmFactory.eINSTANCE.createXmlJoinTable());
getJpaProject().synchronizeContextModel();
assertTrue(resourceMapping.getJoinColumns().isEmpty());
assertNotNull(resourceMapping.getJoinTable());
assertNotNull(resourceMapping.getMappedBy());
assertFalse(rel.strategyIsJoinColumn());
assertFalse(rel.strategyIsJoinTable());
assertTrue(rel.strategyIsMappedBy());
resourceMapping.getJoinColumns().add(OrmFactory.eINSTANCE.createXmlJoinColumn());
getJpaProject().synchronizeContextModel();
assertFalse(resourceMapping.getJoinColumns().isEmpty());
assertNotNull(resourceMapping.getJoinTable());
assertNotNull(resourceMapping.getMappedBy());
assertFalse(rel.strategyIsJoinColumn());
assertFalse(rel.strategyIsJoinTable());
assertTrue(rel.strategyIsMappedBy());
resourceMapping.setMappedBy(null);
getJpaProject().synchronizeContextModel();
assertFalse(resourceMapping.getJoinColumns().isEmpty());
assertNotNull(resourceMapping.getJoinTable());
assertNull(resourceMapping.getMappedBy());
assertTrue(rel.strategyIsJoinColumn());
assertFalse(rel.strategyIsJoinTable());
assertFalse(rel.strategyIsMappedBy());
resourceMapping.setJoinTable(null);
getJpaProject().synchronizeContextModel();
assertFalse(resourceMapping.getJoinColumns().isEmpty());
assertNull(resourceMapping.getJoinTable());
assertNull(resourceMapping.getMappedBy());
assertTrue(rel.strategyIsJoinColumn());
assertFalse(rel.strategyIsJoinTable());
assertFalse(rel.strategyIsMappedBy());
resourceMapping.getJoinColumns().clear();
getJpaProject().synchronizeContextModel();
assertTrue(resourceMapping.getJoinColumns().isEmpty());
assertNull(resourceMapping.getJoinTable());
assertNull(resourceMapping.getMappedBy());
assertFalse(rel.strategyIsJoinColumn());
assertTrue(rel.strategyIsJoinTable());
assertFalse(rel.strategyIsMappedBy());
}
public void testVirtualMappingMetadataCompleteFalse() throws Exception {
createTestEntityOneToManyMapping();
createTestTargetEntityAddress();
OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME);
getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, PACKAGE_NAME + ".Address");
assertEquals(3, ormPersistentType.getVirtualAttributesSize());
OrmReadOnlyPersistentAttribute virtualPersistentAttribute = ormPersistentType.getVirtualAttributes().iterator().next();
EclipseLinkOneToManyMapping virtualOneToManyMapping = (EclipseLinkOneToManyMapping) virtualPersistentAttribute.getMapping();
assertEquals("address", virtualOneToManyMapping.getName());
assertEquals(FetchType.EAGER, virtualOneToManyMapping.getSpecifiedFetch());
assertEquals("Address", virtualOneToManyMapping.getSpecifiedTargetEntity());
assertNull(virtualOneToManyMapping.getRelationship().
getMappedByStrategy().getMappedByAttribute());
Cascade cascade = virtualOneToManyMapping.getCascade();
assertTrue(cascade.isAll());
assertTrue(cascade.isMerge());
assertTrue(cascade.isPersist());
assertTrue(cascade.isRemove());
assertTrue(cascade.isRefresh());
assertTrue(virtualOneToManyMapping.getOrderable().isCustomOrdering());
assertEquals("city", virtualOneToManyMapping.getOrderable().getSpecifiedOrderBy());
assertEquals(EclipseLinkJoinFetchType.INNER, virtualOneToManyMapping.getJoinFetch().getValue());
}
public void testVirtualMappingMetadataCompleteTrue() throws Exception {
createTestEntityOneToManyMapping();
createTestTargetEntityAddress();
OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME);
getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, PACKAGE_NAME + ".Address");
ormPersistentType.getMapping().setSpecifiedMetadataComplete(Boolean.TRUE);
assertEquals(3, ormPersistentType.getVirtualAttributesSize());
OrmReadOnlyPersistentAttribute virtualPersistentAttribute = ormPersistentType.getAttributeNamed("address");
assertEquals(MappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY, virtualPersistentAttribute.getMappingKey());
EclipseLinkOneToManyMapping virtualOneToManyMapping = (EclipseLinkOneToManyMapping) virtualPersistentAttribute.getMapping();
assertEquals("address", virtualOneToManyMapping.getName());
assertEquals(FetchType.LAZY, virtualOneToManyMapping.getFetch());
assertEquals("test.Address", virtualOneToManyMapping.getTargetEntity());
assertNull(virtualOneToManyMapping.getRelationship().getMappedByStrategy().getMappedByAttribute());
Cascade cascade = virtualOneToManyMapping.getCascade();
assertFalse(cascade.isAll());
assertFalse(cascade.isMerge());
assertFalse(cascade.isPersist());
assertFalse(cascade.isRemove());
assertFalse(cascade.isRefresh());
assertTrue(virtualOneToManyMapping.getOrderable().isNoOrdering());
assertEquals(null, virtualOneToManyMapping.getOrderable().getSpecifiedOrderBy());
assertEquals(null, virtualOneToManyMapping.getJoinFetch().getValue());
}
}