I'm new to Java and Hibernate (although I've familiar with the Entity Framework so understand a lot of the concepts)
All I want to do is create a Table OrganisationNode
which has the following fields: Id
, Name
and ParentId
ParentId
is Nullable and, if present, should point at a valid 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.
Let me start with the code snippets...
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;
}
}
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)]
So, my first question is how can I resolve this exception? I believe I've explicitly told it what Children
should be pointing at.
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. 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?