hibernate 一对一 注解配置

一个电视台(Station)有一个频道(Channel),并且电视台(Station)是主表,频道(Channel)是从表,主表拥有从表的外键
主表配置如下:
package cn.nagasoft.cms.entities.po;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

/**
 * @author chenmingming
 * @date 2012-8-6
 * @version 1.0
 */
@Entity
@Table(name = "station")
public class Station extends AuditEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Id
	@Column(name = "STATION_ID")
	@GeneratedValue(strategy = GenerationType.AUTO)
	private int stationId;

	@Column(name = "STATION_NAME", length = 40)
	private String stationName;

	@Column(name = "STATION_LOGO", length = 50)
	private String stationLogo;

	@Column(name = "STATION_COMMENT", length = 500)
	private String stationComment;

	@Column(name = "STATION_TYPE")
	private int stationType;

	@Column(name = "STATION_HIT")
	private int stationHit;

	@Column(name = "STATION_NEEDHIT1")
	private int stationNeedhit1;

	@Column(name = "STATION_NEEDHIT2")
	private int stationNeedhit2;

	@OneToOne(fetch=FetchType.EAGER,optional=true)
	@JoinColumn(name="CHANNEL_ID",insertable=true,unique=true)
	private Channel channel;

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "playStation", cascade = CascadeType.ALL, orphanRemoval = true)
	private Set<PlayList> playLists = new HashSet<PlayList>();

	@ManyToOne(fetch = FetchType.EAGER, optional = true, cascade = CascadeType.MERGE)
	@JoinColumn(name = "SCHOOL_ID")
	private School school;

	/**
	 * 获取电视台ID
	 * 
	 * @return
	 */
	public int getStationId() {
		return stationId;
	}

	/**
	 * 设置电视台ID
	 * 
	 * @param stationId
	 */
	public void setStationId(int stationId) {
		this.stationId = stationId;
	}

	/**
	 * 获取电视台名称
	 * 
	 * @return
	 */
	public String getStationName() {
		return stationName;
	}

	/**
	 * 设置电视台名称
	 * 
	 * @param stationName
	 */
	public void setStationName(String stationName) {
		this.stationName = stationName;
	}

	/**
	 * 获取电视台logo
	 * 
	 * @return
	 */
	public String getStationLogo() {
		return stationLogo;
	}

	/**
	 * 设置电视台logo
	 * 
	 * @param stationLogo
	 */
	public void setStationLogo(String stationLogo) {
		this.stationLogo = stationLogo;
	}

	/**
	 * 获取电视台简介
	 * 
	 * @return
	 */
	public String getStationComment() {
		return stationComment;
	}

	/**
	 * 设置电视台简介
	 * 
	 * @param stationComment
	 */
	public void setStationComment(String stationComment) {
		this.stationComment = stationComment;
	}

	/**
	 * 获取电视台标志(类型)
	 * 
	 * @return
	 */
	public int getStationType() {
		return stationType;
	}

	/**
	 * 设置电视台标志(类型)
	 * 
	 * @param stationType
	 */
	public void setStationType(int stationType) {
		this.stationType = stationType;
	}

	/**
	 * 获取电视台点击率
	 * 
	 * @return
	 */
	public int getStationHit() {
		return stationHit;
	}

	/**
	 * 设置电视台点击率
	 * 
	 * @param stationHit
	 */
	public void setStationHit(int stationHit) {
		this.stationHit = stationHit;
	}

	/**
	 * 获取扩展点击率1
	 * 
	 * @return
	 */
	public int getStationNeedhit1() {
		return stationNeedhit1;
	}

	/**
	 * 设置扩展点击率1
	 * 
	 * @param stationNeedhit1
	 */
	public void setStationNeedhit1(int stationNeedhit1) {
		this.stationNeedhit1 = stationNeedhit1;
	}

	/**
	 * 获取扩展点击率2
	 * 
	 * @return
	 */
	public int getStationNeedhit2() {
		return stationNeedhit2;
	}

	/**
	 * 设置扩展点击率2
	 * 
	 * @param stationNeedhit2
	 */
	public void setStationNeedhit2(int stationNeedhit2) {
		this.stationNeedhit2 = stationNeedhit2;
	}

	/**
	 * 获取频道所有信息
	 * 
	 * @return
	 */
	public Channel getChannel() {
		return channel;
	}

	/**
	 * 设置频道所有信息
	 * 
	 * @param channels
	 */
	public void setChannel(Channel channel) {
		this.channel = channel;
	}

	/**
	 * 获取学校所有信息
	 * 
	 * @return
	 */
	public School getSchool() {
		return school;
	}

	/**
	 * 设置学校所有信息
	 * 
	 * @param schools
	 */
	public void setSchool(School school) {
		this.school = school;
	}

	/**
	 * 获取节目的集合
	 * 
	 * @return the playLists
	 */
	public Set<PlayList> getPlayLists() {
		return playLists;
	}

	/**
	 * 设置节目的集合
	 * 
	 * @param playLists
	 *            the playLists to set
	 */
	public void setPlayLists(Set<PlayList> playLists) {
		this.playLists = playLists;
	}

}


从表配置如下:
package cn.nagasoft.cms.entities.po;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

/**
 * @author wangxingzong
 * @date 2012-8-3
 * @version 1.0
 */
@Entity
@Table(name = "channel")
public class Channel extends AuditEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = -9010605838714842142L;

	@Id
	@Column(name = "CHANNEL_ID")
	@GeneratedValue(strategy = GenerationType.AUTO)
	private int channelId;

	@Column(name = "CHANNEL_NAME", length = 30)
	private String channelName;

	@Column(name = "SOURCE_CHANNEL")
	private int sourceChannelId;

	@Column(name = "CHANNEL_STREAM", length = 30)
	private String channelStream;

	@Column(name = "CHANNEL_STATE")
	private int channelState;

	@Column(name = "VERSION")
	private int version;

	@ManyToOne(cascade = CascadeType.MERGE, fetch = FetchType.EAGER)
	@JoinColumn(name = "SERVER_ID")
	private Server channelServer;

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "channel")
	private Set<Record> record = new HashSet<Record>();

	@OneToOne(cascade = CascadeType.MERGE, fetch = FetchType.EAGER, optional = true, mappedBy = "channel")
	private Station station;

	/**
	 * 获取直播频道 ID
	 */
	public int getChannelId() {
		return channelId;
	}

	/**
	 * 设置直播频道 ID
	 */
	public void setChannelId(int channelId) {
		this.channelId = channelId;
	}

	/**
	 * 获取直播频道名称
	 */
	public String getChannelName() {
		return channelName;
	}

	/**
	 * 设置直播频道名称
	 */
	public void setChannelName(String channelName) {
		this.channelName = channelName;
	}

	/**
	 * 获取播服务器
	 */
	public Server getChannelServer() {
		return channelServer;
	}

	/**
	 * 设置播服务器
	 */
	public void setChannelServer(Server channelServer) {
		this.channelServer = channelServer;
	}

	/**
	 * 获取直播频道流
	 */
	public String getChannelStream() {
		return channelStream;
	}

	/**
	 * 设置直播频道流
	 */

	public void setChannelStream(String channelStream) {
		this.channelStream = channelStream;
	}

	/**
	 * 获取直播频道状态
	 */
	public int getChannelState() {
		return channelState;
	}

	/**
	 * 设置直播频道状态
	 */
	public void setChannelState(int channelState) {
		this.channelState = channelState;
	}

	/**
	 * 获取源频道的频道ID
	 * 
	 * @return the channelSourceId
	 */
	public int getSourceChannelId() {
		return sourceChannelId;
	}

	/**
	 * 设置源频道的频道ID
	 * 
	 * @param channelSourceId
	 *            the channelSourceId to set
	 */
	public void setSourceChannelId(int sourceChannelId) {
		this.sourceChannelId = sourceChannelId;
	}

	/**
	 * 获取电视台信息
	 * 
	 * @return the station
	 */
	public Station getStation() {
		return station;
	}

	/**
	 * 设置电视台信息
	 * 
	 * @param station
	 *            the station to set
	 */
	public void setStation(Station station) {
		this.station = station;
	}

	/**
	 * 获取录制集合
	 * 
	 * @return
	 */
	public Set<Record> getRecord() {
		return record;
	}

	/**
	 * 设置录制集合
	 * 
	 * @param record
	 */
	public void setRecord(Set<Record> record) {
		this.record = record;
	}

	/**
	 * 获得课件版本
	 * 
	 * @return the version
	 */
	public int getVersion() {
		return version;
	}

	/**
	 * 设置课件版本
	 * 
	 * @param version
	 *            the version to set
	 */
	public void setVersion(int version) {
		this.version = version;
	}
}

猜你喜欢

转载自xiaoming123123.iteye.com/blog/1633266