简体   繁体   English

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

[英]hibernate MappingException: Could not determine type for: java.util.Set

I'm new to Java and Hibernate (although I've familiar with the Entity Framework so understand a lot of the concepts) 我是Java和Hibernate的新手(尽管我熟悉实体框架,因此理解了很多概念)

All I want to do is create a Table OrganisationNode which has the following fields: Id , Name and ParentId 我想要做的就是创建一个Table OrganisationNode ,其中包含以下字段: IdNameParentId

ParentId is Nullable and, if present, should point at a valid OranisationNode.Id . ParentId是Nullable,如果存在,则应指向有效的OranisationNode.Id

I'd assumed this should be fairly simple but having spent over 2 days googling and editing XML files, I'm starting to lose patience. 我认为这应该相当简单,但是花了两天时间用Google搜索和编辑XML文件,我开始失去耐心。

Let me start with the code snippets... 让我从代码片段开始......

OrganisationNode.java 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 EntityBase.java

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

    //Getter/Setter
}

Hibernate.config.xml 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 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 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;
    }
}

The exception I currently get is ... 我目前得到的例外是......

javax.servlet.ServletException: org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: OrganisationNodes, for columns: [org.hibernate.mapping.Column(Children)] javax.servlet.ServletException:org.hibernate.MappingException:无法确定类型:java.util.Set,在表:OrganisationNodes,用于列:[org.hibernate.mapping.Column(Children)]

So, my first question is how can I resolve this exception? 所以,我的第一个问题是如何解决这个异常? I believe I've explicitly told it what Children should be pointing at. 我相信我已明确告诉Children应该指出什么。

My next question is... Is this all really necessary? 我的下一个问题是......这一切真的有必要吗? I find it hard to believe that getting a single table up and running on an ORM takes so much configuration and code. 我发现很难相信在ORM上运行单个表需要如此多的配置和代码。 Most of what I've got here has been hacked from various examples on the web so I'm unsure of the quality. 我在这里得到的大部分内容都是在网上的各种例子中被黑客攻击所以我不确定它的质量。 Surely 90% of this information must be available to hibernate via reflection? 肯定有90%的信息必须通过反射来休眠吗?

You are mixing xml-mapping and annotations here. 您在这里混合xml-mapping和注释。 Annotations alone are enough in you case. 在你的情况下,仅注释就足够了。 First remove Hibernate.hbm.xml. 首先删除Hibernate.hbm.xml。

OrganisationNode.java 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 EntityBase.java

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

}

Hibernate.config.xml 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>

You should either use "Hibernate.hbm.xml" or use annotations. 您应该使用“Hibernate.hbm.xml”或使用注释。 You are trying to use both and I think that's confusing hibernate. 你试图使用两者,我认为这是令人困惑的休眠。

Everything you are doing in "Hibernate.hbm.xml" can be put into the entity classes as annotations instead. 您在“Hibernate.hbm.xml”中执行的所有操作都可以作为注释放入实体类中。 This makes things much simpler: 这使事情变得更简单:

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

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

I would get rid of EntityBase too. 我也会摆脱EntityBase It doesn't give you much by the look of it, and extending base classes in Hibernate can be painful. 它的外观并没有给你太多,并且在Hibernate中扩展基类会很痛苦。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 多对多关系:org.hibernate.MappingException:无法确定以下类型:java.util.Set - many to many relationship : org.hibernate.MappingException: Could not determine type for: java.util.Set 无法构建 Hibernate SessionFactory; 嵌套异常是 org.hibernate.MappingException:无法确定类型:java.util.Set,在表中: - Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: org.hibernate.MappingException:无法确定类型:java.util.Set,在表:Company中,用于列:[org.hibernate.mapping.Column(users)] - org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: Company, for columns: [org.hibernate.mapping.Column(users)] 获取 org.hibernate.MappingException:无法确定类型:java.util.Set,在表:使用@ElementCollection 后的作者 - Getting a org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: Authors after using @ElementCollection 使用@ElementCollection时出错:org.hibernate.MappingException:无法确定类型:java.util.Set,在表:对于列 - Error while using @ElementCollection: org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: for columns 基本的JPA问题:“无法确定类型:java.util.Set” - Basic JPA issue: “Could not determine type for: java.util.Set” 无法确定:java.util.Set的类型,在表中 - Could not determine type for: java.util.Set, at table MappingException:无法确定java.util.Set的类型 - MappingException: Type can not be determined for java.util.Set org.hibernate.MappingException:无法确定以下类型:java.util.Comparator - org.hibernate.MappingException: Could not determine type for: java.util.Comparator ManyToMany:MappingException:无法确定以下类型:java.util.List - ManyToMany : MappingException: Could not determine type for: java.util.List
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM