hibernate mysql 外键查询null_使用Hibernate / JPA的@OneToOne映射中的外键字段为null

我有两个实体,Document和BodyElement,并试图用Hibernate 4.2来保持它们. mtdt_t正确填充,但mtdt_body_t表中的外键docid为NULL.

我看到hibernate试图在没有docid值的情况下插入.插入mtdt_body_t values()

@Entity

@Table(name = "mtdt_t")

public class Document implements Serializable {

@Id

@Column(name = "docid", unique = true, nullable = false)

private String docid;

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

@OrderColumn

@JoinColumn(name = "docid", nullable = false)

private BodyElement bodyElement;

public String getDocid() {

return docid;

}

public void setDocid(String docid) {

this.docid = docid;

}

public BodyElement getBodyElement() {

return bodyElement;

}

public void setBodyElement(BodyElement bodyElement) {

this.bodyElement = bodyElement;

}

}

@Entity

@Table(name = "mtdt_body_t")

public class BodyElement implements Serializable {

@Id

@GeneratedValue(strategy = GenerationType.AUTO)

private Long id;

@OneToOne

@JoinColumn(name = "docid", insertable = false, updatable = false, nullable = false)

private Document document;

public BodyElement() {

}

public Long getId() {

return id;

}

public void setId(Long id) {

this.id = id;

}

public Document getDocument() {

return document;

}

public void setDocument(Document document) {

this.document = document;

}

}

我离开了另一个领域.在我有的文件中,

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)

@OrderColumn

@JoinColumn(name = "docid", nullable = false)

@XmlPath("head/meta/kb:keywords/kb:keyword")

private Set keywords;

在Keyword类中,我将外键映射为,

@ManyToOne

@JoinColumn(name = "docid", insertable = false, updatable = false, nullable = false)

@XmlTransient

private Document document;

并且该docid字段永远不会为NULL.

与@OneToMany相比,@ OneToOne Mapping有什么特别之处吗?我只是在@OneToOne字段上模仿我为@OneToMany做的事情.

谢谢

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值