Skip to content

Commit

Permalink
DATAREST-872 - Polishing.
Browse files Browse the repository at this point in the history
Simplified types used for testing. Removed unnecessary repository interface.

Related pull request: spring-projects#221.
  • Loading branch information
odrotbohm committed Sep 19, 2016
1 parent c36267a commit 90f57bd
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
*/
package org.springframework.data.rest.webmvc.jpa;

import lombok.Data;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;

/**
* @author Alex Leigh
* @see DATAREST-872
*/
@Data
@Entity
@DiscriminatorValue("D")
public class Dinner extends Meal {
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,26 @@
*/
package org.springframework.data.rest.webmvc.jpa;

import lombok.Data;

import javax.persistence.DiscriminatorValue;
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;

@Override
public String getType() {
return TYPE;
}

public String getSuiteCode() {
return suiteCode;
}

public void setSuiteCode(String suiteCode) {
this.suiteCode = suiteCode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,24 @@
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;
Expand All @@ -63,6 +75,7 @@
* @author Jon Brisbin
* @author Greg Turnquist
* @author Oliver Gierke
* @author Alex Leigh
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { JpaRepositoryConfig.class, PersistentEntitySerializationTests.TestConfig.class })
Expand All @@ -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 {
Expand Down Expand Up @@ -310,10 +324,9 @@ public void serializesInheritance() throws Exception {
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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
* @author Jon Brisbin
* @author Oliver Gierke
* @author Greg Turnquist
* @author Alex Leigh
*/
public class PersistentEntityJackson2Module extends SimpleModule {

Expand Down Expand Up @@ -317,6 +318,7 @@ private PersistentProperty<?> findProperty(String finalName, PersistentEntity<?,
* 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> {
Expand Down Expand Up @@ -370,10 +372,13 @@ public void serialize(Object value, JsonGenerator gen, SerializerProvider provid
}
}

/*
* (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);
}

Expand Down

0 comments on commit 90f57bd

Please sign in to comment.