0
I’m doing a back end study using the api SWAPI, I have already mapped the entities etc. But now I don’t know how to map to insert, for example:
Entidade Film
@Data
@Entity
public class Film extends Persistent<Long> {
private String title;
@JsonProperty(value = "episode_id")
private int episodeId;
@JsonProperty(value = "opening_crawl")
@Column(columnDefinition = "CLOB")
private String openingCrawl;
private String director;
private String producer;
@JsonDeserialize(converter = StringToLocalDateConverter.class)
@JsonProperty(value = "release_date")
private LocalDate releaseDate;
@JsonDeserialize(converter = ApiURLToEntitiesConverter.class)
private List<Person> characters;
@JsonDeserialize(converter = StringToLocalDateTimeConverter.class)
private LocalDateTime created;
@JsonDeserialize(converter = StringToLocalDateTimeConverter.class)
private LocalDateTime edited;
private String url;
}
Entity Person
@Data
@Entity
public class Person extends Persistent<Long> {
private String name;
private String height;
private String mass;
@JsonProperty(value = "hair_color")
private String hairColor;
@JsonProperty(value = "skin_color")
private String skinColor;
@JsonProperty(value = "eye_color")
private String eyeColor;
@JsonProperty(value = "birth_year")
private String birthYear;
private String gender;
@JsonDeserialize(converter = ApiURLToEntityConverter.class)
@JoinColumn(name = "planet_id", foreignKey = @javax.persistence.ForeignKey(name = "none"))
@OneToOne(optional = true)
private Planet homeworld;
@JsonDeserialize(converter = ApiURLToEntitiesConverter.class)
private List<Film> films;
@JsonDeserialize(converter = StringToLocalDateTimeConverter.class)
private LocalDateTime created;
@JsonDeserialize(converter = StringToLocalDateTimeConverter.class)
private LocalDateTime edited;
private String url;
}
Anyway, in the code above, this without the mapping because I actually tried several things but no effect.
I’m trying to use the method insertAll
of Spring Data
@Override
public List<T> insertAll(List<T> entities) {
for (Persistent entity : entities) {
Set<ConstraintViolation<Persistent>> violations = validator.validate(entity);
if (violations != null && !violations.isEmpty()) {
throw new ConstraintViolationException(violations);
}
}
return repository.saveAll(entities);
}
Method to convert what you receive
@Override
public List convert(List<String> s) {
if (s == null || s.isEmpty()) {
return null;
}
List objetos = new LinkedList();
for (String url : s) {
if (url.contains("people")) {
objetos.add(Util.getPerson(url));
}
if (url.contains("planets")) {
objetos.add(Util.getPlanet(url));
}
if (url.contains("starships")) {
objetos.add(Util.getStarship(url));
}
if (url.contains("vehicles")) {
objetos.add(Util.getVehicle(url));
}
if (url.contains("species")) {
objetos.add(Util.getSpecie(url));
}
}
return objetos;
}
}
Useful method that generates the Person entity class
public static Person getPerson(String characterApiUrl) {
if (characterApiUrl == null || characterApiUrl.isEmpty()) {
return null;
}
Person person = new Person();
person.setId(StringUtil.getIdEntity(characterApiUrl, "people/"));
return person;
}
Some parts were deleted so as not to increase the post too much, but I wanted to know if there is a direct link, because it keeps generating error of Constraint, because the entity has not been saved yet