hibernate将3个实体映射到一个连接表

时间:2016-10-31 13:56:02

标签: java postgresql hibernate jpa annotations

我有3个表skill_checklist,skill_group,skill_master和一个映射表skill_checklist_group_master

该关联是

checklist <-- many to many --> Group 
Group <-- many to many --> skills 

有人可以告诉我如何使用注释在hibernate中管理这个3表关联吗?

以下是我的表结构

CREATE TABLE public.skill_checklist
(
  id bigint NOT NULL DEFAULT nextval('skill_checklist_id_seq'::regclass),
  name character varying(256) NOT NULL,
  description character varying(500) NOT NULL,
  CONSTRAINT "PK_SKILL_CHECKLIST_ID" PRIMARY KEY (id)
)

CREATE TABLE public.skill_group
(
  id bigint NOT NULL DEFAULT nextval('skill_group_id_seq'::regclass),
  name character varying(256) NOT NULL,
  description character varying(500) NOT NULL,
  CONSTRAINT "PK_SKILL_GROUP_ID" PRIMARY KEY (id)
)

CREATE TABLE public.skill_master
(
  id bigint NOT NULL DEFAULT nextval('skill_master_id_seq'::regclass),
  name character varying(256) NOT NULL,
  description character varying(500) NOT NULL,
  CONSTRAINT "PK_SKILL_MASTER_ID" PRIMARY KEY (id)
)

CREATE TABLE public.skill_checklist_group_master
(
  checklist_id bigint NOT NULL,
  group_id bigint NOT NULL,
  skill_id bigint NOT NULL,
  group_order bigint NOT NULL,
  skill_order bigint NOT NULL,
  CONSTRAINT "FK_MAP_CHECKLIST_CHECKLIST_ID" FOREIGN KEY (checklist_id)
      REFERENCES public.skill_checklist (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "FK_MAP_GROUP_GROUP_ID" FOREIGN KEY (group_id)
      REFERENCES public.skill_group (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "FK_MAP_SKILL_SKILL_ID" FOREIGN KEY (skill_id)
      REFERENCES public.skill_master (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)

我没有hibernate的经验,因此编码示例肯定会帮助我。使用最新版本的hibernate。

1 个答案:

答案 0 :(得分:0)

使用Hibernate,您可以定义实体中的每个数据表。每个实体都有自己的专栏注释。

这是一个例子..

fieldset {
      display: block;
      margin: 20px 1%;
      margin-bottom: 20px;
      padding: 0 auto;
      padding: 15px 0;
      border: 0;
      border-top: 1px solid #DDD;
      width: 98%;
}

fieldset:last-of-type {
      margin-bottom: 0px;
}

legend {
     display: table; 
     min-width: 0px;
     max-width: 70%;
     position: relative;
     margin: auto;
     padding: 5px 20px;
     color: #eee8aa;
     font-size: 20px;
     text-align: center;
}

如何看待每个实体都拥有自己的注释,并拥有在其列中获取和设置数据的方法..

如果你使用eclipse或netbeans,他们有自己的插件可以自动转换实体中的数据表..