Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Hibernate的单类复合模式映射_Hibernate_Annotations_Hierarchy_Composite - Fatal编程技术网

使用Hibernate的单类复合模式映射

使用Hibernate的单类复合模式映射,hibernate,annotations,hierarchy,composite,Hibernate,Annotations,Hierarchy,Composite,我想知道如何在Hibernate中映射带有注释的单类复合模式?人类对象可以包含子对象,每个子对象都可以有自己的子对象。多谢各位 人类领域类别: @Entity public class Human implements Serializable { private Human parent; @ManyToOne @JoinColumn(name = "parent_id") public Human getParent() { retu

我想知道如何在Hibernate中映射带有注释的单类复合模式?人类对象可以包含子对象,每个子对象都可以有自己的子对象。多谢各位

人类领域类别:

@Entity
public class Human implements Serializable
{
    private Human parent;

    @ManyToOne
    @JoinColumn(name = "parent_id")
    public Human getParent()
    {
        return parent;
    }

    public void setParent(Human parent)
    {
        this.parent = parent;
    }

    private List<Human> children = new ArrayList<Human>();

    @OneToMany(mappedBy = "parent", targetEntity = Human.class, fetch=FetchType.EAGER, cascade=CascadeType.ALL)
    public List<Human> getChildren()
    {
        return children;
    }

    @Id
    @GeneratedValue
    private Long id = null;

    private int version = 0;
    private String name;
    private int age;

    public String getName()
    {
        return name;
    }

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

    public int getAge()
    {
        return age;
    }

    public void setAge(int age)
    {
        this.age = age;
    }

    @OneToMany(mappedBy = "parent", targetEntity = Human.class, fetch=FetchType.EAGER, cascade=CascadeType.ALL)
    public void addChild(Human child)
    {
        child.setParent(this);
        children.add(child);    
    }
}
public class Main
{

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        System.out.println("start");
        Main main = new Main();
        main.run();
        main.run();
        System.out.println("stop");
    }

    public void run()
    {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();//.openSession();
        Transaction tx = session.beginTransaction();

        int len = 2;
        for (int i = 0; i < len; i++)
        {
            Human human = new Human();
            human.setName("name" + i);
            human.setAge(i);

            session.save(human);

            int clen = 2;
            for (int j = 0; j < clen; j++)
            {
                Human child = new Human();
                child.setName("cname" + j);
                child.setAge(j);

                human.addChild(child);
                session.save(child);
            }
        }

        tx.commit();
    }
}
HibernateUtil类:

public class HibernateUtil
{
    private static final SessionFactory sessionFactory;

    static
    {
        try
        {
            AnnotationConfiguration config = new AnnotationConfiguration();
            config.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
            config.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
            config.setProperty("hibernate.connection.url", "jdbc:mysql://localhost/test");
            config.setProperty("hibernate.connection.username", "root");
            config.setProperty("hibernate.connection.password", "");
            config.setProperty("hibernate.connection.pool_size", "1");
            config.setProperty("hibernate.connection.autocommit", "true");
            config.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.NoCacheProvider");
            config.setProperty("hibernate.hbm2ddl.auto", "create");
            config.setProperty("hibernate.show_sql", "true");
            config.setProperty("hibernate.transaction.factory_class", "org.hibernate.transaction.JDBCTransactionFactory");
            config.setProperty("hibernate.current_session_context_class", "thread");

            config.addAnnotatedClass(Human.class);

            sessionFactory = config.buildSessionFactory();
        }
        catch (Throwable ex)
        {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory()
    {
        return sessionFactory;
    }
}

您肯定需要从
addChild
方法中删除
@OneToMany
。它没有添加任何内容,但可能正在摆脱休眠状态。试试看,然后公布结果


更新。这就是它失败的原因。您可以在
parent
字段上设置
@Id
。这迫使Hibernate查看所有字段的映射,而不是通过getter/setter指定的属性。因此,它会忽略您的
@JoinColumn
注释,并将
父项
字段视为到具有默认名称
父项
的列的映射


将所有注释移动到getter或fields,这将解决列名映射不正确的问题。

您肯定需要从
addChild
方法中删除
@OneToMany
。它没有添加任何内容,但可能正在摆脱休眠状态。试试看,然后公布结果


更新。这就是它失败的原因。您可以在
parent
字段上设置
@Id
。这迫使Hibernate查看所有字段的映射,而不是通过getter/setter指定的属性。因此,它会忽略您的
@JoinColumn
注释,并将
父项
字段视为到具有默认名称
父项
的列的映射


将所有注释移动到getter或fields,应该可以解决列名映射不正确的问题。

发布SQL表def plz(以消除简单错误)。我没有表def,因为它是动态生成的。config.setProperty(“hibernate.hbm2ddl.auto”、“create”);发布您的SQL表def plz(以消除简单错误)我没有表def,因为它是动态生成的。config.setProperty(“hibernate.hbm2ddl.auto”、“create”);谢谢,它起作用了。我已将字段上的所有批注移动到getter。谢谢,它很有效。我已将字段上的所有注释移动到getter。
public class HibernateUtil
{
    private static final SessionFactory sessionFactory;

    static
    {
        try
        {
            AnnotationConfiguration config = new AnnotationConfiguration();
            config.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
            config.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
            config.setProperty("hibernate.connection.url", "jdbc:mysql://localhost/test");
            config.setProperty("hibernate.connection.username", "root");
            config.setProperty("hibernate.connection.password", "");
            config.setProperty("hibernate.connection.pool_size", "1");
            config.setProperty("hibernate.connection.autocommit", "true");
            config.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.NoCacheProvider");
            config.setProperty("hibernate.hbm2ddl.auto", "create");
            config.setProperty("hibernate.show_sql", "true");
            config.setProperty("hibernate.transaction.factory_class", "org.hibernate.transaction.JDBCTransactionFactory");
            config.setProperty("hibernate.current_session_context_class", "thread");

            config.addAnnotatedClass(Human.class);

            sessionFactory = config.buildSessionFactory();
        }
        catch (Throwable ex)
        {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory()
    {
        return sessionFactory;
    }
}