JPA(Hibernate)问题与OneToOne mappedBy注解 [英] JPA (Hibernate) issue with OneToOne mappedBy annotation
问题描述
在设置onetoOne关系时,我有两张表。
Bill和BillSimpleEntry。 (每个账单都有一个BillSimpleEntry
I have two tables for while I setup a oneToOne relationship. Bill and BillSimpleEntry. (Each Bill has one BillSimpleEntry
以下是他们的结构
Here is their structure
CREATE TABLE `bill` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
..
..
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`),
KEY `fk_bill_groups1_idx` (`groupId`),
KEY `fk_bill_user1_idx` (`billPayerId`),
CONSTRAINT `fk_b...` FOREIGN KEY (`groupId`) REFERENCES `groups` (`id`) ON D ELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_b...` FOREIGN KEY (`billPayerId`) REFERENCES `user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `billsimpleentry`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `billsimpleentry` (
`..` varchar(200) DEFAULT NULL,
`..` text,
`billId` bigint(20) DEFAULT NULL,
`id` bigint(20) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`),
KEY `fk_bill_idx` (`billId`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
JPA配置(来自票据实体的oneToOne关系属性片段)。
JPA config (snippet from the bill Entity for the oneToOne relationship attribute).
@OneToOne(cascade=CascadeType.ALL,mappedBy="billId",fetch = FetchType.EAGER)
private BillSimpleEntry billSimpleEntry;
我试图通过billSimpleEntry.billId和bill.Id在Bill和BillSimpleEntry之间进行连接。但我似乎得到一个错误。
I'm trying to do a Join between Bill and BillSimpleEntry by billSimpleEntry.billId and bill.Id. But I seem to get an error.
这是我得到的错误 -
Here is the error I get-
Caused by: org.hibernate.AnnotationException: Referenced property not a (One|Many)ToOne:
com.uh.br.domain.BillSimpleEntry.billId in mappedBy of
com.uh.br.domain.Bill.billSimpleEntryry
以下是实体
Bill.java
@Entity
@Table(name = "bill")
public class Bill implements GenericObject {
/**
*
*/
private static final long serialVersionUID = -5660869020353250221L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
...
..
@OneToOne(cascade=CascadeType.ALL,fetch = FetchType.EAGER)
@JoinColumn(name="billId")
private BillSimpleEntry billSimpleEntry;
...
getters & setters
...
}
BillSimpleEntry.java
@Entity
@Table(name="billsimpleentry")
public class BillSimpleEntry implements GenericObject{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long billId;
@Column(columnDefinition="TEXT")
private String itemDescription;//napkin
...
...
...
getters & setters
...
推荐答案
mappedBy
属性仅对于双向
关系是必需的,因此可以在注释中省略此元素。它在源实体上用于指向定义关系的目标实体上的字段(包含@JoinColumn)。
The mappedBy
attribute is only necessary for a bidirectional
relationship, this element can be omitted on the annotation. It is used on the source entity to point back to a field on the target entity that defines the relationship (contains @JoinColumn).
@ JoinColumn
注释应放置在 billSimpleEntry
字段上以定义应该用于连接两个表的列。在 OneToOne
的情况下适用:
The @JoinColumn
annotation should be placed upon the billSimpleEntry
field to define the column that should be used to join the two tables. In the case of a OneToOne
the following applies:
如果连接是使用外键
映射策略的OneToOne或ManyToOne映射,外键列位于源
实体的表中或可嵌入。
If the join is for a OneToOne or ManyToOne mapping using a foreign key mapping strategy, the foreign key column is in the table of the source entity or embeddable.
$ b $
Here is a code example:
@OneToOne(cascade=CascadeType.ALL,fetch = FetchType.EAGER)
@JoinColumn(name="id")
private BillSimpleEntry billSimpleEntry;
另外,如果 Bill
包含 SimpleBillEntry
字段 BILL
表应该包含 billsimpleentry $ c $的外键c>表。
Also, if the Bill
will contain the SimpleBillEntry
field the BILL
table should contain a foreign key to the billsimpleentry
table.
这篇关于JPA(Hibernate)问题与OneToOne mappedBy注解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!