DATAJPA-658 - Improve metadata detection for xml based entity mappings.

We now honour purely xml based entity definitions, e.g. via rm.xml.
Previously we only checked the annotations for detecting id properties.

Original pull request: #146.
This commit is contained in:
Thomas Darimont
2015-05-18 17:53:26 +02:00
committed by Oliver Gierke
parent 11fd552da5
commit 3d68d4e8a4
4 changed files with 131 additions and 12 deletions

View File

@@ -35,7 +35,9 @@ import javax.persistence.OneToOne;
import javax.persistence.OrderColumn;
import javax.persistence.Transient;
import javax.persistence.Version;
import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.Metamodel;
import javax.persistence.metamodel.SingularAttribute;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.data.annotation.AccessType.Type;
@@ -54,6 +56,7 @@ import org.springframework.util.Assert;
* {@link JpaPersistentProperty} implementation usind a JPA {@link Metamodel}.
*
* @author Oliver Gierke
* @author Thomas Darimont
* @author Greg Turnquist
* @author Christoph Strobl
* @author Mark Paluch
@@ -94,6 +97,7 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty<JpaPer
private final @Nullable TypeInformation<?> associationTargetType;
private final boolean updateable;
private final JpaMetamodel metamodel;
private final EntityType<?> entityType;
/**
* Creates a new {@link JpaPersistentPropertyImpl}
@@ -110,12 +114,35 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty<JpaPer
Assert.notNull(metamodel, "Metamodel must not be null!");
this.entityType = tryResolveEntityType(metamodel, getOwner().getType());
this.usePropertyAccess = detectPropertyAccess();
this.associationTargetType = detectAssociationTargetType();
this.updateable = detectUpdatability();
this.metamodel = new JpaMetamodel(metamodel);
}
/**
* Return the {@link EntityType} for the given Entity {@link Class} or null if given {@code type} is not a mapped
* entity.
*
* @param metamodel
* @param type
* @return
*/
private EntityType<?> tryResolveEntityType(Metamodel metamodel, Class<?> type) {
EntityType<?> ownerEntityType = null;
for (EntityType<?> entityType : metamodel.getEntities()) {
if (entityType.getJavaType().equals(type)) {
ownerEntityType = entityType;
break;
}
}
return ownerEntityType;
}
/*
* (non-Javadoc)
* @see org.springframework.data.mapping.model.AbstractPersistentProperty#getActualType()
@@ -141,7 +168,27 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty<JpaPer
*/
@Override
public boolean isIdProperty() {
return ID_ANNOTATIONS.stream().anyMatch(it -> isAnnotationPresent(it));
boolean isId ID_ANNOTATIONS.stream().anyMatch(it -> isAnnotationPresent(it));
if (isId) {
return true;
}
if (isIdPropertyCandidateAccordingToMetaModel()) {
SingularAttribute<?, ?> idAttribute = entityType.getId(getType());
if (idAttribute.getName().equals(getName())) {
return true;
}
}
return false;
}
private boolean isIdPropertyCandidateAccordingToMetaModel() {
return entityType != null && entityType.hasSingleIdAttribute()
&& entityType.getIdType().getJavaType().equals(getType());
}
/*

View File

@@ -0,0 +1,45 @@
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jpa.domain.sample;
/**
* This entity serves as a test object for orm.xml configuration and MUST NOT be configured via annotations!
*
* @author Thomas Darimont
*/
public class OrmXmlEntity {
private Long id;
private String property1;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getProperty1() {
return property1;
}
public void setProperty1(String property1) {
this.property1 = property1;
}
}

View File

@@ -32,6 +32,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.ImportResource;
import org.springframework.data.jpa.domain.sample.Category;
import org.springframework.data.jpa.domain.sample.OrmXmlEntity;
import org.springframework.data.jpa.domain.sample.Product;
import org.springframework.data.jpa.domain.sample.User;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@@ -47,6 +48,7 @@ import org.springframework.transaction.support.TransactionTemplate;
* Integration tests for {@link JpaMetamodelMappingContext}.
*
* @author Oliver Gierke
* @author Thomas Darimont
* @since 1.3
*/
@RunWith(SpringJUnit4ClassRunner.class)
@@ -147,4 +149,15 @@ public class JpaMetamodelMappingContextIntegrationTests {
return null;
});
}
/**
* @see DATAJPA-658
*/
@Test
public void shouldDetectIdPropertyForEntityConfiguredViaOrmXmlWithoutAnyAnnotations() {
JpaPersistentEntity<?> entity = context.getPersistentEntity(OrmXmlEntity.class);
assertThat(entity.getIdProperty(), is(notNullValue()));
}
}

View File

@@ -1,19 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd" version="2.0">
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd"
version="2.0">
<persistence-unit-metadata>
<persistence-unit-defaults>
<entity-listeners>
<entity-listener class="org.springframework.data.jpa.domain.support.AuditingEntityListener" />
</entity-listeners>
</persistence-unit-defaults>
</persistence-unit-metadata>
<persistence-unit-metadata>
<persistence-unit-defaults>
<entity-listeners>
<entity-listener
class="org.springframework.data.jpa.domain.support.AuditingEntityListener" />
</entity-listeners>
</persistence-unit-defaults>
</persistence-unit-metadata>
<named-query name="User.findByLastname">
<query>SELECT u FROM User u WHERE u.lastname = ?1</query>
</named-query>
<entity class="org.springframework.data.jpa.domain.sample.Role" access="FIELD" name="ROLE">
<entity class="org.springframework.data.jpa.domain.sample.Role"
access="FIELD" name="ROLE">
<attributes>
<id name="id">
<generated-value strategy="AUTO" />
@@ -21,5 +26,14 @@
<basic name="name" />
</attributes>
</entity>
<entity class="org.springframework.data.jpa.domain.sample.OrmXmlEntity">
<attributes>
<id name="id">
<generated-value strategy="AUTO" />
</id>
<basic name="property1" />
</attributes>
</entity>
</entity-mappings>