org.springframework.beans.factory.BeanCreationException在使用singleton中的相同对象时

时间:2016-04-11 18:14:01

标签: java spring hibernate

我基于Springboot编写服务器。我有MonsterFactory我有MonsterType个对象,其中MonsterType是实体。

public class MonsterFactory {

    public static final MonsterFactory INSTANCE = new MonsterFactory();
    private Map<String,MonsterType> monsterTypesMap = new HashMap<>();

    private MonsterFactory(){
        monsterTypesMap.put("Fire Golem", new MonsterType("Fire Golem","Opis",
            new MonsterStats(100,40,30,10),"/jakis/path"));
        monsterTypesMap.put("Bat", new MonsterType("Bat", "Opis 2",
            new MonsterStats(50,20,30,40),"/jakis/path2"));
    }

    public Monster create(final String name, Location location){
        MonsterType monsterType = monsterTypesMap.get(name);
        if(monsterType == null)
            throw new UnsupportedOperationException("unknown monster type");
        return new Monster(monsterType,location);
   }
}

现在,当我想向我的MonsterRepostory monsters添加一些Monster对象时:

monsters.save(MonsterFactory.create("Fire Golem", new Location(40,80));
monsters.save(MonsterFactory.create("Fire Golem", new Location(50, 80));

我得到org.springframework.beans.factory.BeanCreationException

我使用MonsterType对象的相同实例来创建两个不同的怪物或者什么是问题?我是Spring和Hibernate的新手,不知道问题的来源。

编辑: 好吧,我发现我得到了嵌套异常:org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist

0 个答案:

没有答案