Hibernate Mapping包含外键的复合主键

时间:2016-04-01 15:04:20

标签: jpa

我有2个类规则和组。规则通过另一个类RuleGroupMap映射到多个组。 DB中的相应表格如下

CREATE TABLE `rules` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `validity_start_time` timestamp NULL DEFAULT NULL,
  `validity_end_time` timestamp NULL DEFAULT NULL,
  `rule_name` char(25) DEFAULT '',
  `description` text,
  PRIMARY KEY (`id`)
)

CREATE TABLE `group` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(200) NOT NULL
  PRIMARY KEY (`id`)
)

CREATE TABLE `rule_group_map` (
  `area_id` int(11) NOT NULL DEFAULT '0',
  `group_id` bigint(11) NOT NULL DEFAULT '0',
  `active` tinyint(1) DEFAULT NULL,
  PRIMARY KEY (`group_id`,`rule_id`),
  KEY `group` (`group_id`),
  KEY `rule` (`rule_id`),
  CONSTRAINT `group` FOREIGN KEY (`group`) REFERENCES `group` (`id`),
  CONSTRAINT `rule` FOREIGN KEY (`rule_id`) REFERENCES `rules` (`id`)
)

RuleGroupMap将其PK作为rule_id和group_id的组合。此外,rule_id与Rule具有ManyToOne关系,group_id与Group具有ManyToOne关系。

如何在这种情况下在Hibernate中创建实体?

我当前的代码如下并抛出SQLGrammerException。

@Entity
@Table(name = "rules")
public class Rule implements Serializable{

    @Id @GeneratedValue(strategy = IDENTITY)
    @Getter @Setter @Column(name = "id", nullable = false, unique = true)
    private Long Id;

    @Getter @Setter @Column(name = "validity_start_time", nullable = false)
    private Timestamp validity_start;

    @Getter @Setter @Column(name = "validity_end_time", nullable = false) 
    private Timestamp validity_end;

    @Getter @Setter @Column(name = "rule_name") 
    private String ruleName;

    @Getter @Setter @Column(name = "description") 
    private String description;

    @Getter @Setter @OneToMany(mappedBy = "rule") 
    private Set<RuleGroupMap> groupsMap;
}



    @Entity
    @Table(name = "group")
    public class Group implements Serializable {

        @Id
        @Getter @Setter @Column(name = "id", nullable = false) 
        private long id;

        @Getter @Setter @Column(name = "name", nullable = false) 
        private String name;

        @Getter @Setter @OneToMany(mappedBy = "area") 
        private Set<RuleGroupMap> rulesMap;
    }


@Entity
@Table(name = "rule_group_map")
@Data
@IdClass(RuleMapPK.class)
public class RuleGroupMap {

    @Id @ManyToOne @JoinColumn(name="group_id", referencedColumnName = "id") private Group group;

    @Id @ManyToOne @JoinColumn(name="rule_id", referencedColumnName = "id") private Rule rule;

    @Column(name = "active") private boolean active;
}

@Data
public class RuleMapPK implements Serializable {
    @Column(name="group_id") long group;
    @Column(name="rule_id") Long rule;
}

2 个答案:

答案 0 :(得分:0)

在你的@IdClass中你没有声明列名,只需声明:

//geometry
geometry = new THREE.CircleGeometry(100, 12);

geometry.mergeVertices();

material = new THREE.MeshPhongMaterial( { color: 0xffa800 } );


///////////////////////////////////////////////////////////////

//CREATE NEW FACES

///////////////////////////////////////////////////////////////


var sideGeo = new THREE.Geometry(); 

//duplicate vertices
for ( var v = 0; v < geometry.vertices.length; v++ ) {

    sideGeo.vertices.push( geometry.vertices[ v ].clone() );
    sideGeo.vertices.push( geometry.vertices[ v ].clone() );

}

//translate every second vertex on Z by 10
for ( var v = 0; v < sideGeo.vertices.length; v += 2 ) {

    sideGeo.vertices[ v ].z += 100;

}

//add them to faces
for ( var v = 0; v < sideGeo.vertices.length -2; v++ ) {

    //vertices IDs
    var a = v;
    var b = v + 1;
    var c = v + 2;

    //add them to a face
    var f = new THREE.Face3(a, b, c);
    sideGeo.faces.push(f);


}

//merge with original geo and compute face normals
geometry.merge(sideGeo);

geometry.computeFaceNormals();


//mesh
mesh = new THREE.Mesh(geometry, material);

scene.add(mesh);

答案 1 :(得分:0)

MapId课程中使用RuleMap注释。使用@Embeddable@EmbeddedId也会更好,因为它们更新并且是JPA提供商推荐的。请参阅How to create a composite primary key which contains a @ManyToOne attribute as an @EmbeddedId in JPA?.How to map compound primary key in JPA, where part of Primary Key is a Foreign Key