hibernate一对一双向映射

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,Blog)