Unable to generate tables using hibernate

 ̄綄美尐妖づ 提交于 2019-12-13 04:04:07

问题


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:


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



来源:https://stackoverflow.com/questions/7964417/unable-to-generate-tables-using-hibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!