Hibernate annotations to map one to one unidirectional association with composite primary key

强颜欢笑 提交于 2019-12-24 09:11:10

问题


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.

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

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

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"


回答1:


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

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

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:

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



来源:https://stackoverflow.com/questions/42840293/hibernate-annotations-to-map-one-to-one-unidirectional-association-with-composit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!