关闭

java注解一对多@OneToMany

257人阅读 评论(0) 收藏 举报
分类:
package com.po.configSubT;

import java.util.Date;
import java.util.List;
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.OneToMany;
import javax.persistence.Table;

import org.springframework.format.annotation.DateTimeFormat;

import com.fasterxml.jackson.annotation.JsonFormat;


@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 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(cascade = { CascadeType.REFRESH,
            CascadeType.PERSIST,
            CascadeType.MERGE,
            CascadeType.REMOVE }
            ,mappedBy = "configTvChannel")
    public java.util.List getConfigTvSchedules() {
        return configTvSchedules;
    }

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

    public void setConfigTvSchedules(
            java.util.List 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() {
    }

}


第二个类
-----------------------------------------------------------------------------
package com.po.configSubT;


import java.util.Date;

import javax.persistence.*;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.LazyToOne;
import org.hibernate.annotations.LazyToOneOption;
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.MERGE,
             CascadeType.REFRESH }
     )
//该表的外键字段名字就是tv_channel_id,关联上一个表的id字段
    @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 + "]";
    }

}
--------------Dao层调用-----------------------------------
package com.dao.config;

import java.util.ArrayList;
import java.util.List;

import javax.jms.Session;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.common.BaseDao;
import com.dto.ConfigTvDto;
import com.po.config.ConfigTvChannel;
import com.po.configSubT.ConfigTvChannelNew;
import com.po.configSubT.ConfigTvScheduleNew;

@Repository
@Transactional
public class ConfigTvDao extends BaseDao {

    @SuppressWarnings("unchecked")
    public List get() {
        List resList = new ArrayList();
        String hql = "from ConfigTvChannelNew ";
        Query query = getCurrentSession().createQuery(hql);
        List  list = (List)query.list();
        for (int i = 0; i < list.size(); i++) {
            ConfigTvChannelNew conDto = new ConfigTvChannelNew();
            ConfigTvChannel con = new ConfigTvChannel();
            ConfigTvChannelNew conCh = (ConfigTvChannelNew) (list.get(i));
            conDto.setId(conCh.getId());
            conDto.setChannel(conCh.getChannel());
            conDto.setUpdateBy(conCh.getUpdateBy());
            conDto.setUpdateTime(conCh.getUpdateTime());
            ArrayList configTvScheduleList = new ArrayList();
            for (int j = 0; j < conCh.getConfigTvSchedules().size(); j++) {
                ConfigTvScheduleNew conSc = new ConfigTvScheduleNew();
                conSc.setId(conCh.getConfigTvSchedules().get(j).getId());
                conSc.setTvProgram(conCh.getConfigTvSchedules().get(j).getTvProgram());
                conSc.setBeginTime(conCh.getConfigTvSchedules().get(j).getBeginTime());
                conSc.setEndTime(conCh.getConfigTvSchedules().get(j).getEndTime());
                configTvScheduleList.add(conSc);
                conSc=null;
            }
            conDto.setConfigTvSchedules(configTvScheduleList);
            resList.add(conDto);
        }
        return resList;

    }

}

——————–结果————————-

[{“id”:1,”channel”:”东方卫视”,”updateBy”:8888,”updateTime”:”2016年05月19日,12:26:19”,”configTvSchedules”:[{“id”:14,”tvProgram”:”12321”,”beginTime”:”19:00”,”endTime”:”19:00”,”configTvChannel”:null}]},{“id”:2,”channel”:”江苏卫视”,”updateBy”:123,”updateTime”:”2016年04月19日,13:13:07”,”configTvSchedules”:[]},{“id”:3,”channel”:”浙江卫视”,”updateBy”:123,”updateTime”:”2016年04月19日,13:14:45”,”configTvSchedules”:[]},{“id”:4,”channel”:”安徽卫视”,”updateBy”:888,”updateTime”:”2016年04月21日,13:04:38”,”configTvSchedules”:[]},{“id”:5,”channel”:”北京卫视”,”updateBy”:123,”updateTime”:”2016年04月19日,13:15:54”,”configTvSchedules”:[]},{“id”:6,”channel”:”天津卫视”,”updateBy”:123,”updateTime”:”2016年04月19日,13:16:11”,”configTvSchedules”:[]},{“id”:8,”channel”:”重庆卫视”,”updateBy”:111,”updateTime”:”2016年04月19日,13:17:04”,”configTvSchedules”:[]},{“id”:9,”channel”:”延边卫视111”,”updateBy”:8888,”updateTime”:”2016年05月19日,12:32:31”,”configTvSchedules”:[]},{“id”:12,”channel”:”sss”,”updateBy”:8888,”updateTime”:”2016年05月19日,13:09:53”,”configTvSchedules”:[{“id”:17,”tvProgram”:”111111”,”beginTime”:”19:00”,”endTime”:”19:00”,”configTvChannel”:null},{“id”:18,”tvProgram”:”111111”,”beginTime”:”19:00”,”endTime”:”19:00”,”configTvChannel”:null}]},{“id”:13,”channel”:”wqsdw”,”updateBy”:8888,”updateTime”:”2016年05月19日,13:46:15”,”configTvSchedules”:[]}]

这里写代码片

这里写图片描述

0
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:19469次
    • 积分:497
    • 等级:
    • 排名:千里之外
    • 原创:30篇
    • 转载:3篇
    • 译文:0篇
    • 评论:4条
    最新评论