Removed relational data from JSON serialization.

This commit is contained in:
neviyn 2018-09-24 21:59:59 +01:00
parent 4aa20221f0
commit 40ad371748
6 changed files with 18 additions and 11 deletions

View File

@ -1,8 +1,7 @@
package uk.co.neviyn.Observations.core; package uk.co.neviyn.Observations.core;
import com.fasterxml.jackson.annotation.JsonIdentityInfo; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import lombok.*; import lombok.*;
import org.joda.time.DateTime; import org.joda.time.DateTime;
@ -17,7 +16,6 @@ import java.util.Set;
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
@Builder @Builder
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class Observation implements Serializable { public class Observation implements Serializable {
@NonNull @NonNull
@ -28,10 +26,12 @@ public class Observation implements Serializable {
@ManyToOne @ManyToOne
@JoinColumn(name="site_id", nullable=false) @JoinColumn(name="site_id", nullable=false)
@JsonIgnore
@EqualsAndHashCode.Exclude @EqualsAndHashCode.Exclude
private Site site; private Site site;
@ManyToMany(mappedBy = "observations") @ManyToMany(mappedBy = "observations")
@JsonIgnore
@EqualsAndHashCode.Exclude @EqualsAndHashCode.Exclude
private Set<Tutor> tutors; private Set<Tutor> tutors;

View File

@ -1,8 +1,7 @@
package uk.co.neviyn.Observations.core; package uk.co.neviyn.Observations.core;
import com.fasterxml.jackson.annotation.JsonIdentityInfo; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import lombok.*; import lombok.*;
import javax.persistence.*; import javax.persistence.*;
@ -15,7 +14,6 @@ import java.util.Set;
@NoArgsConstructor @NoArgsConstructor
@Builder @Builder
@AllArgsConstructor @AllArgsConstructor
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class Site implements Serializable { public class Site implements Serializable {
@NonNull @NonNull
@ -29,10 +27,12 @@ public class Site implements Serializable {
private String name; private String name;
@OneToMany(mappedBy="site") @OneToMany(mappedBy="site")
@JsonIgnore
@EqualsAndHashCode.Exclude @EqualsAndHashCode.Exclude
private Set<Tutor> tutors; private Set<Tutor> tutors;
@OneToMany(mappedBy = "site") @OneToMany(mappedBy = "site")
@JsonIgnore
@EqualsAndHashCode.Exclude @EqualsAndHashCode.Exclude
private Set<Observation> observations; private Set<Observation> observations;
} }

View File

@ -1,8 +1,7 @@
package uk.co.neviyn.Observations.core; package uk.co.neviyn.Observations.core;
import com.fasterxml.jackson.annotation.JsonIdentityInfo; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import lombok.*; import lombok.*;
import javax.persistence.*; import javax.persistence.*;
@ -15,7 +14,6 @@ import java.util.Set;
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
@Builder @Builder
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class Tutor implements Serializable { public class Tutor implements Serializable {
@NonNull @NonNull
@ -30,10 +28,12 @@ public class Tutor implements Serializable {
@ManyToOne @ManyToOne
@JoinColumn(name="site_id", nullable=false) @JoinColumn(name="site_id", nullable=false)
@JsonIgnore
@EqualsAndHashCode.Exclude @EqualsAndHashCode.Exclude
private Site site; private Site site;
@ManyToMany @ManyToMany
@JsonIgnore
@JoinTable( @JoinTable(
name = "TUTOR_OBSERVATION", name = "TUTOR_OBSERVATION",
joinColumns = { @JoinColumn(name = "tutor_id")}, joinColumns = { @JoinColumn(name = "tutor_id")},

View File

@ -53,7 +53,7 @@ public class ObservationResource {
t.getObservations().add(observation); t.getObservations().add(observation);
} }
observation = dao.persist(observation); observation = dao.persist(observation);
log.info("Created observation with ID " + observation.getId() + " at " + DateTime.now().toString()); log.info("Created observation with ID " + observation.getId());
return observation.getId(); return observation.getId();
} }

View File

@ -3,6 +3,7 @@ package uk.co.neviyn.Observations.resources;
import io.dropwizard.auth.Auth; import io.dropwizard.auth.Auth;
import io.dropwizard.hibernate.UnitOfWork; import io.dropwizard.hibernate.UnitOfWork;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import uk.co.neviyn.Observations.api.SelectOption; import uk.co.neviyn.Observations.api.SelectOption;
import uk.co.neviyn.Observations.core.Site; import uk.co.neviyn.Observations.core.Site;
import uk.co.neviyn.Observations.core.User; import uk.co.neviyn.Observations.core.User;
@ -16,6 +17,7 @@ import java.util.stream.Collectors;
@RequiredArgsConstructor @RequiredArgsConstructor
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@Path("/site") @Path("/site")
@Slf4j
public class SiteResource { public class SiteResource {
private final SiteDao dao; private final SiteDao dao;
@ -23,7 +25,9 @@ public class SiteResource {
@POST @POST
@UnitOfWork @UnitOfWork
public Site add(@Auth User user, String name) { public Site add(@Auth User user, String name) {
return dao.persist(Site.builder().name(name).build()); Site site = dao.persist(Site.builder().name(name).build());
log.info("Created site '" + site.getName() + "' with id " + site.getId());
return site;
} }
@Path("/all") @Path("/all")

View File

@ -7,6 +7,7 @@ import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.NonNull; import lombok.NonNull;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import uk.co.neviyn.Observations.core.Observation; import uk.co.neviyn.Observations.core.Observation;
import uk.co.neviyn.Observations.core.Site; import uk.co.neviyn.Observations.core.Site;
import uk.co.neviyn.Observations.core.Tutor; import uk.co.neviyn.Observations.core.Tutor;
@ -23,6 +24,7 @@ import java.util.Set;
@RequiredArgsConstructor @RequiredArgsConstructor
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@Path("/tutor") @Path("/tutor")
@Slf4j
public class TutorResource { public class TutorResource {
private final TutorDao dao; private final TutorDao dao;
@ -34,6 +36,7 @@ public class TutorResource {
final Site site = siteDao.get(newTutor.siteId); final Site site = siteDao.get(newTutor.siteId);
final Tutor tutor = Tutor.builder().name(newTutor.name).site(site).build(); final Tutor tutor = Tutor.builder().name(newTutor.name).site(site).build();
site.getTutors().add(tutor); site.getTutors().add(tutor);
log.info("Created tutor '" + tutor.getName() + "' with id " + tutor.getId() + " at site " + site.getId());
return dao.persist(tutor); return dao.persist(tutor);
} }