关闭

Hibernate 中一对多的 注解 问题

标签: hibernate
258人阅读 评论(0) 收藏 举报
分类:

Hibernate 中一对多的 注解 问题

现有两张表:频道表(config_tv_channel),节目表(config_tv_schedule),关系为每个频道有多个节目,每个节目属于一个频道。如下:

-表 config_tv_channel

CREATE TABLE `config_tv_channel` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `channel` varchar(20) NOT NULL COMMENT '电视频道',
  `update_by` int(11) NOT NULL COMMENT '操作用户',
  `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8 COMMENT='电视频道';
  • 表config_tv_schedule
CREATE TABLE `config_tv_schedule` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `tv_channel_id` int(11) NOT NULL COMMENT '电视频道ID',
  `tv_program` varchar(20) NOT NULL COMMENT '电视节目名称',
  `beginTime` time DEFAULT NULL COMMENT '节目开始时间',
  `endTime` time DEFAULT NULL COMMENT '节目结束时间',
  PRIMARY KEY (`id`),
  KEY `tv_channel_id` (`tv_channel_id`),
  CONSTRAINT `config_tv_schedule_ibfk_1` FOREIGN KEY (`tv_channel_id`) REFERENCES `config_tv_channel` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=49 DEFAULT CHARSET=utf8 COMMENT='电视节目时间表';
  • 频道实体类
import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.springframework.format.annotation.DateTimeFormat;

import com.fasterxml.jackson.annotation.JsonFormat;

/**
 * 利用一对多绑定
 * 
 * @author Michael
 *
 */
@Entity
@Table(name = "config_tv_channel")
public class ConfigTvChannelNew {
    private Integer id;//逻辑主键
    private String channel;//频道名
    private Integer updateBy;//操作人
    private Date updateTime;//更新时间
    private java.util.List<ConfigTvScheduleNew> configTvSchedules; //频道下属节目单

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Integer getId() {
        return id;
    }

    @Column(name = "channel")
    public String getChannel() {
        return channel;
    }

    @Column(name = "update_by")
    public Integer getUpdateBy() {
        return updateBy;
    }

    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy年MM月dd日,HH:mm:ss", timezone = "GMT+8")
    @Column(name = "update_time")
    public Date getUpdateTime() {
        return updateTime;
    }

    @OneToMany(mappedBy = "configTvChannel")
    public java.util.List<ConfigTvScheduleNew> getConfigTvSchedules() {
        return configTvSchedules;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public void setConfigTvSchedules(
            java.util.List<ConfigTvScheduleNew> configTvSchedules) {
        this.configTvSchedules = configTvSchedules;
    }

    public void setChannel(String channel) {
        this.channel = channel;
    }

    public void setUpdateBy(Integer updateBy) {
        this.updateBy = updateBy;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public ConfigTvChannelNew() {
    }

}
  • 节目实体类
/**
 * 利用对对一绑定
 */
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.*;
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.Table;

import org.springframework.format.annotation.DateTimeFormat;

import com.fasterxml.jackson.annotation.JsonFormat;

@Entity
@Table(name = "config_tv_schedule")
public class ConfigTvScheduleNew {
    private Integer id;//逻辑主键
    private String tvProgram; //节目名
    private Date beginTime; //开始时间
    private Date endTime; //结束时间
    private ConfigTvChannelNew configTvChannel;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Integer getId() {
        return id;
    }

    @Column(name = "tv_program")
    public String getTvProgram() {
        return tvProgram;
    }

    @DateTimeFormat(pattern = "HH:mm")
    @JsonFormat(pattern = "HH:mm", timezone = "GMT+8")
    @Column(name = "beginTime")
    public Date getBeginTime() {
        return beginTime;
    }

    @DateTimeFormat(pattern = "HH:mm")
    @JsonFormat(pattern = "HH:mm", timezone = "GMT+8")
    @Column(name = "endTime")
    public Date getEndTime() {
        return endTime;
    }

    @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE,
            CascadeType.REFRESH }/* , fetch = FetchType.LAZY */)
    @JoinColumn(name = "tv_channel_id")
    public ConfigTvChannelNew getConfigTvChannel() {
        return configTvChannel;
    }

    public void setConfigTvChannel(ConfigTvChannelNew configTvChannel) {
        this.configTvChannel = configTvChannel;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public void setTvProgram(String tvProgram) {
        this.tvProgram = tvProgram;
    }

    public void setBeginTime(Date beginTime) {
        this.beginTime = beginTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    @Override
    public String toString() {
        return "ConfigTvSchedule [id=" + id + ", tvProgram=" + tvProgram
                + ", beginTime=" + beginTime + ", endTime=" + endTime
                + ", configTvChannel=" + configTvChannel + "]";
    }

}

有几点需要注意:
(1) 属性中不要用ArrayList或HashSet等,改用List或Set等接口。否则会报错,暂时还没有弄明白其中的原因。
(2)在实体类中,注解要么全部放在字段上,要么全部放在get方法上,不能混合使用!切记!!! 本文中这样的是可行的。

以下可参考:

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:191249次
    • 积分:2191
    • 等级:
    • 排名:第18189名
    • 原创:63篇
    • 转载:39篇
    • 译文:0篇
    • 评论:16条
    交流方式
    最新评论