您的位置:首页 > 编程语言 > Java开发

java注解一对多@OneToMany

2016-06-01 16:39 609 查看
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”:[]}]

这里写代码片


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: