gpt4 book ai didi

java - hibernate MappingException : Could not determine type for: java. util.Set

转载 作者:行者123 更新时间:2023-11-29 05:48:05 28 4
gpt4 key购买 nike

我是 Java 和 Hibernate 的新手(虽然我熟悉 Entity Framework ,所以了解很多概念)

我想做的就是创建一个表 OrganisationNode,它具有以下字段:IdNameParentId

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

我原以为这应该相当简单,但在谷歌搜索和编辑 XML 文件上花了 2 天多的时间,我开始失去耐心了。

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

组织节点.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
}

实体库.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>

hibernate 工具.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: Could not determine type for: java.util.Set, at table: OrganisationNodes, for columns: [org.hibernate.mapping.Column(Children)]

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

我的下一个问题是……这一切真的有必要吗?我很难相信在 ORM 上启动并运行单个表需要如此多的配置和代码。我在这里得到的大部分内容都是从网络上的各种示例中破解而来的,所以我不确定质量。这些信息中的 90% 肯定可以通过反射进入 hibernate 状态吗?

最佳答案

您在这里混合了 xml 映射和注释。在您的情况下,仅注释就足够了。首先删除 Hibernate.hbm.xml。

组织节点.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>();

}

实体库.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>

关于java - hibernate MappingException : Could not determine type for: java. util.Set,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15071116/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com