简体   繁体   English

Hibernate批注使用复合主键一对一映射单向关联

[英]Hibernate annotations to map one to one unidirectional association with composite primary key

I have two tables : report and flyleaf. 我有两个表:report和flyleaf。 A report is identified by an 'id' and an 'index' which are foreign and primary keys in flyleaf table. 报告由“ id”和“ index”标识,这是flyleaf表中的外键和主键。

The schema of the tables are : 这些表的架构为:

CREATE TABLE `report` (
   `id` int(11) NOT NULL,
   `index` varchar(5) NOT NULL,
   `nb_page` int(11) DEFAULT NULL
); 
ALTER TABLE `report` ADD PRIMARY KEY (`id`,`index`);

CREATE TABLE `flyleaf_t` (
   `id` int(11) NOT NULL,
   `index` varchar(5) NOT NULL,
   `title` varchar(30) DEFAULT NULL,
   `author` varchar(30) DEFAULT NULL,
   `checker` varchar(30) DEFAULT NULL
);
ALTER TABLE `flyleaf` ADD PRIMARY KEY (`id`,`index`);
ALTER TABLE `flyleaf` ADD CONSTRAINT `flyleaf_ibfk_1` FOREIGN KEY (`id`,`index`) REFERENCES `report` (`id`, `index`);

I use Hibernate annotation to map this association in unidirectional way. 我使用Hibernate注释以单向方式映射此关联。 When I use a simple keys(non composite) it works well obviously, but when I try to use composite keys an "org.hibernate.TypeMismatchException" is thrown with the message : "Provided id of the wrong type for class lgmi_cr.Flyleaf. Expected: class lgmi_cr.Flyleaf, got class lgmi_cr.Report" 当我使用简单的键(非复合键)时,它显然工作良好,但是当我尝试使用复合键时,消息“ org.hibernate.TypeMismatchException”被抛出:“为类lgmi_cr.Flyleaf提供了错误类型的ID。预期:类lgmi_cr.Flyleaf,获得了类lgmi_cr.Report“

If someone already saw the same case or have an idea about how to represent this association in unidirectional one-to-one way, its help will be appreciated. 如果有人已经看到相同的案例,或者对如何以单向一对一的方式表示此关联有任何想法,将不胜感激。

EDIT : This is how Id did the mapping 编辑:这就是我的ID做映射的方式

@Entity
@Table(name="report")
public class Report implements Serializable{

    @Id
    @Column(name = "id")
    private int id;

    @Id
    @Column(name = "index")
    private String index;

    @OneToOne 
    @JoinColumns({@JoinColumn(name="id", referencedColumnName="id"),
            @JoinColumn(name="index", referencedColumnName="index")})
    private Flyleaf flyleaf;
...

@Entity
@Table(name="flyleaf")
public class Flyleaf implements Serializable {

    @Id
    @Column(name = "id")
    private int id;

    @Id
    @Column(name = "index")
    private String index;
....

And I got this exception "org.hibernate.TypeMismatchException: Provided id of the wrong type for class lgmi_cr.Flyleaf. Expected: class lgmi_cr.Flyleaf, got class lgmi_cr.Report" 我得到了这个异常“ org.hibernate.TypeMismatchException:为类lgmi_cr.Flyleaf提供了错误类型的ID。预期:类lgmi_cr.Flyleaf,得到了类lgmi_cr.Report”

I wish you could share how you're mapping these relationships in Hibernate? 我希望您可以分享如何在Hibernate中映射这些关系? This is how I would do it: 这就是我要做的:

1) Map each composite table as a separate class object using @Embeddable annotation, eg: 1)使用@Embeddable注解将每个组合表映射为单独的类对象,例如:

@Embeddable
public class ReportPK implements Serializable {

  @Basic(optional = false)
  @Column(name="id")
  private int id;
  @Basic(optional = false)
  @Column(name="index")
  private String index;
...

And the entity class would look like: 实体类如下所示:

@Table(name="report")
public class Report implements Serializable {

  @EmbeddedId
  protected ReportPK id;
...

2) In the flyleaf entity class, you can add the unidirectional mapping as: 2)在flyleaf实体类中,可以将单向映射添加为:

@OneToOne
  @JoinColumns({
    @JoinColumn(name="id",
      referencedColumnName="id"),
    @JoinColumn(name="index",
      referencedColumnName="index"),
  })
  private Report report;
...

Perhaps you can be more specific if this doesn't help. 如果这样做没有帮助,也许您可​​以更具体一些。 :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何在Hibernate中映射多对一关联,其中孩子有一个组合键,其中一部分是父级的主键? - How do I map a many-to-one association in Hibernate where the child has a composite key and a part of that is the primary key in the parent? 休眠5,将外键作为主键,单向一对一 - hibernate 5, foreign key as primary key, unidirectional one to one JPA / Hibernate使用共享主键进行单向一对一映射 - JPA / Hibernate unidirectional one-to-one mapping with shared primary key 休眠一对一单向主键XML映射 - Hibernate One to One Unidirectional Primary key XML mapping 休眠中具有相同主键的一对一单向映射 - One to One unidirectional mapping with same primary key in hibernate Hibernate中没有组合键的一对多关联 - One to many association in Hibernate without composite key 带有反向主外键级联的零到一单向休眠 - Hibernate Zero to One unidirectional with reverse primary foreign key cascade 单向一对多,父级具有复合密钥 - Unidirectional one to many, parent has composite key 我如何 map 与 JPA(休眠)中的复合主键建立一对多关系? - How do I map a One To Many Relationship with Composite Primary Key in JPA (Hibernate)? Hibernate / JPA-复合键子级的一个主键父级 - Hibernate/JPA - one primary key parent to composite key child
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM