0

I am trying to generate some tables using hibernate. I have following classes :

class Candidate {
    long candidateID;
    String candidate_name;
    List<Project> projects;
}

class Project {
    long projectID;
    Set<String> technologies;
}

and I want to generate tables like below :

+------------------------------+
      candidates
------------------------------|
candidate_id | candidate_name  
+------------------------------+

+------------------------------+
      projects 
------------------------------|
candidate_id | project_id  
+------------------------------+

+----------------------------------------+
      project_technologies 
-----------------------------------------|
candidate_id | project_id | technology_id  
+----------------------------------------+

+------------------------------+
      technologies 
-------------------------------|
technology_id | technology_name  
+------------------------------+

and currently mapping file for Project class is as follows :

<hibernate-mapping package="com.shekhar.tmpProject.model">
    <class name="Project" table="PROJECTS">
        <id name="projectID" column="PROJECT_ID" type="integer"
            unsaved-value="0">
            <generator class="native" />
        </id>
        <set name="technologies" table="PROJECT_TECHNOLOGIES">
            <key column="PROJECT_ID" />
            <element column="TECHNOLOGY_NAME" type="string" />
        </set>
        </class>
</hibernate_mapping>

but currently hibernate is not generating the tables the way I want. What I am getting now is something like as follows :

mysql> desc project_technologies;
+------------+--------------+------+-----+---------+-------+
| Field      | Type         | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| PROJECT_ID | int(11)      | NO   | PRI | NULL    |       |
| TECHNOLOGY | varchar(255) | NO   | PRI | NULL    |       |
+------------+--------------+------+-----+---------+-------+
2 rows in set (0.03 sec)

mysql> desc projects;
+--------------+------------+------+-----+---------+----------------+
| Field        | Type       | Null | Key | Default | Extra          |
+--------------+------------+------+-----+---------+----------------+
| PROJECT_ID   | int(11)    | NO   | PRI | NULL    | auto_increment |
| CANDIDATE_ID | bigint(20) | YES  | MUL | NULL    |                |
+--------------+------------+------+-----+---------+----------------+
5 rows in set (0.01 sec)

mysql> desc candidates;
+----------------+--------------+------+-----+---------+----------------+
| Field          | Type         | Null | Key | Default | Extra          |
+----------------+--------------+------+-----+---------+----------------+
| CANDIDATE_ID   | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| CANDIDATE_NAME | varchar(255) | NO   |     | NULL    |                |
+----------------+--------------+------+-----+---------+----------------+
2 rows in set (0.01 sec)

Can anyone please help me in this?

1
  • Why do you want to have candidate_id in project_technologies? I think the project_id would be enouth Commented Nov 2, 2011 at 7:55

1 Answer 1

1

You will have to design a Technology entity if you want to have control over its layout.

class Technology {
   long technologyId;
   String technologyName;
}

In your Project mapping you will have to use a <many-to-many> mapping instead of <element>

EDIT

You will always have to create concrete classes for unique Entities if you are using hibernate. For further inheritance mapping see following documentation, which is the other way around. (Multiple entities - one or more tables)

http://docs.jboss.org/hibernate/core/3.3/reference/en/html/inheritance.html

Another chance is to use the abstract attribute in your <class> in combination with <union-subclass> which is explained in detail here:

http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-declaration-unionsubclass

Sign up to request clarification or add additional context in comments.

5 Comments

Similar to technology, I have other fields also with the same structure. For example, I have a list of clients and employers which are basically organization names. So, if I have to create new class for 'technology' then does it mean that I have to create new class for employer and client also?
Yes, of course. But you have always the chance to use inhertitance and abstraction to DRY (don't repeat yourself)
How to implement this using inheritance? Actually I want to avoid creating separate classes for technology, clients, employers as those are only strings and I want to avoid duplication of data.
You do have to write own classes... Write an AbstractNameableEntity with id and name column and let Technology, Organisation, etc extend it. For details on mappings, see my coming EDIT
Alternatively you can use <element> for simple strings, but then you won't get id's for your new entities

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.