java注解一对多@OneToMany

本文详细介绍了如何通过配置电视频道及其节目安排,包括频道管理、节目时间表创建与关联,以及通过DAO层调用获取配置信息的过程。
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”:[]}]

这里写代码片

这里写图片描述

<think>嗯,用户问的是@ManyToOne和@OneToMany的区别,这两个注解在JPA中用于处理实体之间的关系。首先,我需要回忆这两个注解的基本用法和它们所表示的关系类型。@ManyToOne通常用于对一的关系,比如个订单属于一个客户,这时候订单实体里会有@ManyToOne指向客户。而@OneToMany则是一对,比如一个客户有个订单,客户实体里用@OneToMany来表示。不过,实际中这两个注解常常成对使用,形成双向关联。接下来,我需要考虑它们的映射方式。@ManyToOne通常在的一方使用,并会在数据库中创建外键。而@OneToMany在一的一方,可以通过mappedBy属性避免生成额外的表,或者配合@JoinColumn来指定外键,但这样可能会生成中间表,需要注意。 然后,关于加载策略,默认情况下@ManyToOne是EAGER加载,而@OneToMany是LAZY。这可能引发性能问题,比如N+1查询,需要提醒用户根据实际情况调整FetchType。 还要提到级联操作和关系维护的问题。比如,在双向关系中,通常由的一方维护关系,而一的一方使用mappedBy。级联设置不当可能导致意外的数据删除或更新,需要举例说明。 另外,用户可能想知道在实际应用中如何选择使用这两个注解,以及常见的错误用法,比如没有正确设置mappedBy导致重复的外键字段,或者级联配置不当。需要给出代码示例来展示正确和错误的用法,帮助用户理解。 最后,相关问题的部分需要涵盖其他关联注解,如@OneToOne和@ManyToMany的区别,以及如何优化性能,解决N+1问题,还有orphanRemoval的作用。这些都是用户可能进一步提问的方向,需要预先准备好。</think>@ManyToOne和@OneToMany是JPA中处理实体关系的两个核心注解,主要区别如下: 1. **关系方向与映射方式** ```java // 对一(子实体端) @Entity public class Order { @ManyToOne @JoinColumn(name = "customer_id") private Customer customer; } // 一对(父实体端) @Entity public class Customer { @OneToMany(mappedBy = "customer") private List<Order> orders = new ArrayList<>(); } ``` - @ManyToOne:在""的一方定义,自动生成外键列 - @OneToMany:在"一"的一方定义,需要配合mappedBy指定关联字段 2. **数据库结构差异** - @ManyToOne:会在当前表创建外键列(如customer_id) - @OneToMany:默认会创建中间关联表(除非使用mappedBy) 3. **加载策略差异** - @ManyToOne:默认FetchType.EAGER(立即加载) - @OneToMany:默认FetchType.LAZY(延迟加载) 4. **关系维护权** ```java // 错误示例:双向关系未指定维护端 @Entity public class Customer { @OneToMany private List<Order> orders; // 将产生余的中间表 } // 正确示例:明确维护关系 @Entity public class Customer { @OneToMany(mappedBy = "customer") private List<Order> orders; // 由Order.customer维护关系 } ``` 5. **级联操作差异** ```java // 级联删除配置示例 @OneToMany(mappedBy = "customer", cascade = CascadeType.REMOVE) private List<Order> orders; // 删除Customer时自动删除关联Order @ManyToOne(cascade = CascadeType.PERSIST) private Customer customer; // 保存Order时自动保存关联Customer ``` **典型应用场景**: - @ManyToOne:订单-客户、评论-文章、员工-部门 - @OneToMany:购物车-商品项、学校-学生、分类-商品 **常见错误处理**: ```java // N+1查询问题解决方案 @Entity public class Customer { @OneToMany(mappedBy = "customer", fetch = FetchType.LAZY) private List<Order> orders; // 改为LAZY加载 } // 查询时使用JOIN FETCH @Query("SELECT c FROM Customer c JOIN FETCH c.orders WHERE c.id = :id") Customer findWithOrders(@Param("id") Long id); ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值