jpa onetoone_JPA(Hibernate)问题与OneToOne mappedBy注解

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 config (snippet from the bill Entity for the oneToOne relationship attribute).

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

private BillSimpleEntry 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

Here are the entities

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

...

解决方案

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

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:

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.

Here is a code example:

@OneToOne(cascade=CascadeType.ALL,fetch = FetchType.EAGER)

@JoinColumn(name="id")

private BillSimpleEntry billSimpleEntry;

Also, if the Bill will contain the SimpleBillEntry field the BILL table should contain a foreign key to the billsimpleentry table.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值