SceneryPhoto和SceneryInformation 一对一双向关系,注意红色字体
package com.dio.blog.news.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
@Entity
public class SceneryPhoto {
private int id;
private String name;
private SceneryInformation sceneryInformation;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToOne(mappedBy="sceneryPhoto")
public SceneryInformation getSceneryInformation() {
return sceneryInformation;
}
public void setSceneryInformation(SceneryInformation sceneryInformation) {
this.sceneryInformation = sceneryInformation;
}
}
package com.dio.blog.news.model;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
@Entity
public class SceneryInformation {
private int id;
private String content;
private SceneryPhoto sceneryPhoto;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@OneToOne
@JoinColumn(name="sceneryPhotoId")
public SceneryPhoto getSceneryPhoto() {
return sceneryPhoto;
}
public void setSceneryPhoto(SceneryPhoto sceneryPhoto) {
this.sceneryPhoto = sceneryPhoto;
}
}
在定义对应关系是不能初始化对象,否则会报错
分享到:
相关推荐
Hibernate双向一对一关联映射(注解版)
Hibernate 一对一外键单向...Hibernate 一对一外键双向关联 Hibernate 一对一主键双向关联 Hibernate 一对一连接表双向关联 Hibernate 一对多外键双向关联 Hibernate 一对多连接表双向关联 Hibernate 多对多双向关联
包含《多对多双向关联映射》《多对一单向关联映射》《多对一双向关联映射》《一对多单向关联映射》等文档,并有图解及例子,非常适合新手学习,尤其是刚刚接触hibernate,对映射关系不清楚的。。。。
hibernate双向一对多关联映射(注解版)
博文链接:https://shaqiang32.iteye.com/blog/201309
Hibernate的映射策略:一对一双向关联映射,有所有的测试用例,用以证明从任何一端都可以加载另一端信息。
hibernate 双向 多对对,一对多,一对一 映射的 集合 hibernate所有映射的集合 非常有用
http://blog.csdn.net/e421083458/article/details/8794127 该源码为Hibernate教程配套源码
Hibernate双向一对一关联映射(XML版)
hibernate双向一对多关联映射(XML)
博文链接:https://shaqiang32.iteye.com/blog/201312
Hibernate从入门到精通(六)一对一双向关联映射 - 赵帅 廊坊师范学院信息技术提高班
Hibernate关联关系映射 单向关联 │ ├─ 一对一外键单向关联...├─ 一对一外键双向关联 ├─ 一对一主键双向关联 ├─ 一对一连接表双向关联 ├─ 一对多外键双向关联 ├─ 一对多连接表双向关联 └─ 多对多双向关联
NULL 博文链接:https://lijunabc.iteye.com/blog/438426
hibernate核心,一对多,多对多映射讲解,看了就完全搞明白了
博文链接:https://llying.iteye.com/blog/220803
NULL 博文链接:https://hi-beijing.iteye.com/blog/1315150
主要介绍了hibernate一对多(多对一)双向关联映射的用法
hibernate one-to-one 一对一唯一外键关联映射_单向 and 双向
这是一对一双向主键共享方式的映射例子,可以参考一下。