DATAJPA-630 - Add IdentifierAccessor implementation that avoids proxy resolution for id lookups.
We now customize the IdentifierAccessor returned from JpaPersistentEntity.getIdentifierAccessor(…) to allow the identifier lookup use persistence provider specific means. This is needed to make sure we lookup identifiers for proxies correctly for which a field value lookup would not succeed (as the providers do not pre-populate the identifier field).
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
package org.springframework.data.jpa.domain.sample;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
@Entity
|
||||
public class Category {
|
||||
|
||||
@Id @GeneratedValue private Long id;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)//
|
||||
private Product product;
|
||||
|
||||
public Category(Product product) {
|
||||
this.product = product;
|
||||
}
|
||||
|
||||
protected Category() {}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public Product getProduct() {
|
||||
return product;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package org.springframework.data.jpa.domain.sample;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Product {
|
||||
|
||||
@Id @GeneratedValue private Long id;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012 the original author or authors.
|
||||
* Copyright 2012-2014 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.
|
||||
@@ -18,15 +18,32 @@ package org.springframework.data.jpa.mapping;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import java.util.Collections;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
import org.hibernate.proxy.HibernateProxy;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.ComponentScan.Filter;
|
||||
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.Product;
|
||||
import org.springframework.data.jpa.domain.sample.User;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
import org.springframework.data.jpa.repository.sample.CategoryRepository;
|
||||
import org.springframework.data.jpa.repository.sample.ProductRepository;
|
||||
import org.springframework.data.mapping.IdentifierAccessor;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.TransactionStatus;
|
||||
import org.springframework.transaction.support.TransactionCallback;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link JpaMetamodelMappingContext}.
|
||||
@@ -35,16 +52,28 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
* @since 1.3
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration("classpath:infrastructure.xml")
|
||||
@ContextConfiguration
|
||||
public class JpaMetamodelMappingContextIntegrationTests {
|
||||
|
||||
@Configuration
|
||||
@ImportResource("classpath:infrastructure.xml")
|
||||
@EnableJpaRepositories(basePackageClasses = CategoryRepository.class,//
|
||||
includeFilters = @Filter(value = { CategoryRepository.class, ProductRepository.class },
|
||||
type = FilterType.ASSIGNABLE_TYPE))
|
||||
static class Config {
|
||||
|
||||
}
|
||||
|
||||
JpaMetamodelMappingContext context;
|
||||
|
||||
@PersistenceContext EntityManager em;
|
||||
@Autowired ProductRepository products;
|
||||
@Autowired CategoryRepository categories;
|
||||
@Autowired EntityManager em;
|
||||
@Autowired PlatformTransactionManager transactionManager;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
context = new JpaMetamodelMappingContext(em.getMetamodel());
|
||||
context = new JpaMetamodelMappingContext(Collections.singleton(em.getMetamodel()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -95,4 +124,36 @@ public class JpaMetamodelMappingContextIntegrationTests {
|
||||
|
||||
assertThat(entity.getPersistentProperty("colleagues").isEntity(), is(true));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAJPA-630
|
||||
*/
|
||||
@Test
|
||||
public void lookingUpIdentifierOfProxyDoesNotInitializeProxy() {
|
||||
|
||||
new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
|
||||
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
|
||||
Product product = products.save(new Product());
|
||||
Category category = categories.save(new Category(product));
|
||||
em.clear();
|
||||
|
||||
Category loaded = categories.findOne(category.getId());
|
||||
Product loadedProduct = loaded.getProduct();
|
||||
|
||||
JpaPersistentEntity<?> entity = context.getPersistentEntity(Product.class);
|
||||
IdentifierAccessor accessor = entity.getIdentifierAccessor(loadedProduct);
|
||||
|
||||
assertThat(accessor.getIdentifier(), is((Object) product.getId()));
|
||||
assertThat(loadedProduct, is(instanceOf(HibernateProxy.class)));
|
||||
assertThat(((HibernateProxy) loadedProduct).getHibernateLazyInitializer().isUninitialized(), is(true));
|
||||
|
||||
status.setRollbackOnly();
|
||||
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,8 @@ package org.springframework.data.jpa.mapping;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import javax.persistence.Access;
|
||||
import javax.persistence.AccessType;
|
||||
import javax.persistence.Embeddable;
|
||||
@@ -50,7 +52,7 @@ public class JpaPersistentPropertyImplUnitTests {
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
context = new JpaMetamodelMappingContext(model);
|
||||
context = new JpaMetamodelMappingContext(Collections.singleton(model));
|
||||
entity = context.getPersistentEntity(Sample.class);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* Copyright 2014 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.provider;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.ComponentScan.Filter;
|
||||
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.Product;
|
||||
import org.springframework.data.jpa.provider.PersistenceProvider;
|
||||
import org.springframework.data.jpa.provider.ProxyIdAccessor;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
import org.springframework.data.jpa.repository.sample.CategoryRepository;
|
||||
import org.springframework.data.jpa.repository.sample.ProductRepository;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.TransactionStatus;
|
||||
import org.springframework.transaction.support.TransactionCallback;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link PersistenceProvider}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration
|
||||
public class PersistenceProviderIntegrationTests {
|
||||
|
||||
@Configuration
|
||||
@ImportResource("classpath:infrastructure.xml")
|
||||
@EnableJpaRepositories(basePackageClasses = CategoryRepository.class,//
|
||||
includeFilters = @Filter(value = { CategoryRepository.class, ProductRepository.class },
|
||||
type = FilterType.ASSIGNABLE_TYPE))
|
||||
static class Config {
|
||||
|
||||
}
|
||||
|
||||
@Autowired CategoryRepository categories;
|
||||
@Autowired ProductRepository products;
|
||||
@Autowired PlatformTransactionManager transactionManager;
|
||||
@Autowired EntityManager em;
|
||||
|
||||
Product product;
|
||||
Category category;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
this.product = products.save(new Product());
|
||||
this.category = categories.save(new Category(product));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAJPA-630
|
||||
*/
|
||||
@Test
|
||||
public void testname() {
|
||||
|
||||
new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
|
||||
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
|
||||
Product product = categories.findOne(category.getId()).getProduct();
|
||||
ProxyIdAccessor accessor = PersistenceProvider.fromEntityManager(em);
|
||||
|
||||
assertThat(accessor.shouldUseAccessorFor(product), is(true));
|
||||
assertThat(accessor.getIdentifierFrom(product).toString(), is((Object) product.getId().toString()));
|
||||
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -13,10 +13,12 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.jpa.repository.support;
|
||||
package org.springframework.data.jpa.provider;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.data.jpa.provider.PersistenceProvider.*;
|
||||
import static org.springframework.data.jpa.provider.PersistenceProvider.Constants.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -28,6 +30,7 @@ import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.asm.ClassWriter;
|
||||
import org.springframework.asm.Opcodes;
|
||||
import org.springframework.data.jpa.provider.PersistenceProvider;
|
||||
import org.springframework.instrument.classloading.ShadowingClassLoader;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
@@ -35,27 +38,28 @@ import org.springframework.util.ClassUtils;
|
||||
* Tests for PersistenceProvider detection logic in {@link PersistenceProvider}.
|
||||
*
|
||||
* @author Thomas Darimont
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class PersistenceProviderTests {
|
||||
public class PersistenceProviderUnitTests {
|
||||
|
||||
private ShadowingClassLoader shadowingClassLoader;
|
||||
ShadowingClassLoader shadowingClassLoader;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
shadowingClassLoader = new ShadowingClassLoader(getClass().getClassLoader());
|
||||
this.shadowingClassLoader = new ShadowingClassLoader(getClass().getClassLoader());
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAJPA-444
|
||||
*/
|
||||
@Test
|
||||
public void detectsHibernatePersistenceProviderForHibernateVersionLessThan4dot3() throws Exception {
|
||||
public void detectsHibernatePersistenceProviderForHibernateVersionLessThan4Dot3() throws Exception {
|
||||
|
||||
shadowingClassLoader.excludePackage("org.hibernate");
|
||||
|
||||
EntityManager em = mockProviderSpecificEntityManagerInterface(PersistenceProvider.Constants.HIBERNATE_ENTITY_MANAGER_INTERFACE);
|
||||
EntityManager em = mockProviderSpecificEntityManagerInterface(HIBERNATE_ENTITY_MANAGER_INTERFACE);
|
||||
|
||||
assertThat(PersistenceProvider.fromEntityManager(em), is(PersistenceProvider.HIBERNATE));
|
||||
assertThat(fromEntityManager(em), is(HIBERNATE));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -66,9 +70,9 @@ public class PersistenceProviderTests {
|
||||
|
||||
shadowingClassLoader.excludePackage("org.hibernate");
|
||||
|
||||
EntityManager em = mockProviderSpecificEntityManagerInterface(PersistenceProvider.Constants.HIBERNATE43_ENTITY_MANAGER_INTERFACE);
|
||||
EntityManager em = mockProviderSpecificEntityManagerInterface(HIBERNATE43_ENTITY_MANAGER_INTERFACE);
|
||||
|
||||
assertThat(PersistenceProvider.fromEntityManager(em), is(PersistenceProvider.HIBERNATE));
|
||||
assertThat(fromEntityManager(em), is(HIBERNATE));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -76,9 +80,9 @@ public class PersistenceProviderTests {
|
||||
|
||||
shadowingClassLoader.excludePackage("org.apache.openjpa.persistence");
|
||||
|
||||
EntityManager em = mockProviderSpecificEntityManagerInterface(PersistenceProvider.Constants.OPENJPA_ENTITY_MANAGER_INTERFACE);
|
||||
EntityManager em = mockProviderSpecificEntityManagerInterface(OPENJPA_ENTITY_MANAGER_INTERFACE);
|
||||
|
||||
assertThat(PersistenceProvider.fromEntityManager(em), is(PersistenceProvider.OPEN_JPA));
|
||||
assertThat(fromEntityManager(em), is(OPEN_JPA));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -86,9 +90,9 @@ public class PersistenceProviderTests {
|
||||
|
||||
shadowingClassLoader.excludePackage("org.eclipse.persistence.jpa");
|
||||
|
||||
EntityManager em = mockProviderSpecificEntityManagerInterface(PersistenceProvider.Constants.ECLIPSELINK_ENTITY_MANAGER_INTERFACE);
|
||||
EntityManager em = mockProviderSpecificEntityManagerInterface(ECLIPSELINK_ENTITY_MANAGER_INTERFACE);
|
||||
|
||||
assertThat(PersistenceProvider.fromEntityManager(em), is(PersistenceProvider.ECLIPSELINK));
|
||||
assertThat(fromEntityManager(em), is(ECLIPSELINK));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -96,7 +100,7 @@ public class PersistenceProviderTests {
|
||||
|
||||
EntityManager em = mockProviderSpecificEntityManagerInterface("foo.bar.unknown.jpa.JpaEntityManager");
|
||||
|
||||
assertThat(PersistenceProvider.fromEntityManager(em), is(PersistenceProvider.GENERIC_JPA));
|
||||
assertThat(fromEntityManager(em), is(GENERIC_JPA));
|
||||
}
|
||||
|
||||
private EntityManager mockProviderSpecificEntityManagerInterface(String interfaceName) throws ClassNotFoundException {
|
||||
@@ -48,7 +48,7 @@ import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class CrudMethodMetadataIntegrationTests {
|
||||
public class CrudMethodMetadataUnitTests {
|
||||
|
||||
@Mock EntityManager em;
|
||||
@Mock EntityManagerFactory emf;
|
||||
@@ -65,6 +65,7 @@ public class CrudMethodMetadataIntegrationTests {
|
||||
|
||||
when(information.getJavaType()).thenReturn(Role.class);
|
||||
|
||||
when(em.getDelegate()).thenReturn(em);
|
||||
when(em.getEntityManagerFactory()).thenReturn(emf);
|
||||
when(emf.createEntityManager()).thenReturn(em);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
* Copyright 2012-2014 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.
|
||||
@@ -22,7 +22,6 @@ import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.config.PropertiesFactoryBean;
|
||||
|
||||
@@ -34,11 +34,12 @@ import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.data.jpa.domain.sample.User;
|
||||
import org.springframework.data.jpa.provider.PersistenceProvider;
|
||||
import org.springframework.data.jpa.provider.QueryExtractor;
|
||||
import org.springframework.data.jpa.repository.EntityGraph;
|
||||
import org.springframework.data.jpa.repository.EntityGraph.EntityGraphType;
|
||||
import org.springframework.data.jpa.repository.Lock;
|
||||
import org.springframework.data.jpa.repository.QueryHints;
|
||||
import org.springframework.data.jpa.repository.support.PersistenceProvider;
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
@@ -35,6 +35,7 @@ import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.domain.sample.User;
|
||||
import org.springframework.data.jpa.provider.QueryExtractor;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.data.repository.core.NamedQueries;
|
||||
|
||||
@@ -36,6 +36,7 @@ import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.jpa.domain.sample.User;
|
||||
import org.springframework.data.jpa.provider.QueryExtractor;
|
||||
import org.springframework.data.jpa.repository.EntityGraph;
|
||||
import org.springframework.data.jpa.repository.EntityGraph.EntityGraphType;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
@@ -30,6 +30,7 @@ import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.provider.QueryExtractor;
|
||||
import org.springframework.data.repository.core.RepositoryMetadata;
|
||||
import org.springframework.data.repository.query.QueryCreationException;
|
||||
|
||||
|
||||
@@ -42,8 +42,8 @@ import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.domain.sample.User;
|
||||
import org.springframework.data.jpa.provider.PersistenceProvider;
|
||||
import org.springframework.data.jpa.repository.Temporal;
|
||||
import org.springframework.data.jpa.repository.support.PersistenceProvider;
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
@@ -40,6 +40,7 @@ import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.jpa.domain.sample.User;
|
||||
import org.springframework.data.jpa.provider.QueryExtractor;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.jpa.repository.sample.UserRepository;
|
||||
import org.springframework.data.repository.core.RepositoryMetadata;
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright 2014 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.repository.sample;
|
||||
|
||||
import org.springframework.data.jpa.domain.sample.Category;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public interface CategoryRepository extends CrudRepository<Category, Long> {}
|
||||
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright 2014 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.repository.sample;
|
||||
|
||||
import org.springframework.data.jpa.domain.sample.Product;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public interface ProductRepository extends JpaRepository<Product, Long> {
|
||||
|
||||
}
|
||||
@@ -38,7 +38,8 @@ public class EclipseLinkJpaMetamodelEntityInformationIntegrationTests extends
|
||||
*/
|
||||
@Test
|
||||
public void reactivatedDetectsIdTypeForMappedSuperclass() {
|
||||
JpaEntityInformation<?, ?> information = JpaEntityInformationSupport.getMetadata(AbstractPersistable.class, em);
|
||||
JpaEntityInformation<?, ?> information = JpaEntityInformationSupport.getEntityInformation(
|
||||
AbstractPersistable.class, em);
|
||||
assertEquals(String.class, information.getIdType());
|
||||
}
|
||||
|
||||
@@ -56,10 +57,6 @@ public class EclipseLinkJpaMetamodelEntityInformationIntegrationTests extends
|
||||
@Ignore
|
||||
public void detectsNewStateForEntityWithPrimitiveId() {}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformationIntegrationTests#considersEntityWithUnSetCompundIdNew()
|
||||
*/
|
||||
@Override
|
||||
@Ignore
|
||||
public void considersEntityWithUnsetCompundIdNew() {}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright 2014 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.repository.support;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.ImportResource;
|
||||
import org.springframework.data.jpa.provider.PersistenceProviderIntegrationTests;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@ContextConfiguration(classes = EclipseLinkProxyIdAccessorTests.EclipseLinkConfig.class)
|
||||
public class EclipseLinkProxyIdAccessorTests extends PersistenceProviderIntegrationTests {
|
||||
|
||||
@Configuration
|
||||
@ImportResource("classpath:eclipselink.xml")
|
||||
static class EclipseLinkConfig {}
|
||||
|
||||
/**
|
||||
* Do not execute the test as EclipseLink does not create a lazy-loading proxy as expected.
|
||||
*/
|
||||
@Override
|
||||
@Ignore
|
||||
public void testname() {}
|
||||
}
|
||||
@@ -61,7 +61,7 @@ public class JpaEntityInformationSupportUnitTests {
|
||||
public void rejectsClassNotBeingFoundInMetamodel() {
|
||||
|
||||
when(em.getMetamodel()).thenReturn(metaModel);
|
||||
JpaEntityInformationSupport.getMetadata(User.class, em);
|
||||
JpaEntityInformationSupport.getEntityInformation(User.class, em);
|
||||
}
|
||||
|
||||
static class User {
|
||||
|
||||
@@ -17,6 +17,7 @@ package org.springframework.data.jpa.repository.support;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.sql.Timestamp;
|
||||
@@ -43,7 +44,6 @@ import org.springframework.data.jpa.domain.sample.PersistableWithIdClassPK;
|
||||
import org.springframework.data.jpa.domain.sample.PrimitiveVersionProperty;
|
||||
import org.springframework.data.jpa.domain.sample.Role;
|
||||
import org.springframework.data.jpa.domain.sample.SampleWithIdClass;
|
||||
import org.springframework.data.jpa.domain.sample.SampleWithIdClass.SampleWithIdClassPK;
|
||||
import org.springframework.data.jpa.domain.sample.SampleWithPrimitiveId;
|
||||
import org.springframework.data.jpa.domain.sample.SampleWithTimestampVersion;
|
||||
import org.springframework.data.jpa.domain.sample.User;
|
||||
@@ -68,7 +68,7 @@ public class JpaMetamodelEntityInformationIntegrationTests {
|
||||
@Test
|
||||
public void detectsIdTypeForEntity() {
|
||||
|
||||
JpaEntityInformation<User, ?> information = JpaEntityInformationSupport.getMetadata(User.class, em);
|
||||
JpaEntityInformation<User, ?> information = getEntityInformation(User.class, em);
|
||||
assertThat(information.getIdType(), is(typeCompatibleWith(Integer.class)));
|
||||
}
|
||||
|
||||
@@ -83,7 +83,7 @@ public class JpaMetamodelEntityInformationIntegrationTests {
|
||||
@Ignore
|
||||
public void detectsIdTypeForMappedSuperclass() {
|
||||
|
||||
JpaEntityInformation<?, ?> information = JpaEntityInformationSupport.getMetadata(AbstractPersistable.class, em);
|
||||
JpaEntityInformation<?, ?> information = getEntityInformation(AbstractPersistable.class, em);
|
||||
assertEquals(Serializable.class, information.getIdType());
|
||||
}
|
||||
|
||||
@@ -93,8 +93,7 @@ public class JpaMetamodelEntityInformationIntegrationTests {
|
||||
@Test
|
||||
public void detectsIdClass() {
|
||||
|
||||
EntityInformation<PersistableWithIdClass, ?> information = JpaEntityInformationSupport.getMetadata(
|
||||
PersistableWithIdClass.class, em);
|
||||
EntityInformation<PersistableWithIdClass, ?> information = getEntityInformation(PersistableWithIdClass.class, em);
|
||||
assertThat(information.getIdType(), is(typeCompatibleWith(PersistableWithIdClassPK.class)));
|
||||
}
|
||||
|
||||
@@ -106,8 +105,7 @@ public class JpaMetamodelEntityInformationIntegrationTests {
|
||||
|
||||
PersistableWithIdClass entity = new PersistableWithIdClass(2L, 4L);
|
||||
|
||||
JpaEntityInformation<PersistableWithIdClass, ?> information = JpaEntityInformationSupport.getMetadata(
|
||||
PersistableWithIdClass.class, em);
|
||||
JpaEntityInformation<PersistableWithIdClass, ?> information = getEntityInformation(PersistableWithIdClass.class, em);
|
||||
Object id = information.getId(entity);
|
||||
|
||||
assertThat(id, is(instanceOf(PersistableWithIdClassPK.class)));
|
||||
@@ -227,8 +225,7 @@ public class JpaMetamodelEntityInformationIntegrationTests {
|
||||
@Test
|
||||
public void considersEntityWithUnsetCompundIdNew() {
|
||||
|
||||
EntityInformation<SampleWithIdClass, SampleWithIdClassPK> information = new JpaMetamodelEntityInformation<SampleWithIdClass, SampleWithIdClassPK>(
|
||||
SampleWithIdClass.class, em.getMetamodel());
|
||||
EntityInformation<SampleWithIdClass, ?> information = getEntityInformation(SampleWithIdClass.class, em);
|
||||
|
||||
assertThat(information.isNew(new SampleWithIdClass()), is(true));
|
||||
}
|
||||
@@ -239,7 +236,8 @@ public class JpaMetamodelEntityInformationIntegrationTests {
|
||||
@Test
|
||||
public void considersEntityWithSetTimestampVersionNotNew() {
|
||||
|
||||
EntityInformation<SampleWithTimestampVersion, Long> information = getEntityInformation(SampleWithTimestampVersion.class);
|
||||
EntityInformation<SampleWithTimestampVersion, ?> information = getEntityInformation(
|
||||
SampleWithTimestampVersion.class, em);
|
||||
|
||||
SampleWithTimestampVersion entity = new SampleWithTimestampVersion();
|
||||
entity.version = new Timestamp(new Date().getTime());
|
||||
@@ -253,7 +251,7 @@ public class JpaMetamodelEntityInformationIntegrationTests {
|
||||
@Test
|
||||
public void considersEntityWithNonPrimitiveNonNullIdTypeNotNew() {
|
||||
|
||||
EntityInformation<User, Long> information = getEntityInformation(User.class);
|
||||
EntityInformation<User, ?> information = getEntityInformation(User.class, em);
|
||||
|
||||
User user = new User();
|
||||
assertThat(information.isNew(user), is(true));
|
||||
@@ -262,10 +260,6 @@ public class JpaMetamodelEntityInformationIntegrationTests {
|
||||
assertThat(information.isNew(user), is(false));
|
||||
}
|
||||
|
||||
private <T, ID extends Serializable> EntityInformation<T, ID> getEntityInformation(Class<T> domainType) {
|
||||
return new JpaMetamodelEntityInformation<T, ID>(domainType, em.getMetamodel());
|
||||
}
|
||||
|
||||
protected String getMetadadataPersitenceUnitName() {
|
||||
return "metadata";
|
||||
}
|
||||
|
||||
@@ -52,27 +52,27 @@ public class JpaRepositoryFactoryUnitTests {
|
||||
JpaRepositoryFactory factory;
|
||||
|
||||
@Mock EntityManager entityManager;
|
||||
@Mock @SuppressWarnings("rawtypes") JpaEntityInformation metadata;
|
||||
@Mock @SuppressWarnings("rawtypes") JpaEntityInformation entityInformation;
|
||||
@Mock EntityManagerFactory emf;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
when(entityManager.getEntityManagerFactory()).thenReturn(emf);
|
||||
when(entityManager.getDelegate()).thenReturn(entityManager);
|
||||
when(emf.createEntityManager()).thenReturn(entityManager);
|
||||
|
||||
// Setup standard factory configuration
|
||||
factory = new JpaRepositoryFactory(entityManager) {
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T, ID extends Serializable> JpaEntityInformation<T, ID> getEntityInformation(Class<T> domainClass) {
|
||||
|
||||
return metadata;
|
||||
return entityInformation;
|
||||
};
|
||||
};
|
||||
|
||||
factory.setQueryLookupStrategyKey(Key.CREATE_IF_NOT_FOUND);
|
||||
|
||||
when(entityManager.getEntityManagerFactory()).thenReturn(emf);
|
||||
when(emf.createEntityManager()).thenReturn(entityManager);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -140,7 +140,7 @@ public class JpaRepositoryFactoryUnitTests {
|
||||
@Test
|
||||
public void usesQueryDslRepositoryIfInterfaceImplementsExecutor() {
|
||||
|
||||
when(metadata.getJavaType()).thenReturn(User.class);
|
||||
when(entityInformation.getJavaType()).thenReturn(User.class);
|
||||
assertEquals(QueryDslJpaRepository.class,
|
||||
factory.getRepositoryBaseClass(new DefaultRepositoryMetadata(QueryDslSampleRepository.class)));
|
||||
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright 2014 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.repository.support;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.ImportResource;
|
||||
import org.springframework.data.jpa.provider.PersistenceProviderIntegrationTests;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@ContextConfiguration
|
||||
public class OpenJpaProxyIdAccessorTests extends PersistenceProviderIntegrationTests {
|
||||
|
||||
@Configuration
|
||||
@ImportResource("classpath:openjpa.xml")
|
||||
static class Config {}
|
||||
}
|
||||
@@ -53,6 +53,8 @@ public class SimpleJpaRepositoryUnitTests {
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
when(em.getDelegate()).thenReturn(em);
|
||||
|
||||
when(information.getJavaType()).thenReturn(User.class);
|
||||
when(em.getCriteriaBuilder()).thenReturn(builder);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user