Hibernate批注使用复合主键一对一映射单向关联 [英] Hibernate annotations to map one to one unidirectional association with composite primary key

查看:61
本文介绍了Hibernate批注使用复合主键一对一映射单向关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表:report和flyleaf.报表是由"id"和"index"标识的,这是flyleaf表中的外键和主键.

I have two tables : report and flyleaf. A report is identified by an 'id' and an 'index' which are foreign and primary keys in flyleaf table.

表的架构为:

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`);

我使用Hibernate注释以单向方式映射此关联.当我使用一个简单的键(非复合键)时,它显然可以很好地工作,但是当我尝试使用复合键时,会抛出"org.hibernate.TypeMismatchException"消息:为lgmi_cr.Flyleaf类提供了错误类型的ID.预期:类lgmi_cr.Flyleaf,获得了类lgmi_cr.Report"

I use Hibernate annotation to map this association in unidirectional way. 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"

如果某人已经看到相同的案例,或者对如何以单向一对一的方式表示此关联有任何想法,将不胜感激.

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.

:这是ID进行映射的方式

EDIT : This is how Id did the mapping

@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;
....

我得到了这样的异常"org.hibernate.TypeMismatchException:为类lgmi_cr.Flyleaf提供了错误类型的ID.应该:类lgmi_cr.Flyleaf,得到了类lgmi_cr.Report"

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"

推荐答案

我希望您能分享如何在Hibernate中映射这些关系?这就是我的方法:

I wish you could share how you're mapping these relationships in Hibernate? This is how I would do it:

1)使用@Embeddable注释将每个组合表映射为单独的类对象,例如:

1) Map each composite table as a separate class object using @Embeddable annotation, e.g:

@Embeddable
public class ReportPK implements Serializable {

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

实体类如下:

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

  @EmbeddedId
  protected ReportPK id;
...

2)在flyleaf实体类中,可以将单向映射添加为:

2) In the flyleaf entity class, you can add the unidirectional mapping as:

@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. :-)

这篇关于Hibernate批注使用复合主键一对一映射单向关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆