7

在设置 oneToOne 关系时,我有两个表。比尔和比尔SimpleEntry。(每个 Bill 都有一个 BillSimpleEntry

这是他们的结构

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 关系属性的片段)。

@OneToOne(cascade=CascadeType.ALL,mappedBy="billId",fetch = FetchType.EAGER)
private BillSimpleEntry billSimpleEntry;

我正在尝试通过 billSimpleEntry.billId 和 bill.Id 在 Bill 和 BillSimpleEntry 之间进行连接。但我似乎得到了一个错误。

这是我得到的错误-

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

1 回答 1

18

mappedBy属性仅对bidirectional关系是必需的,注释上可以省略该元素。它用于源实体以指向目标实体上定义关系的字段(包含@JoinColumn)。

@JoinColumn注释应放置在字段billSimpleEntry上以定义应用于连接两个表的列。在以下情况OneToOne下适用:

如果连接是针对使用外键映射策略的 OneToOne 或 ManyToOne 映射,则外键列在源实体的表中或可嵌入的。

这是一个代码示例:

@OneToOne(cascade=CascadeType.ALL,fetch = FetchType.EAGER)
@JoinColumn(name="id")
private BillSimpleEntry billSimpleEntry;

此外,如果Bill将包含该SimpleBillEntry字段,则该BILL表应包含该表的外键billsimpleentry

于 2013-08-18T20:54:53.707 回答