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:
committed by
Oliver Gierke
parent
cdb0541208
commit
8627cdf300
@@ -37,7 +37,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
|
||||
* @since 1.3
|
||||
*/
|
||||
@@ -92,6 +95,7 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty<JpaPer
|
||||
private final TypeInformation<?> associationTargetType;
|
||||
private final boolean updateable;
|
||||
private final JpaMetamodel metamodel;
|
||||
private final EntityType<?> entityType;
|
||||
|
||||
/**
|
||||
* Creates a new {@link JpaPersistentPropertyImpl}
|
||||
@@ -109,13 +113,36 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty<JpaPer
|
||||
|
||||
Assert.notNull(metamodel, "Metamodel must not be null!");
|
||||
|
||||
this.entityType = tryResolveEntityType(metamodel, getOwner().getType());
|
||||
this.usePropertyAccess = detectPropertyAccess();
|
||||
this.associationTargetType = isAssociation() ? detectAssociationTargetType() : null;
|
||||
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()
|
||||
*/
|
||||
@@ -147,9 +174,23 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty<JpaPer
|
||||
}
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.mapping.model.AbstractPersistentProperty#isEntity()
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
@@ -49,6 +50,7 @@ import org.springframework.transaction.support.TransactionTemplate;
|
||||
* Integration tests for {@link JpaMetamodelMappingContext}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
* @since 1.3
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@@ -157,4 +159,15 @@ public class JpaMetamodelMappingContextIntegrationTests {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAJPA-658
|
||||
*/
|
||||
@Test
|
||||
public void shouldDetectIdPropertyForEntityConfiguredViaOrmXmlWithoutAnyAnnotations() {
|
||||
|
||||
JpaPersistentEntity<?> entity = context.getPersistentEntity(OrmXmlEntity.class);
|
||||
|
||||
assertThat(entity.getIdProperty(), is(notNullValue()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user