[英]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.