Paul T Davies
Paul T Davies

Reputation: 2573

Hibernate hbm mapping file referencing entity in another project

I have two projects in an Eclipse workspace: 'myproject-main' references 'myproject-domain' (It is referenced by adding it to the build path. 'myproject-domain' contains my domain entities (POJOs), and nothing else. Everything builds correctly, and any references from classes in 'myproject-main' to entities in 'myproject-domain' are fine.

The problem is I have Hibernate hbm.xml files in 'myproject-main' that reference entities in 'myproject-domain'.

This is in 'myproject-main', along with all other Hibernate and Spring setup:

<class name="uk.co.luciditysoftware.myproject.domain.entities.User" table="`User`">

  <id name="id" column="`Id`" type="uuid-char">
    <generator class="assigned" />
  </id>

  <property name="firstName">
    <column name="`FirstName`" length="50" not-null="true" />
  </property>

  <property name="lastName">
    <column name="`LastName`" length="50" not-null="true" />
  </property>

</class>

and this class in in 'myproject-domain':

package uk.co.luciditysoftware.myproject.domain.entities;

import java.util.UUID;

public class User {
  private UUID id;
  private String firstName;
  private String lastName;

  public UUID getId() {
    return id;
  }

  public void setId(UUID id) {
    this.id = id;
  }

  public String getFirstName() {
    return firstName;
  }

  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }

  public String getLastName() {
    return lastName;
  }

  public void setLastName(String lastName) {
    this.lastName = lastName;
  }
}

If I included the user entity in the 'myproject-main' project, everything works as expected, so there's nothing wrong with my Hibernate/Spring setup, it just has a problem finding the User entity when it is in a referenced project.

The expection stack raised when starting the project has

java.lang.ClassNotFoundException: uk.co.luciditysoftware.myproject.domain.entities.User 

at the bottom with

org.hibernate.MappingException: class uk.co.luciditysoftware.myproject.domain.entities.User not found while looking for property 

next in the stack.

Upvotes: 1

Views: 845

Answers (1)

Paul T Davies
Paul T Davies

Reputation: 2573

I worked it out. I was referencing myproject-domain from my-project-main by going:

myproject-main -> right click -> Properties -> Java Build Path -> Projects -> Add

and adding myproject domain there, but you also need to do:

Main menu -> Run -> Run Configurations... -> Apache Tomcat -> Tomcat v8.0 server -> Classpath tab -> Add Projects... 

and add myproject-domain from there, as mentioned in this question: calling a class in another project Eclipse

Upvotes: 1

Related Questions