我一直在就英雄和别.进行午餐会。 下面是Character级,由Superhero和级延长:
@Data
@NoArgsConstructor
@Entity
@Table(name = "characters")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = Superhero.class, name = "superhero"),
@JsonSubTypes.Type(value = Villain.class, name = "villain")})
public abstract class Character {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private Integer id;
private String firstName;
private String lastName;
@Transient
private String fullName;
@Convert(converter = GenderConverter.class)
private Gender gender;
@Column(nullable = false)
private String alias;
@Convert(converter = StatusConverter.class)
private Status status;
@ManyToOne
@JoinColumn(name = "team_id")
private Team team;
private String originStory;
@Convert(converter = UniverseConverter.class)
private Universe universe;
public Character(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public Character(String firstName, String lastName, String alias) {
this.firstName = firstName;
this.lastName = lastName;
this.alias = alias;
}
public String getFullName() {
return firstName + " " + lastName;
}
}
接着是Team,可以由多个英雄或别,组成,但只有一支英雄或别villa才能与一个团队联系:
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "teams")
public class Team {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
@OneToMany(mappedBy = "team", cascade = CascadeType.ALL)
private List<Character> characters;
@Convert(converter = StatusConverter.class)
private Status status;
}
在这里,我如何尝试启动,这些实体正在存放:
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
/** Disable before running Tests! */
@Bean
public CommandLineRunner commandLineRunner(CharacterRepository characterRepository,
TeamRepository teamRepository) {
return args -> {
// Initilaizing
Superhero ironMan = new Superhero("Tony", "Stark", "Iron Man");
Superhero thor = new Superhero("Thor", "Odinson", "God of Thunder");
Team avengers = Team.builder().name("Avengers").status(DISBANDED).build();
// Setting attributes
ironMan.setGender(MALE); thor.setGender(MALE);
ironMan.setUniverse(MCU); thor.setUniverse(MCU);
ironMan.setStatus(DECEASED); thor.setStatus(ALIVE);
// Saving to repositories
characterRepository.saveAll(List.of(ironMan, thor));
teamRepository.save(avengers);
// Trying to assign foreign keys
avengers.setCharacters(List.of(ironMan, thor));
ironMan.setTeam(avengers);
thor.setTeam(avengers);
};
}
}
The result in postgres looks like this:
demo2=# SELECT * FROM teams;
id | name | status
----+----------+-----------
1 | Avengers | Disbanded
(1 row)
demo2=# SELECT * FROM superheroes;
id | team_id | alias | first_name | gender | last_name | origin_story | status | universe
----+---------+----------------+------------+--------+-----------+--------------+----------+----------
1 | | Iron Man | Tony | Male | Stark | | Deceased | MCU
2 | | God of Thunder | Thor | Male | Odinson | | Alive | MCU
(2 rows)
如你看见<代码>team_id,作为两种英雄的外国钥匙是空的。 我尝试在<条码>上添加<条码>参考/代码>,在“编码”上添加“JoinColumn说明,添加机体类型、计算数据,然后确定属性,但既无帮助。