DATAREST-872 - Polishing.

Simplified types used for testing. Removed unnecessary repository interface.

Related pull request: #221.
This commit is contained in:
Oliver Gierke
2016-09-19 09:41:03 +02:00
parent 3494c5eacc
commit 74895680eb
8 changed files with 60 additions and 109 deletions

View File

@@ -15,6 +15,8 @@
*/
package org.springframework.data.rest.webmvc.jpa;
import lombok.Data;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
@@ -22,6 +24,7 @@ import javax.persistence.Entity;
* @author Alex Leigh
* @see DATAREST-872
*/
@Data
@Entity
@DiscriminatorValue("D")
public class Dinner extends Meal {
@@ -34,12 +37,4 @@ public class Dinner extends Meal {
public String getType() {
return TYPE;
}
public String getDinnerCode() {
return dinnerCode;
}
public void setDinnerCode(String dinnerCode) {
this.dinnerCode = dinnerCode;
}
}

View File

@@ -15,52 +15,36 @@
*/
package org.springframework.data.rest.webmvc.jpa;
import javax.persistence.*;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
/**
* @author Alex Leigh
* @see DATAREST-872
*/
@Data
@Entity
public class Guest {
@Id
@GeneratedValue
@Id @GeneratedValue //
private Long id;
@OneToOne(cascade = CascadeType.ALL)
@OneToOne(cascade = CascadeType.ALL) //
private Room room;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true) //
private List<Meal> meals = new ArrayList<Meal>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Room getRoom() {
return room;
}
public void setRoom(Room room) {
this.room = room;
}
public List<Meal> getMeals() {
return meals;
}
public void setMeals(List<Meal> meals) {
this.meals = meals;
}
public void addMeal(Meal meal) {
meals.add(meal);
this.meals.add(meal);
}
}

View File

@@ -1,24 +0,0 @@
/*
* Copyright 2015-2016 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.rest.webmvc.jpa;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* @author Alex Leigh
* @see DATAREST-872
*/
public interface GuestRepository extends JpaRepository<Guest, Long> {}

View File

@@ -15,37 +15,28 @@
*/
package org.springframework.data.rest.webmvc.jpa;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
/**
* @author Alex Leigh
* @see DATAREST-872
*/
@Data
@Entity
@Inheritance
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = Dinner.class, name = Dinner.TYPE)
})
@JsonSubTypes({ @JsonSubTypes.Type(value = Dinner.class, name = Dinner.TYPE) })
public abstract class Meal {
@Id
@GeneratedValue
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Id @GeneratedValue private Long id;
public abstract String getType();
}

View File

@@ -15,37 +15,29 @@
*/
package org.springframework.data.rest.webmvc.jpa;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
/**
* @author Alex Leigh
* @see DATAREST-872
*/
@Data
@Entity
@Inheritance
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = Suite.class, name = Suite.TYPE)
})
@JsonSubTypes({ @JsonSubTypes.Type(value = Suite.class, name = Suite.TYPE) })
public abstract class Room {
@Id
@GeneratedValue
@Id @GeneratedValue //
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public abstract String getType();
}

View File

@@ -15,6 +15,8 @@
*/
package org.springframework.data.rest.webmvc.jpa;
import lombok.Data;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
@@ -22,11 +24,12 @@ import javax.persistence.Entity;
* @author Alex Leigh
* @see DATAREST-872
*/
@Data
@Entity
@DiscriminatorValue("S")
public class Suite extends Room {
public static final String TYPE = "suite";
static final String TYPE = "suite";
private String suiteCode;
@@ -34,12 +37,4 @@ public class Suite extends Room {
public String getType() {
return TYPE;
}
public String getSuiteCode() {
return suiteCode;
}
public void setSuiteCode(String suiteCode) {
this.suiteCode = suiteCode;
}
}

View File

@@ -31,12 +31,24 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.context.support.StaticMessageSource;
import org.springframework.data.jpa.mapping.JpaMetamodelMappingContext;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.projection.ProjectionFactory;
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
import org.springframework.data.repository.support.Repositories;
import org.springframework.data.rest.webmvc.PersistentEntityResource;
import org.springframework.data.rest.webmvc.jpa.*;
import org.springframework.data.rest.webmvc.jpa.CreditCard;
import org.springframework.data.rest.webmvc.jpa.Dinner;
import org.springframework.data.rest.webmvc.jpa.Guest;
import org.springframework.data.rest.webmvc.jpa.JpaRepositoryConfig;
import org.springframework.data.rest.webmvc.jpa.LineItem;
import org.springframework.data.rest.webmvc.jpa.Order;
import org.springframework.data.rest.webmvc.jpa.OrderRepository;
import org.springframework.data.rest.webmvc.jpa.Person;
import org.springframework.data.rest.webmvc.jpa.PersonRepository;
import org.springframework.data.rest.webmvc.jpa.PersonSummary;
import org.springframework.data.rest.webmvc.jpa.Suite;
import org.springframework.data.rest.webmvc.jpa.UserExcerpt;
import org.springframework.data.rest.webmvc.util.TestUtils;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.LinkDiscoverer;
@@ -63,6 +75,7 @@ import com.jayway.jsonpath.JsonPath;
* @author Jon Brisbin
* @author Greg Turnquist
* @author Oliver Gierke
* @author Alex Leigh
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { JpaRepositoryConfig.class, PersistentEntitySerializationTests.TestConfig.class })
@@ -75,6 +88,7 @@ public class PersistentEntitySerializationTests {
@Autowired Repositories repositories;
@Autowired PersonRepository people;
@Autowired OrderRepository orders;
@Autowired JpaMetamodelMappingContext context;
@Configuration
static class TestConfig extends RepositoryTestsConfig {
@@ -310,10 +324,9 @@ public class PersistentEntitySerializationTests {
guest.setRoom(suite);
guest.addMeal(dinner);
PersistentEntityResource resource = PersistentEntityResource.
build(guest, repositories.getPersistentEntity(Guest.class)).
withLink(new Link("/guests/1")).
build();
PersistentEntityResource resource = PersistentEntityResource//
.build(guest, context.getPersistentEntity(Guest.class))//
.withLink(new Link("/guests/1")).build();
String result = mapper.writeValueAsString(resource);

View File

@@ -96,6 +96,7 @@ import com.fasterxml.jackson.databind.util.NameTransformer;
* @author Jon Brisbin
* @author Oliver Gierke
* @author Greg Turnquist
* @author Alex Leigh
*/
public class PersistentEntityJackson2Module extends SimpleModule {
@@ -318,6 +319,7 @@ public class PersistentEntityJackson2Module extends SimpleModule {
* Serializer to wrap values into an {@link Resource} instance and collecting all association links.
*
* @author Oliver Gierke
* @author Alex Leigh
* @since 2.5
*/
public static class NestedEntitySerializer extends StdSerializer<Object> {
@@ -371,10 +373,13 @@ public class PersistentEntityJackson2Module extends SimpleModule {
}
}
/*
* (non-Javadoc)
* @see com.fasterxml.jackson.databind.JsonSerializer#serializeWithType(java.lang.Object, com.fasterxml.jackson.core.JsonGenerator, com.fasterxml.jackson.databind.SerializerProvider, com.fasterxml.jackson.databind.jsontype.TypeSerializer)
*/
@Override
public void serializeWithType(Object value, JsonGenerator gen, SerializerProvider provider,
TypeSerializer typeSerializer) throws IOException {
TypeSerializer typeSerializer) throws IOException {
serialize(value, gen, provider);
}