diff --git a/src/main/java/org/springframework/data/jpa/domain/AbstractPersistable.java b/src/main/java/org/springframework/data/jpa/domain/AbstractPersistable.java index 7ab4bed24..c3aa080ce 100644 --- a/src/main/java/org/springframework/data/jpa/domain/AbstractPersistable.java +++ b/src/main/java/org/springframework/data/jpa/domain/AbstractPersistable.java @@ -1,5 +1,5 @@ /* - * Copyright 2008-2011 the original author or authors. + * Copyright 2008-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,9 +18,9 @@ package org.springframework.data.jpa.domain; import java.io.Serializable; import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.MappedSuperclass; +import javax.persistence.Transient; import org.springframework.data.domain.Persistable; @@ -29,6 +29,7 @@ import org.springframework.data.domain.Persistable; * {@link #equals(Object)} and {@link #hashCode()} based on that id. * * @author Oliver Gierke + * @author Thomas Darimont * @param the the of the entity */ @MappedSuperclass @@ -36,9 +37,7 @@ public abstract class AbstractPersistable implements Pe private static final long serialVersionUID = -5554308939380869754L; - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - private PK id; + @Id @GeneratedValue private PK id; /* * (non-Javadoc) @@ -60,11 +59,13 @@ public abstract class AbstractPersistable implements Pe this.id = id; } - /* - * (non-Javadoc) + /** + * Must be {@link Transient} in order to ensure that no JPA provider complains because of a missing setter. * + * @see DATAJPA-622 * @see org.springframework.data.domain.Persistable#isNew() */ + @Transient public boolean isNew() { return null == getId(); @@ -120,4 +121,4 @@ public abstract class AbstractPersistable implements Pe return hashCode; } -} \ No newline at end of file +} diff --git a/src/test/java/org/springframework/data/jpa/domain/sample/CustomAbstractPersistable.java b/src/test/java/org/springframework/data/jpa/domain/sample/CustomAbstractPersistable.java new file mode 100644 index 000000000..60065ca45 --- /dev/null +++ b/src/test/java/org/springframework/data/jpa/domain/sample/CustomAbstractPersistable.java @@ -0,0 +1,46 @@ +/* + * 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.domain.sample; + +import java.util.UUID; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; + +import org.hibernate.annotations.GenericGenerator; +import org.springframework.data.jpa.domain.AbstractPersistable; + +/** + * @author Thomas Darimont + */ +@Entity +@Table(name = "customAbstractPersistable") +public class CustomAbstractPersistable extends AbstractPersistable { + + private static final long serialVersionUID = 1L; + + @Id + @Override + @GeneratedValue(generator = "uuid2") + @GenericGenerator(name = "uuid2", strategy = "uuid2") + @Column(name = "task_id", columnDefinition = "BINARY(16)") + public UUID getId() { + return super.getId(); + } +} diff --git a/src/test/java/org/springframework/data/jpa/repository/AbstractPersistableIntegrationTests.java b/src/test/java/org/springframework/data/jpa/repository/AbstractPersistableIntegrationTests.java new file mode 100644 index 000000000..33a674406 --- /dev/null +++ b/src/test/java/org/springframework/data/jpa/repository/AbstractPersistableIntegrationTests.java @@ -0,0 +1,54 @@ +/* + * 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; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.jpa.domain.sample.CustomAbstractPersistable; +import org.springframework.data.jpa.repository.sample.CustomAbstractPersistableRepository; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.transaction.annotation.Transactional; + +/** + * @author Thomas Darimont + */ +@Transactional +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = { "classpath:config/namespace-autoconfig-context.xml" }) +public class CustomAbstractPersistableIntegrationTests { + + @Autowired CustomAbstractPersistableRepository repository; + + /** + * @see DATAJPA-622 + */ + @Test + public void shouldBeAbleToSaveAndLoadCustomPersistableWithUuidId() { + + CustomAbstractPersistable entity = new CustomAbstractPersistable(); + CustomAbstractPersistable saved = repository.save(entity); + + CustomAbstractPersistable found = repository.findOne(saved.getId()); + + assertThat(found, is(saved)); + } + +} diff --git a/src/test/java/org/springframework/data/jpa/repository/CustomAbstractPersistableIntegrationTests.java b/src/test/java/org/springframework/data/jpa/repository/CustomAbstractPersistableIntegrationTests.java new file mode 100644 index 000000000..33a674406 --- /dev/null +++ b/src/test/java/org/springframework/data/jpa/repository/CustomAbstractPersistableIntegrationTests.java @@ -0,0 +1,54 @@ +/* + * 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; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.jpa.domain.sample.CustomAbstractPersistable; +import org.springframework.data.jpa.repository.sample.CustomAbstractPersistableRepository; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.transaction.annotation.Transactional; + +/** + * @author Thomas Darimont + */ +@Transactional +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = { "classpath:config/namespace-autoconfig-context.xml" }) +public class CustomAbstractPersistableIntegrationTests { + + @Autowired CustomAbstractPersistableRepository repository; + + /** + * @see DATAJPA-622 + */ + @Test + public void shouldBeAbleToSaveAndLoadCustomPersistableWithUuidId() { + + CustomAbstractPersistable entity = new CustomAbstractPersistable(); + CustomAbstractPersistable saved = repository.save(entity); + + CustomAbstractPersistable found = repository.findOne(saved.getId()); + + assertThat(found, is(saved)); + } + +} diff --git a/src/test/java/org/springframework/data/jpa/repository/sample/CustomAbstractPersistableRepository.java b/src/test/java/org/springframework/data/jpa/repository/sample/CustomAbstractPersistableRepository.java new file mode 100644 index 000000000..819a2b0d0 --- /dev/null +++ b/src/test/java/org/springframework/data/jpa/repository/sample/CustomAbstractPersistableRepository.java @@ -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 java.util.UUID; + +import org.springframework.data.jpa.domain.sample.CustomAbstractPersistable; +import org.springframework.data.repository.CrudRepository; + +/** + * @author Thomas Darimont + */ +public interface CustomAbstractPersistableRepository extends CrudRepository {} diff --git a/src/test/resources/META-INF/persistence.xml b/src/test/resources/META-INF/persistence.xml index e58401db3..2c83c6506 100644 --- a/src/test/resources/META-INF/persistence.xml +++ b/src/test/resources/META-INF/persistence.xml @@ -35,6 +35,7 @@ org.springframework.data.jpa.domain.sample.SpecialUser org.springframework.data.jpa.domain.sample.User org.springframework.data.jpa.domain.sample.VersionedUser + org.springframework.data.jpa.domain.sample.CustomAbstractPersistable true @@ -42,6 +43,7 @@ org.springframework.data.jpa.domain.sample.MailSender org.springframework.data.jpa.domain.sample.MailUser org.springframework.data.jpa.domain.sample.User + org.springframework.data.jpa.domain.sample.CustomAbstractPersistable true @@ -83,6 +85,7 @@ org.springframework.data.jpa.domain.sample.MailSender org.springframework.data.jpa.domain.sample.MailUser org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformationIntegrationTests$Sample + org.springframework.data.jpa.domain.sample.CustomAbstractPersistable true @@ -95,6 +98,7 @@ org.springframework.data.jpa.domain.sample.MailSender org.springframework.data.jpa.domain.sample.MailUser org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformationIntegrationTests$Sample + org.springframework.data.jpa.domain.sample.CustomAbstractPersistable true @@ -104,6 +108,7 @@ org.springframework.data.jpa.domain.sample.MailSender org.springframework.data.jpa.domain.sample.MailUser org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformationIntegrationTests$Sample + org.springframework.data.jpa.domain.sample.CustomAbstractPersistable true