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;
[color=red]private SceneryInformation sceneryInformation;[/color]
@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;
[color=red] private SceneryPhoto sceneryPhoto; [/color]
@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;
}
}
[color=red][size=x-large]在定义对应关系是不能初始化对象,否则会报错[/size][/color]
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;
[color=red]private SceneryInformation sceneryInformation;[/color]
@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;
[color=red] private SceneryPhoto sceneryPhoto; [/color]
@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;
}
}
[color=red][size=x-large]在定义对应关系是不能初始化对象,否则会报错[/size][/color]
本文探讨了SceneryPhoto与SceneryInformation两个类之间的双向一对一关联关系,并通过实例代码展示了这种关系的具体实现方式。注意在定义双向关联时不可直接初始化对象,否则将导致运行时错误。
613

被折叠的 条评论
为什么被折叠?



