如何做这个hibernate映射?

时间:2011-11-29 18:07:58

标签: hibernate orm mapping hibernate-mapping

我是休眠世界的新手,所以在以下条件下编写映射文件时会有些困惑。

我想使用hibernate继续使用以下类:

class Project {
    int projectID;
    List<String> clients;
    List<String> employers;

    List<String> technologies;
    String description;
}

我已在数据库中手动创建了以下表格:

+---------------------------+
           project
+---------------------------+

 project_id (pk) | description
+---------------------------+

+---------------------------+    
          organizations
+---------------------------+
  org_id(pk)      | org_name
+---------------------------+

要链接organizationproject表,我有以下表格:

+-------------------------------------------------------------+
                      project_clients
+-------------------------------------------------------------+
  project_id(fk of projects)  | client_id(fk of organizations)
+-------------------------------------------------------------+

+-------------------------------------------------------------+
                     project_employers
+-------------------------------------------------------------+
  project_id(fk of projects) | employer_id(fk of organizations)
+-------------------------------------------------------------+

我不知道如何将这些表映射到我的project类?知道怎么做吗?

我尝试利用列表但是使用列表持久性我们不能使用类似(project_clientsproject_employers)的链接表,我不想为客户列表和雇主列表创建单独的类。或者它是唯一的选择?

请帮忙!


编辑1 谢谢你的回答。我还有一个问题。如何告诉hibernate对于客户端(例如'foo company')它应该在组织表中搜索'foo company',如果该名称存在,则获取其相应的ID并将其放在project_clients表中。如果“组织”表中没有“foo公司”,则添加它,然后获取相应的ID。我也想为雇主做同样的事情。是否有可能在休眠中做这件事?

目前我已用java编写程序来完成所有这些工作。


2 个答案:

答案 0 :(得分:1)

我认为以下映射可以解决您的问题

  @Entity
  @Table(name="project")
  class Project {

    @Id
    @GeneratedValue
    int projectID;
    String description;
    //for joing the tables (many-to-many)
    @ManyToMany(cascade=CascadeType.ALL)
    @JoinTable(name = "project_clients",
    joinColumns = {
               @JoinColumn(name="projectId") 
             },
    inverseJoinColumns = {@JoinColumn(name="clientId")})

    private Set<Organization> clients;

   public Set<Organization> getClients(){ 
        return clients; 
   }
   public void setClients(Set<Organization> clients){ 
        this.clients= clients; 
   }

   //same for employers

   @ManyToMany(cascade=CascadeType.ALL)
   @JoinTable(name = "project_employers",
   joinColumns = {
               @JoinColumn(name="projectId") 
             },
   inverseJoinColumns = {@JoinColumn(name="employerId")})

   private Set<Organization> employers;

   public Set<Organization> getEmployers(){ 
        return employers; 
   }
   public void setEmployers(Set<Organization> employers){ 
        this.employers= employers; 
   }

}

答案 1 :(得分:0)

如果您正在寻找一种直接的方式来实现您的目标:

import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
class Project {
    @Id
    @GeneratedValue
    int projectID;
    @ElementCollection
    List<String> clients;
    @ElementCollection
    List<String> employers;
    @ElementCollection
    List<String> technologies;
    String description;
}

请注意,不推荐使用@CollectionsOfElements,因此最好使用@ElementCollection

让我知道它是否适合你!