无法使用hibernate生成表

时间:2011-11-01 09:12:21

标签: java hibernate hibernate-mapping

我正在尝试使用hibernate生成一些表。我有以下课程:

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

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

我想生成如下表格:

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

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

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

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

目前Project类的映射文件如下:

<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>

但是目前hibernate没有按照我想要的方式生成表。我现在得到的是如下:

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)

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

如果您想控制其布局,则必须设计一个Technology实体。

class Technology {
   long technologyId;
   String technologyName;
}

在项目映射中,您必须使用<many-to-many>映射而不是<element>

修改

如果您正在使用休眠,则必须始终为唯一实体创建具体类。有关进一步的继承映射,请参阅以下文档,这是另一种方法。 (多个实体 - 一个或多个表)

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

另一种可能性是使用abstract中的<class>属性与<union-subclass>结合使用,详细说明如下:

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