gpt4 book ai didi

java - 如何在 Spring Data JPA 中实现与额外列的多对多关系?

转载 作者:行者123 更新时间:2023-12-02 02:24:02 26 4
gpt4 key购买 nike

我正在尝试创建多对多关系。以下是我试图创建的关系:

enter image description here

Heroes 表正在正常保存值,但没有记录写入 Hero_DnDClasses 表中。

这是我的类(class):

Hero.java

@Entity
@Table(name = "HEROES")
public class Hero {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "HERO_ID")
private Long id;

private Short strength;
private Short dexterity;
private Short constitution;
private Short intelligence;
private Short wisdom;
private Short charisma;
private String name;

@ManyToOne
private Race race;

@OneToMany(mappedBy = "hero")
private List<HeroDnDClasses> heroDnDClasses = new ArrayList<>();

public Hero() {
}

public Hero(Short strength, Short dexterity, Short constitution, Short intelligence, Short wisdom, Short charisma, String name, Race race, List<HeroDnDClasses> heroDnDClasses) {
this.strength = strength;
this.dexterity = dexterity;
this.constitution = constitution;
this.intelligence = intelligence;
this.wisdom = wisdom;
this.charisma = charisma;
this.name = name;
this.race = race;
this.heroDnDClasses = heroDnDClasses;
}

public Long getId() {
return id;
}

public Short getStrength() {
return strength;
}

public Short getDexterity() {
return dexterity;
}

public Short getConstitution() {
return constitution;
}

public Short getIntelligence() {
return intelligence;
}

public Short getWisdom() {
return wisdom;
}

public Short getCharisma() {
return charisma;
}

public String getName() {
return name;
}

public Race getRace() {
return race;
}

public List<HeroDnDClasses> getHeroDnDClasses() {
return heroDnDClasses;
}

@Override
public String toString() {
return "Hero{" +
"id=" + id +
", strength=" + strength +
", dexterity=" + dexterity +
", constitution=" + constitution +
", intelligence=" + intelligence +
", wisdom=" + wisdom +
", charisma=" + charisma +
", name='" + name + '\'' +
", race=" + race +
", HeroDnDClasses=" + heroDnDClasses +
'}';
}

public static HeroBuilder builder(){
return new HeroBuilder();
}

public static class HeroBuilder {
private Short strength;
private Short dexterity;
private Short constitution;
private Short intelligence;
private Short wisdom;
private Short charisma;
private String name;
private Race race;
private List<HeroDnDClasses> heroDnDClasses;

public HeroBuilder setStrength(Short strength) {
this.strength = strength;
return this;
}

public HeroBuilder setDexterity(Short dexterity) {
this.dexterity = dexterity;
return this;
}

public HeroBuilder setConstitution(Short constitution) {
this.constitution = constitution;
return this;
}

public HeroBuilder setIntelligence(Short intelligence) {
this.intelligence = intelligence;
return this;
}

public HeroBuilder setWisdom(Short wisdom) {
this.wisdom = wisdom;
return this;
}

public HeroBuilder setCharisma(Short charisma) {
this.charisma = charisma;
return this;
}

public HeroBuilder setName(String name) {
this.name = name;
return this;
}

public HeroBuilder setRace(Race race) {
this.race = race;
return this;
}

public HeroBuilder setHeroDnDClasses(List<HeroDnDClasses> heroDnDClasses) {
this.heroDnDClasses = heroDnDClasses;
return this;
}

public Hero build() {
return new Hero(strength, dexterity, constitution, intelligence, wisdom, charisma, name, race, heroDnDClasses);
}
}
}

HeroDnDClasses.java

@Entity
@Table(name = "HERO_DNDCLASSES")
public class HeroDnDClasses implements Serializable {

@Id
@ManyToOne
@JoinColumn(name = "HERO_ID")
private Hero hero;

@Id
@ManyToOne
@JoinColumn(name = "DNDCLASS_ID")
private DnDClass dnDClass;

private Integer level;

public Hero getHero() {
return hero;
}

public void setHero(Hero hero) {
this.hero = hero;
}

public DnDClass getDnDClass() {
return dnDClass;
}

public void setDnDClass(DnDClass dnDClass) {
this.dnDClass = dnDClass;
}

public Integer getLevel() {
return level;
}

public void setLevel(Integer level) {
this.level = level;
}

@Override
public String toString() {
return "HeroDnDClasses{" +
"hero=" + hero +
", dnDClass=" + dnDClass +
", level=" + level +
'}';
}
}

HeroService.java

@Service
public class HeroService {

private static final Logger LOGGER = LoggerFactory.getLogger(HeroService.class);

@Autowired
HeroRepository heroRepository;

@Autowired
DnDClassService dnDClassService;

@Autowired
RaceService raceService;

public List<Hero> getAllHeroes(){
return heroRepository.findAll();
}

@Transactional
public void addHero(Hero hero){
DnDClass newDnDClass = dnDClassService.findDnDClassById(hero.getHeroDnDClasses().get(0).getDnDClass().getId());
HeroDnDClasses newHeroDnDClasses = new HeroDnDClasses();

Race newRace = raceService.getRaceById(hero.getRace().getId());
newHeroDnDClasses.setDnDClass(newDnDClass);
newHeroDnDClasses.setLevel(hero.getHeroDnDClasses().get(0).getLevel());

List<HeroDnDClasses> heroDnDClassesList = new ArrayList<>();
heroDnDClassesList.add(newHeroDnDClasses);

Hero newHero = Hero.builder().setName(hero.getName())
.setStrength(hero.getStrength())
.setDexterity(hero.getDexterity())
.setConstitution(hero.getConstitution())
.setIntelligence(hero.getIntelligence())
.setWisdom(hero.getWisdom())
.setCharisma(hero.getCharisma())
.setRace(newRace)
.setHeroDnDClasses(heroDnDClassesList)
.build();

heroRepository.save(newHero);
}

public Hero findHeroById(Long id){
return heroRepository.findById(id);
}

public Hero findHeroByName(String name){
return heroRepository.findByName(name);
}
}

我已经遵循了一些教程,但是我的代码虽然可以运行,但似乎不起作用。如果您需要我发布任何其他类(class),请告诉我。提前致谢。

最佳答案

1) 设置级联:

@OneToMany(mappedBy = "hero", cascade = {CascadeType.PERSIST, CascadeTYPE.MERGE})
private List<HeroDnDClasses> heroDnDClasses = new ArrayList<>();

2)您忘记在链接实体上设置英雄引用:

newHeroDnDClasses.setDnDClass(newDnDClass);
newHeroDnDClasses.setLevel(hero.getHeroDnDClasses().get(0).getLevel());
..
Hero newHero = Hero.builder().setName(hero.getName())
..
newHeroDnDClasses.setHero(newHero);

现在双方都有引用,保存操作也将级联到链接表。

关于java - 如何在 Spring Data JPA 中实现与额外列的多对多关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48059996/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com