本文介绍了休眠状态下的mapbyby到底是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了文档,但一无所获.我尝试在google和stackoverflow上搜索它,但仍然没有帮助.我有一个单例班,这个班和另一个班有一个单调的关系.这是我的代码.

I tried the documentation but I've got nothing. I tried searching for it on google and stackoverflow but there is still no help. I've got a singleton class and this class has a onetomany relationship to another class. Here's my code.

@Entity
public class HomeLibrary extends BaseModelObject {
    @OneToMany(mappedBy = "homeLibrary", cascade = { CascadeType.ALL })
    private Collection<Book> books = new ArrayList<Book>();

    private static HomeLibrary sharedHomeLibrary = new HomeLibrary();

    public static HomeLibrary getSharedHomeLibrary() {
        return sharedHomeLibrary;
    }

    public Collection<Book> getBooks() {
        return books;
    }

    public void setBooks(Collection<Book> books) {
        this.books = books;
    }

    private HomeLibrary() {
    }
}

我在测试中遇到错误.似乎无法建立表格.

And I got an error in testing. It seems that the table cannot be built.

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [HibernateApplicationContext-aop.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: @OneToOne or @ManyToOne on edu.fudan.ss.persistence.hibernate.Book.homeLibrary references an unknown entity: edu.fudan.ss.persistence.hibernate.HomeLibrary

推荐答案

Hibernate需要存在默认的构造函数才能创建实体,因为隐藏了构造函数,因此它无法创建HomeEntity.

Hibernate needs your default constructor to be present in order to create entity, since your constructor is hidden it cannot create your HomeEntity.

无论构造函数是公共的还是程序包可见性,休眠都应该能够使用setAccessible(true)//注意,我还没有尝试过程序包可见性选项.

Either constructor should be public or package visibility, hibernate should be be able to use setAccessible(true) // Note I havent tried package visibility option.

休眠并在一般框架中创建实体的人通过反射使用Class<T>.newInstance().

Hibernate and in general framework who create entity uses Class<T>.newInstance() via reflection.

mappedBy表示映射是由拥有方完成的,从属不需要定义映射.

mappedBy means that mapping is done by the Owning side, the dependent need not define the mapping.

因此,如果您要在HomeLibrary中定义映射,则将您放置在Book中,反之亦然.

So in your case you will place mappedBy in Book if you are defining mapping in HomeLibraryor vice versa.

帖子在SO上谈到了通过Factory方法创建对象的方法,您可以使用该方法来创建您的单例对象.

This post on SO speaks about creating objects via Factory method, you can use that to have your singleton object.

这篇关于休眠状态下的mapbyby到底是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 03:57