Hibernate EntityNotFoundException:无法找到xxx

时间:2016-06-13 19:29:27

标签: hibernate

我的实体里面有地图字段。

@Entity
@Table(name = "dictionaries")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Dictionary {

    .........         

    @ElementCollection
    @OneToMany(cascade = CascadeType.ALL)
    @MapKeyColumn(name="word_id")
    @Column(name="translation ")
    @CollectionTable(name="dict_word_translate", joinColumns=@JoinColumn(name="dict_id"))
    private Map<Translation, Word> attributes = new HashMap();

    .........
}

翻译

@Entity
public class Translation {

     @Id
     private String value;

    public Translation() {}

    public Translation(String translation) {
        this.value = translation;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
         this.value = value;
    }
}

字符:

@Entity
@NamedQuery(name = "Word.retrivetFirst",
    query = "from Word u where u.value = ?1")
public class Word {

    @Id
    private String value;

    public Word() {}

    public Word(String word) {
        this.value = word;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

我在dao中有保存词典的方法:

public Dictionary addWordToDictionary(int id, Word word, Translation translation) {
    Dictionary dict = getDictionary(id);
    dict.getAttributes().put(translation, word);
    repository.save(dict);
    return dict;
}

当我尝试调用此方法时,我收到了:

javax.persistence.EntityNotFoundException: Unable to find allig.englishtrainer.dao.translation.Translation with id

如何通过自动保存翻译和Word实体来正确保存字典?

更新

在保存字典保存翻译之前,我发现了一种热门方法,使其能够通过调用工作。

translationRepository.save(translation);

但是,也许,Hibernate有一些Annotation可以自动完成吗?

0 个答案:

没有答案