| /******************************************************************************* |
| * Copyright (c) 2009, 2013 Oracle. All rights reserved. |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License 2.0, which accompanies this distribution |
| * and is available at https://www.eclipse.org/legal/epl-2.0/. |
| * |
| * Contributors: |
| * Oracle - initial API and implementation |
| ******************************************************************************/ |
| package org.eclipse.jpt.jpa.eclipselink.core.tests.internal.context.java; |
| |
| import java.util.Iterator; |
| import org.eclipse.jdt.core.ICompilationUnit; |
| import org.eclipse.jpt.common.core.resource.java.JavaResourceField; |
| import org.eclipse.jpt.common.core.resource.java.JavaResourceType; |
| import org.eclipse.jpt.common.core.resource.java.JavaResourceAnnotatedElement.AstNodeType; |
| import org.eclipse.jpt.common.utility.internal.iterator.IteratorTools; |
| import org.eclipse.jpt.jpa.core.MappingKeys; |
| import org.eclipse.jpt.jpa.core.context.SpecifiedPersistentAttribute; |
| import org.eclipse.jpt.jpa.core.context.java.JavaSpecifiedPersistentAttribute; |
| import org.eclipse.jpt.jpa.core.context.java.JavaPersistentType; |
| import org.eclipse.jpt.jpa.core.context.orm.OrmPersistentType; |
| import org.eclipse.jpt.jpa.core.context.orm.OrmPersistentAttribute; |
| import org.eclipse.jpt.jpa.core.jpa2.context.ManyToOneMapping2_0; |
| import org.eclipse.jpt.jpa.core.jpa2.context.ManyToOneRelationship2_0; |
| import org.eclipse.jpt.jpa.core.jpa2.context.OneToOneMapping2_0; |
| import org.eclipse.jpt.jpa.core.jpa2.resource.java.JPA2_0; |
| import org.eclipse.jpt.jpa.core.jpa2.resource.java.MapsIdAnnotation2_0; |
| import org.eclipse.jpt.jpa.core.resource.java.JPA; |
| import org.eclipse.jpt.jpa.eclipselink.core.tests.internal.context.EclipseLink2_0ContextModelTestCase; |
| |
| @SuppressWarnings("nls") |
| public class EclipseLink2_0JavaManyToOneMappingTests |
| extends EclipseLink2_0ContextModelTestCase |
| { |
| public EclipseLink2_0JavaManyToOneMappingTests(String name) { |
| super(name); |
| } |
| |
| |
| private void createTestEntity() throws Exception { |
| createTestType(new DefaultAnnotationWriter() { |
| @Override |
| public Iterator<String> imports() { |
| return IteratorTools.iterator(JPA.ENTITY, JPA.MANY_TO_ONE); |
| } |
| |
| @Override |
| public void appendTypeAnnotationTo(StringBuilder sb) { |
| sb.append("@Entity"); |
| sb.append(CR); |
| } |
| |
| @Override |
| public void appendIdFieldAnnotationTo(StringBuilder sb) { |
| sb.append(CR); |
| sb.append(" @ManyToOne"); |
| sb.append(CR); |
| sb.append(" private Address address;"); |
| sb.append(CR); |
| sb.append(CR); |
| sb.append(" @Id"); |
| } |
| }); |
| } |
| |
| private ICompilationUnit createTestEntityWithIdDerivedIdentity() throws Exception { |
| return this.createTestType(new DefaultAnnotationWriter() { |
| @Override |
| public Iterator<String> imports() { |
| return IteratorTools.iterator(JPA.ENTITY, JPA.MANY_TO_ONE, JPA.ID); |
| } |
| @Override |
| public void appendTypeAnnotationTo(StringBuilder sb) { |
| sb.append("@Entity").append(CR); |
| } |
| |
| @Override |
| public void appendIdFieldAnnotationTo(StringBuilder sb) { |
| sb.append(CR); |
| sb.append(" @ManyToOne @Id").append(CR); |
| sb.append(" private " + TYPE_NAME + " manyToOne;").append(CR); |
| sb.append(CR); |
| } |
| }); |
| } |
| |
| private void createTestEntityWithMapsIdDerivedIdentity() throws Exception { |
| createTestType(new DefaultAnnotationWriter() { |
| @Override |
| public Iterator<String> imports() { |
| return IteratorTools.iterator(JPA.ENTITY, JPA.MANY_TO_ONE, JPA2_0.MAPS_ID); |
| } |
| @Override |
| public void appendTypeAnnotationTo(StringBuilder sb) { |
| sb.append("@Entity").append(CR); |
| } |
| |
| @Override |
| public void appendIdFieldAnnotationTo(StringBuilder sb) { |
| sb.append(CR); |
| sb.append(" @ManyToOne @MapsId").append(CR); |
| sb.append(" private " + TYPE_NAME + " manyToOne;").append(CR); |
| sb.append(CR); |
| } |
| }); |
| OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); |
| for (OrmPersistentAttribute each : ormPersistentType.getAttributes()) { |
| each.addToXml(); |
| } |
| } |
| |
| public void testUpdateDerivedId() throws Exception { |
| createTestEntityWithIdDerivedIdentity(); |
| addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); |
| JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, AstNodeType.TYPE); |
| JavaResourceField resourceField = resourceType.getFields().iterator().next(); |
| JavaPersistentType contextType = getJavaPersistentType(); |
| JavaSpecifiedPersistentAttribute contextAttribute = contextType.getAttributeNamed("manyToOne"); |
| ManyToOneMapping2_0 contextMapping = (ManyToOneMapping2_0) contextAttribute.getMapping(); |
| |
| assertNotNull(resourceField.getAnnotation(JPA.ID)); |
| assertTrue(contextMapping.getDerivedIdentity().getIdDerivedIdentityStrategy().getValue()); |
| |
| resourceField.removeAnnotation(JPA.ID); |
| getJpaProject().synchronizeContextModel(); |
| assertNull(resourceField.getAnnotation(JPA.ID)); |
| assertFalse(contextMapping.getDerivedIdentity().getIdDerivedIdentityStrategy().getValue()); |
| |
| resourceField.addAnnotation(JPA.ID); |
| getJpaProject().synchronizeContextModel(); |
| assertNotNull(resourceField.getAnnotation(JPA.ID)); |
| assertTrue(contextMapping.getDerivedIdentity().getIdDerivedIdentityStrategy().getValue()); |
| } |
| |
| public void testSetDerivedId() throws Exception { |
| createTestEntityWithIdDerivedIdentity(); |
| addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); |
| JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, AstNodeType.TYPE); |
| JavaResourceField resourceField = resourceType.getFields().iterator().next(); |
| JavaPersistentType contextType = getJavaPersistentType(); |
| JavaSpecifiedPersistentAttribute contextAttribute = contextType.getAttributeNamed("manyToOne"); |
| ManyToOneMapping2_0 contextMapping = (ManyToOneMapping2_0) contextAttribute.getMapping(); |
| |
| assertNotNull(resourceField.getAnnotation(JPA.ID)); |
| assertTrue(contextMapping.getDerivedIdentity().getIdDerivedIdentityStrategy().getValue()); |
| |
| contextMapping.getDerivedIdentity().getIdDerivedIdentityStrategy().setValue(false); |
| assertNull(resourceField.getAnnotation(JPA.ID)); |
| assertFalse(contextMapping.getDerivedIdentity().getIdDerivedIdentityStrategy().getValue()); |
| |
| contextMapping.getDerivedIdentity().getIdDerivedIdentityStrategy().setValue(true); |
| assertNotNull(resourceField.getAnnotation(JPA.ID)); |
| assertTrue(contextMapping.getDerivedIdentity().getIdDerivedIdentityStrategy().getValue()); |
| } |
| |
| public void testUpdateMapsId() throws Exception { |
| createTestEntityWithMapsIdDerivedIdentity(); |
| addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); |
| JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, AstNodeType.TYPE); |
| JavaResourceField resourceField = resourceType.getFields().iterator().next(); |
| JavaPersistentType contextType = getJavaPersistentType(); |
| JavaSpecifiedPersistentAttribute contextAttribute = contextType.getAttributeNamed("manyToOne"); |
| ManyToOneMapping2_0 contextMapping = (ManyToOneMapping2_0) contextAttribute.getMapping(); |
| |
| MapsIdAnnotation2_0 annotation = |
| (MapsIdAnnotation2_0) resourceField.getAnnotation(JPA2_0.MAPS_ID); |
| annotation.setValue("foo"); |
| getJpaProject().synchronizeContextModel(); |
| assertEquals("foo", annotation.getValue()); |
| assertEquals("foo", contextMapping.getDerivedIdentity().getMapsIdDerivedIdentityStrategy().getSpecifiedIdAttributeName()); |
| |
| annotation.setValue("bar"); |
| getJpaProject().synchronizeContextModel(); |
| assertEquals("bar", annotation.getValue()); |
| assertEquals("bar", contextMapping.getDerivedIdentity().getMapsIdDerivedIdentityStrategy().getSpecifiedIdAttributeName()); |
| |
| resourceField.removeAnnotation(JPA2_0.MAPS_ID); |
| getJpaProject().synchronizeContextModel(); |
| assertNull(resourceField.getAnnotation(JPA2_0.MAPS_ID)); |
| assertNull(contextMapping.getDerivedIdentity().getMapsIdDerivedIdentityStrategy().getSpecifiedIdAttributeName()); |
| } |
| |
| public void testSetMapsId() throws Exception { |
| createTestEntity(); |
| addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); |
| JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, AstNodeType.TYPE); |
| JavaResourceField resourceField = resourceType.getFields().iterator().next(); |
| JavaPersistentType contextType = getJavaPersistentType(); |
| JavaSpecifiedPersistentAttribute contextAttribute = contextType.getAttributeNamed("address"); |
| ManyToOneMapping2_0 contextMapping = (ManyToOneMapping2_0) contextAttribute.getMapping(); |
| |
| assertNull(resourceField.getAnnotation(JPA2_0.MAPS_ID)); |
| assertNull(contextMapping.getDerivedIdentity().getMapsIdDerivedIdentityStrategy().getSpecifiedIdAttributeName()); |
| |
| contextMapping.getDerivedIdentity().getMapsIdDerivedIdentityStrategy().setSpecifiedIdAttributeName("foo"); |
| MapsIdAnnotation2_0 annotation = |
| (MapsIdAnnotation2_0) resourceField.getAnnotation(JPA2_0.MAPS_ID); |
| assertNotNull(annotation); |
| assertEquals("foo", annotation.getValue()); |
| assertEquals("foo", contextMapping.getDerivedIdentity().getMapsIdDerivedIdentityStrategy().getSpecifiedIdAttributeName()); |
| |
| contextMapping.getDerivedIdentity().getMapsIdDerivedIdentityStrategy().setSpecifiedIdAttributeName("bar"); |
| assertEquals("bar", annotation.getValue()); |
| assertEquals("bar", contextMapping.getDerivedIdentity().getMapsIdDerivedIdentityStrategy().getSpecifiedIdAttributeName()); |
| |
| contextMapping.getDerivedIdentity().getMapsIdDerivedIdentityStrategy().setSpecifiedIdAttributeName(null); |
| assertNotNull(resourceField.getAnnotation(JPA2_0.MAPS_ID)); |
| assertNull(contextMapping.getDerivedIdentity().getMapsIdDerivedIdentityStrategy().getSpecifiedIdAttributeName()); |
| } |
| |
| public void testMorphMapping() throws Exception { |
| createTestEntityWithMapsIdDerivedIdentity(); |
| addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); |
| JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, AstNodeType.TYPE); |
| JavaResourceField resourceField = resourceType.getFields().iterator().next(); |
| JavaPersistentType contextType = getJavaPersistentType(); |
| JavaSpecifiedPersistentAttribute contextAttribute = contextType.getAttributeNamed("manyToOne"); |
| |
| ((MapsIdAnnotation2_0) resourceField.getAnnotation(JPA2_0.MAPS_ID)).setValue("foo"); |
| getJpaProject().synchronizeContextModel(); |
| |
| assertNull(resourceField.getAnnotation(JPA.ID)); |
| assertFalse(((ManyToOneMapping2_0) contextAttribute.getMapping()). |
| getDerivedIdentity().getIdDerivedIdentityStrategy().getValue()); |
| assertNotNull(resourceField.getAnnotation(JPA2_0.MAPS_ID)); |
| assertEquals("foo", ((MapsIdAnnotation2_0) resourceField.getAnnotation(JPA2_0.MAPS_ID)).getValue()); |
| assertEquals("foo", ((ManyToOneMapping2_0) contextAttribute.getMapping()). |
| getDerivedIdentity().getMapsIdDerivedIdentityStrategy().getSpecifiedIdAttributeName()); |
| |
| contextAttribute.setMappingKey(MappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY); |
| assertFalse(((OneToOneMapping2_0) contextAttribute.getMapping()). |
| getDerivedIdentity().getIdDerivedIdentityStrategy().getValue()); |
| assertNotNull(resourceField.getAnnotation(JPA2_0.MAPS_ID)); |
| assertEquals("foo", ((MapsIdAnnotation2_0) resourceField.getAnnotation(JPA2_0.MAPS_ID)).getValue()); |
| assertEquals("foo", ((OneToOneMapping2_0) contextAttribute.getMapping()). |
| getDerivedIdentity().getMapsIdDerivedIdentityStrategy().getSpecifiedIdAttributeName()); |
| |
| contextAttribute.setMappingKey(MappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY); |
| assertFalse(((ManyToOneMapping2_0) contextAttribute.getMapping()). |
| getDerivedIdentity().getIdDerivedIdentityStrategy().getValue()); |
| assertNotNull(resourceField.getAnnotation(JPA2_0.MAPS_ID)); |
| assertEquals("foo", ((MapsIdAnnotation2_0) resourceField.getAnnotation(JPA2_0.MAPS_ID)).getValue()); |
| assertEquals("foo", ((ManyToOneMapping2_0) contextAttribute.getMapping()). |
| getDerivedIdentity().getMapsIdDerivedIdentityStrategy().getSpecifiedIdAttributeName()); |
| } |
| |
| public void testModifyPredominantJoiningStrategy() throws Exception { |
| createTestEntity(); |
| addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); |
| |
| JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, AstNodeType.TYPE); |
| JavaResourceField resourceField = resourceType.getFields().iterator().next(); |
| SpecifiedPersistentAttribute contextAttribute = getJavaPersistentType().getAttributes().iterator().next(); |
| ManyToOneMapping2_0 mapping = (ManyToOneMapping2_0) contextAttribute.getMapping(); |
| ManyToOneRelationship2_0 rel = (ManyToOneRelationship2_0) mapping.getRelationship(); |
| |
| assertEquals(0, resourceField.getAnnotationsSize(JPA.JOIN_COLUMN)); |
| assertNull(resourceField.getAnnotation(JPA.JOIN_TABLE)); |
| assertTrue(rel.strategyIsJoinColumn()); |
| assertFalse(rel.strategyIsJoinTable()); |
| |
| rel.setStrategyToJoinColumn(); |
| assertEquals(0, resourceField.getAnnotationsSize(JPA.JOIN_COLUMN)); |
| assertNull(resourceField.getAnnotation(JPA.JOIN_TABLE)); |
| assertTrue(rel.strategyIsJoinColumn()); |
| assertFalse(rel.strategyIsJoinTable()); |
| |
| rel.setStrategyToJoinTable(); |
| assertEquals(0, resourceField.getAnnotationsSize(JPA.JOIN_COLUMN)); |
| assertNotNull(resourceField.getAnnotation(JPA.JOIN_TABLE)); |
| assertFalse(rel.strategyIsJoinColumn()); |
| assertTrue(rel.strategyIsJoinTable()); |
| |
| rel.setStrategyToJoinColumn(); |
| assertEquals(0, resourceField.getAnnotationsSize(JPA.JOIN_COLUMN)); |
| assertNull(resourceField.getAnnotation(JPA.JOIN_TABLE)); |
| assertTrue(rel.strategyIsJoinColumn()); |
| assertFalse(rel.strategyIsJoinTable()); |
| } |
| |
| public void testUpdatePredominantJoiningStrategy() throws Exception { |
| createTestEntity(); |
| addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); |
| |
| JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, AstNodeType.TYPE); |
| JavaResourceField resourceField = resourceType.getFields().iterator().next(); |
| SpecifiedPersistentAttribute contextAttribute = getJavaPersistentType().getAttributes().iterator().next(); |
| ManyToOneMapping2_0 mapping = (ManyToOneMapping2_0) contextAttribute.getMapping(); |
| ManyToOneRelationship2_0 rel = (ManyToOneRelationship2_0) mapping.getRelationship(); |
| |
| assertEquals(0, resourceField.getAnnotationsSize(JPA.JOIN_COLUMN)); |
| assertNull(resourceField.getAnnotation(JPA.JOIN_TABLE)); |
| assertTrue(rel.strategyIsJoinColumn()); |
| assertFalse(rel.strategyIsJoinTable()); |
| |
| resourceField.addAnnotation(0, JPA.JOIN_COLUMN); |
| getJpaProject().synchronizeContextModel(); |
| assertNotNull(resourceField.getAnnotation(0, JPA.JOIN_COLUMN)); |
| assertNull(resourceField.getAnnotation(JPA.JOIN_TABLE)); |
| assertTrue(rel.strategyIsJoinColumn()); |
| assertFalse(rel.strategyIsJoinTable()); |
| |
| resourceField.addAnnotation(JPA.JOIN_TABLE); |
| getJpaProject().synchronizeContextModel(); |
| assertNotNull(resourceField.getAnnotation(0, JPA.JOIN_COLUMN)); |
| assertNotNull(resourceField.getAnnotation(JPA.JOIN_TABLE)); |
| assertFalse(rel.strategyIsJoinColumn()); |
| assertTrue(rel.strategyIsJoinTable()); |
| |
| resourceField.removeAnnotation(0, JPA.JOIN_COLUMN); |
| getJpaProject().synchronizeContextModel(); |
| assertEquals(0, resourceField.getAnnotationsSize(JPA.JOIN_COLUMN)); |
| assertNotNull(resourceField.getAnnotation(JPA.JOIN_TABLE)); |
| assertFalse(rel.strategyIsJoinColumn()); |
| assertTrue(rel.strategyIsJoinTable()); |
| |
| resourceField.removeAnnotation(JPA.JOIN_TABLE); |
| getJpaProject().synchronizeContextModel(); |
| assertEquals(0, resourceField.getAnnotationsSize(JPA.JOIN_COLUMN)); |
| assertNull(resourceField.getAnnotation(JPA.JOIN_TABLE)); |
| assertTrue(rel.strategyIsJoinColumn()); |
| assertFalse(rel.strategyIsJoinTable()); |
| } |
| } |