hibernate MappingException:无法确定类型:java.util.Set

时间:2013-02-25 16:03:55

标签: java hibernate hibernate-mapping

我是Java和Hibernate的新手(尽管我熟悉实体框架,因此理解了很多概念)

我要做的就是创建一个包含以下字段的表OrganisationNodeIdNameParentId

ParentId是Nullable,如果存在,则应指向有效的OranisationNode.Id

我认为这应该相当简单,但是花了两天时间搜索和编辑XML文件,我开始失去耐心。

让我先从代码片段开始......

OrganisationNode.java

@javax.persistence.Entity
public class OrganisationNode extends EntityBase implements Serializable {
    private String name;

    @ManyToOne(targetEntity = OrganisationNode.class, optional = true)
    @JoinColumn(name="ParentId", referencedColumnName="Id") //This is one example of this attribute. I've tried many combinations
    private OrganisationNode parent;

    @OneToMany
    private Set<OrganisationNode> children = new HashSet<OrganisationNode>();

    //Getters/Setters
}

EntityBase.java

@javax.persistence.Entity
public class EntityBase implements Serializable {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

    //Getter/Setter
}

Hibernate.config.xml

<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://mysqldev:3306/SomeDb?zeroDateTimeBehavior=convertToNull</property>
    <property name="hibernate.connection.username">SomeUser</property>
    <property name="hibernate.connection.password">SomePass!</property>
    <property name="hibernate.hbm2ddl.auto">create</property>
    <property name="show_sql">true</property>
    <mapping resource="hibernate.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

Hibernate.hbm.xml

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.overshare.document.entities.OrganisationNode" table="OrganisationNodes">
        <id name="id"><generator class="native"/></id>
        <property name="name"/>
        <property name="children"/>
        <property name="parent"/>
    </class>
</hibernate-mapping>

HibernateUtil.java

public class HibernateUtil {

    private static SessionFactory sessionFactory;

    public static void configureSessionFactory() {

            Configuration configuration = new Configuration();
            configuration.configure("hibernate.cfg.xml");
            ServiceRegistryBuilder serviceRegistryBuilder = new ServiceRegistryBuilder().applySettings(configuration.getProperties());
            sessionFactory = configuration.buildSessionFactory(serviceRegistryBuilder.buildServiceRegistry());
    }

    public static SessionFactory getSessionFactory() {
        if(sessionFactory == null){configureSessionFactory();}
        return sessionFactory;
    }
}

我目前得到的例外是......

  

javax.servlet.ServletException:org.hibernate.MappingException:无法确定类型:java.util.Set,在表:OrganisationNodes,用于列:[org.hibernate.mapping.Column(Children)]

所以,我的第一个问题是如何解决这个异常?我相信我已明确告诉它Children应该指出什么。

我的下一个问题是......这一切真的有必要吗?我发现很难相信在ORM上运行单个表需要如此多的配置和代码。我在这里得到的大部分内容都是在网上的各种例子中被黑客攻击所以我不确定它的质量。肯定有90%的信息可以通过反射来休眠吗?

2 个答案:

答案 0 :(得分:2)

您正在混合xml-mapping和注释。在你的情况下,仅注释就足够了。首先删除Hibernate.hbm.xml。

OrganisationNode.java

@Entity
public class OrganisationNode extends EntityBase {

    private String name;
    private String description;

    @ManyToOne
    @JoinColumn(name="ParentId")
    private OrganisationNode parent;

    @OneToMany
    private Set<OrganisationNode> children = new HashSet<OrganisationNode>();

}

EntityBase.java

@Entity
public class EntityBase {
    @Id
    @GeneratedValue
    private Long id;

}

Hibernate.config.xml

<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://mysqldev:3306/SomeDb?zeroDateTimeBehavior=convertToNull</property>
    <property name="hibernate.connection.username">SomeUser</property>
    <property name="hibernate.connection.password">SomePass!</property>
    <property name="hibernate.hbm2ddl.auto">create</property>
    <property name="show_sql">true</property>

    <mapping class="com.overshare.document.entities.OrganisationNode"/>
  </session-factory>
</hibernate-configuration>

答案 1 :(得分:1)

您应该使用“Hibernate.hbm.xml”或使用注释。你试图使用两者,我认为这是令人困惑的休眠。

您在“Hibernate.hbm.xml”中所做的一切都可以作为注释放入实体类中。这使得更多更简单:

@Entity
@Table(name = "OrganisationNodes")
public class OrganisationNode implements Serializable {
    @Column(name = "NAME")
    private String name;

    @Column(name = "DESCRIPTION")
    private String description;
    etc.

我也会摆脱EntityBase。它的外观并没有给你太多,而在Hibernate中扩展基类会很痛苦。