我正在使用这个api swapi研究spring引导数据,我做了很多事情,但是现在我不知道如何Map两个列表的关系,上面你可以看到我的代码和实体。
实体胶片
@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)
@ManyToMany(mappedBy = "films")
private List<Person> characters;
@JsonDeserialize(converter = StringToLocalDateTimeConverter.class)
private LocalDateTime created;
@JsonDeserialize(converter = StringToLocalDateTimeConverter.class)
private LocalDateTime edited;
private String url;
}
实体人
@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)
@ManyToMany
@JoinTable(
name = "film_person",
joinColumns = @JoinColumn(name = "film_fk", referencedColumnName = "id", nullable = true),
inverseJoinColumns = @JoinColumn(name = "person_fk", referencedColumnName = "id", nullable = true))
private List<Film> films;
@JsonDeserialize(converter = StringToLocalDateTimeConverter.class)
private LocalDateTime created;
@JsonDeserialize(converter = StringToLocalDateTimeConverter.class)
private LocalDateTime edited;
private String url;
}
我正在尝试使用springjpa方法来保存所有
@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);
}
转换器方法
@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;
}
}
util方法
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;
}
正在创建关系表,但没有填充关系表
暂无答案!
目前还没有任何答案,快来回答吧!