当前位置: 首页 > 知识库问答 >
问题:

Hibernate分离实体传递到持久化

温镜
2023-03-14

我正试图实现与hibernate的许多单向关系。问题是,当我试图向数据库中添加一些值时,我遇到了以下错误:

运行时发生异常。null:InvocationTargetException:未能执行ApplicationRunner:传递给persist:dnd35cg的分离实体。模型DND类;嵌套的异常是org。冬眠PersistentObjectException:传递给persist:dnd35cg的分离实体。模型DND类

以下是我的课程:

英雄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;

@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "HERO_CLASSES",
            joinColumns = { @JoinColumn(name = "HERO_ID") },
            inverseJoinColumns = { @JoinColumn(name = "DNDCLASS_ID") })
private Set<DnDClass> dnDClasses = new HashSet<>();

public Hero() {
}

public Hero(Short strength, Short dexterity, Short constitution, Short intelligence, Short wisdom, Short charisma, String name, Race race, Set<DnDClass> dnDClasses) {
    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.dnDClasses = dnDClasses;
}

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 Set<DnDClass> getDnDClasses() {
    return dnDClasses;
}

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

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 Set<DnDClass> dndClasses;

    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 setDnDClasses(Set<DnDClass> dndClasses) {
        this.dndClasses = dndClasses;
        return this;
    }

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

DnDClass.java

@Entity
@Table(name = "DNDCLASSES")
public class DnDClass {

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

private String name;
private Byte hitDie;

public DnDClass() {
}

public DnDClass(String name, Byte hitDie) {
    this.name = name;
    this.hitDie = hitDie;
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getName() {
    return name;
}

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

public Byte getHitDie() {
    return hitDie;
}

public void setHitDie(Byte hitDie) {
    this.hitDie = hitDie;
}

@Override
public String toString() {
    return "DnDClass{" +
            "id=" + id +
            ", name='" + name + '\'' +
            ", hitDie=" + hitDie +
            '}';
}
}

DND35CG应用程序。JAVA

@SpringBootApplication
public class Dnd35cgApplication implements ApplicationRunner{

public static final Logger LOGGER = LoggerFactory.getLogger(Dnd35cgApplication.class);
public static void main(String[] args) {
    SpringApplication.run(Dnd35cgApplication.class, args);
}

@Autowired
HeroRepository heroRepository;

@Autowired
RaceRepository raceRepository;

@Autowired
DnDClassRepository dnDClassRepository;

@Override
public void run(ApplicationArguments applicationArguments) throws Exception {
    List<Race> races = new ArrayList<>();
    races.add(new Race("Human", "Medium", 30));
    races.add(new Race("Dwarf", "Medium", 20));
    races.add(new Race("Elf", "Medium", 30));
    races.add(new Race("Half-Elf", "Medium", 30));
    races.add(new Race("Half-Orc", "Medium", 30));
    races.add(new Race("Gnome", "Small", 20));
    races.add(new Race("Halfling", "Small", 20));

    for(Race r : races)
        raceRepository.save(r);

    List<DnDClass> classes = new ArrayList<>();
    classes.add(new DnDClass("Barbarian", (byte)12));
    classes.add(new DnDClass("Bard", (byte)6));
    classes.add(new DnDClass("Cleric", (byte)8));
    classes.add(new DnDClass("Druid", (byte)8));
    classes.add(new DnDClass("Fighter", (byte)10));
    classes.add(new DnDClass("Monk", (byte)8));
    classes.add(new DnDClass("Paladin", (byte)10));
    classes.add(new DnDClass("Ranger", (byte)10));
    classes.add(new DnDClass("Rogue", (byte)6));
    classes.add(new DnDClass("Sorcerer", (byte)4));
    classes.add(new DnDClass("Wizard", (byte)4));

    for(DnDClass c : classes)
        dnDClassRepository.save(c);

    List<Hero> initialHeroes = new ArrayList<>();

    Random rand = new Random();

    Set<DnDClass> dnDClassesSet = new HashSet<>();
    dnDClassesSet.add(dnDClassRepository.findById(1L));

    for (int i=0; i<=10; i++) {
        initialHeroes.add(new Hero().builder().setName("Hero " + i)
                .setStrength((short) (rand.nextInt(16) + 3))
                .setDexterity((short) (rand.nextInt(16) + 3))
                .setConstitution((short) (rand.nextInt(16) + 3))
                .setIntelligence((short) (rand.nextInt(16) + 3))
                .setWisdom((short) (rand.nextInt(16) + 3))
                .setCharisma((short) (rand.nextInt(16) + 3))
                .setRace(raceRepository.findById(rand.nextInt(7) + 1L))
                .setDnDClasses(dnDClassesSet)
                .build());
    }

    for (Hero h : initialHeroes) {
        heroRepository.save(h);
    }

}
}

问题是当我使用时。setDnDClasses(DNDClasseSet)当我在run方法中创建新英雄时。

我怀疑这可能是因为我使用的是构建器模式,没有合适的setter。我稍后会进行重构,因为Hero类有太多字段,但目前我希望它能像这样运行。

我想提前感谢您的帮助。

共有1个答案

相化
2023-03-14

修正了错误。

瀑布型。这一切都是问题的根源。我不知道为什么,但当我把它取下来时,它起了作用。

欢迎解释。

非常感谢你。

 类似资料:
  • 这个网站上没有一个类似的问题能够解决我的问题。 错误:org.hibernate.persistentobjectexception:传递给persist:healthcheckapi.model.checks的分离实体 示例JSON健康对象: 请注意,这两个对象的ID都是自动生成的,我认为这是问题的一部分。

  • 问题内容: 我正在尝试使用Hibernate EntityManagerpersist方法将个人pojo插入mysql db, 得到这个例外 还有更多.. 我的Person.java类是, 然后是pojo的getter setter方法。 引发TitleType类的错误是“公共静态最终”标题类型定义提供程序, 我正在为人员设置标题类型类, 不可能将公共静态最终类型定义的类传递给Hibernate?

  • 我有一个实体,它已经持久化,并希望将其添加到新生成的父实体(尚未持久化)。如果我尝试持久化父级,我会得到错误“分离的实体传递到持久化:model.child”。我想我必须以某种方式为孩子调用“EntityManager.merge()”,而不是“EntityManager.persisted()”。但是我没有显式调用persisted。这由“cascade=cascadetype.all”注释处理

  • 我用的是JPA。 在数据库中,有建筑表和房间表。它们有一个OneTo多连接。 我可以将数据添加到建筑中,但当我尝试将数据添加到房间中时,会出现错误: 我的建筑模型: 我的房间模型: 我的主应用。在这里,我获取建筑对象并将其传递到房间: 请帮忙。

  • 问题内容: 我已经成功用hibernate写了我的第一个主要的孩子例子。几天后,我再次使用它并升级了一些库。不知道我做了什么,但是我再也无法使它运行了。有人可以帮助我找出返回以下错误消息的代码中的错误吗: hibernate映射: 编辑: InvoiceManager.java 发票.java InvoiceItem.java 编辑: 从客户端发送的JSON对象: 编辑: 一些详细信息: 我试图通

  • 我有一个实体,它已经被持久化了,想把它添加到一个新生成的父实体(还没有持久化)。如果我尝试持久化父级,那么我会得到错误“Detached entity pass to persist:model.child”。我认为我必须以某种方式为孩子调用“EntityManager.merge()”,而不是“EntityManager.Persist()”。但我没有显式调用persist。这是由“Cascad