package com.allqj.calculator.entity;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.*;
import java.io.Serializable;
import java.util.List;
/**
* spec多表尝试电影表实体类
*
* @author Xing
* @CREATE 2018-03-21
*/
@Entity
@Setter
@Getter
@Table(name = "movie")
public class MovieBean implements Serializable {
@Id
@GenericGenerator(name = "bb", strategy = "guid")
@GeneratedValue(generator = "bb")
@Column(name = "mid", nullable = false)
private String mid; @Column(name = "mname")
private String mname; @Column(name = "mtype")
private String mtype; @Column(name = "mfeel")
private String mfeel;
@JsonIgnoreProperties(value = { "movies" })
//@JsonIgnore
@ManyToMany(mappedBy = "movies")
private List<SuperStarBean> supers;
}
//@OneToMany(mappedBy="movies",/*targetEntity=SuperstarToMovie.class,(如果使用泛型来定义集合以指定元素类型,则不需要指定关联的目标实体类型; 否则必须指定目标实体类)*/fetch=FetchType.LAZY)
package com.allqj.calculator.entity;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import com.allqj.calculator.entity.MovieBean;
import org.hibernate.annotations.GenericGenerator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
* spec多表尝试演员表实体类
*
* @author Xing
* @CREATE 2018-03-12-18:31
*/ @Entity
@Getter
@Setter
@Table(name = "superstar")
public class SuperStarBean implements Serializable {
@Id
@GenericGenerator(name = "aa", strategy = "guid")
@GeneratedValue(generator = "aa")
@Column(name = "sid", nullable = false)
private String sid; @Column(name = "name")
private String name; @Column(name = "sex")
private String sex;
@JsonIgnoreProperties(value = { "supers" })
@ManyToMany(fetch = FetchType.EAGER, cascade ={ CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REFRESH,CascadeType.REMOVE})
@JoinTable(name = "movie_superstar",
joinColumns = {
@JoinColumn(name = "sid")},
inverseJoinColumns = {
@JoinColumn(name = "mid")}) private List<MovieBean> movies; }
package com.allqj.calculator.controller;

import com.allqj.calculator.entity.MovieBean;
import com.allqj.calculator.entity.SuperStarBean;
import com.allqj.calculator.service.ZmanyService;
import com.allqj.qjf.common.controller.BaseController;
import com.allqj.qjf.common.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List; /**
* spec多表尝试Controller层
*
* @author Xing
* @CREATE 2018-03-12-18:30
*/
@RestController
@RequestMapping("/tomany")
public class ZmanyController extends BaseController {
@Autowired
private ZmanyService s; @ResponseBody
//根据演员名查询信息
@RequestMapping("/superlist")
public ResultVO list(@RequestBody SuperStarBean stom) {
List<SuperStarBean> list = s.findAllbySuper(stom);
return ok(list);
} //根据电影名查询信息
@RequestMapping("/movielist")
public ResultVO list1(@RequestBody MovieBean mb) {
List<MovieBean> list1 = s.findAllbyMovie(mb);
return ok(list1);
} //删除演员信息中的某部电影關聯
@RequestMapping("/delmovie")
public ResultVO delmovie(@RequestBody String sid) {
s.delmovie(sid);
return ok();
} //刪除電影信息中的某位演員關聯
@RequestMapping("/delsuper")
public ResultVO delsuper(@RequestBody String mid) {
s.delsuper(mid);
return ok();
} //修改或添加演员
@RequestMapping("/superadd")
public ResultVO add(@RequestBody SuperStarBean superStar) {
SuperStarBean add1 = s.add(superStar);
return ok(add1);
} //修改或添加电影
@RequestMapping("/movieadd")
public ResultVO addmovie(@RequestBody MovieBean movie) {
MovieBean addm = s.addm(movie);
return ok();
} /*public boolean register(SuperStarBean supers){
MovieBean role=new MovieBean();
role.setMid((long) 2);
Set<MovieBean> roles=new HashSet<MovieBean>();
roles.add(role);
supers.setMovies(movies); ;
return userService.insert(supers);
}*/ //删除演员
@RequestMapping("/superdelAll")
public ResultVO superdelall(@RequestBody SuperStarBean superbean) {
s.superdelall(superbean);
return ok();
}
//删除电影
@RequestMapping("/moviedelAll")
public ResultVO moviedelall(@RequestBody MovieBean movieBean){
s.moviedelall(movieBean);
return ok();
}
}
package com.allqj.calculator.service;

import com.allqj.calculator.entity.MovieBean;
import com.allqj.calculator.entity.SuperStarBean; import java.util.List; public interface ZmanyService { List<MovieBean> findAllbyMovie(MovieBean mb); List<SuperStarBean> findAllbySuper(SuperStarBean stom); void delmovie(String sid); void delsuper(String mid); SuperStarBean add(SuperStarBean superStar); MovieBean addm(MovieBean movie); SuperStarBean superdelall(SuperStarBean superbean); void moviedelall(MovieBean movieBean);
}
package com.allqj.calculator.service;
import com.allqj.calculator.entity.MovieBean;
import com.allqj.calculator.entity.SuperStarBean;
import com.allqj.calculator.repository.SuperRepository;
import com.allqj.calculator.repository.ZmanyRepository;
import com.allqj.calculator.repository.ZmanyRepositoryImpl;
import com.github.wenhao.jpa.Specifications;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Range;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects; /**
* spec多表尝试Service层实现类
*
* @author Xing
* @CREATE 2018-03-12-18:32
*/
@Service
public class ZmanyServiceImpl implements ZmanyService{
@Autowired
private ZmanyRepository movieDao;
@Autowired
private SuperRepository superDao;
@Autowired
private ZmanyRepositoryImpl zmanyRepositoryImpl;
@Override
@Modifying
public List<MovieBean> findAllbyMovie(MovieBean mb) {
Specification<MovieBean> specification = new Specifications<MovieBean>()
//.between("age", new Range<>(10, 35))
//.eq(Objects.nonNull(stom.getMovies()),"movie.mname", "666")
.build();
return movieDao.findAll();
}
@Override
@Modifying
public List<SuperStarBean> findAllbySuper(SuperStarBean stom) {
Specification<SuperStarBean> specification = new Specifications<SuperStarBean>()
.between("age", new Range<>(10, 35))
.eq(Objects.nonNull(stom.getMovies()),"movie.mname", "666")
.build();
return superDao.findAll();
} @Override
public void delmovie(String sid) {
zmanyRepositoryImpl.delmovie(sid);
} @Override
public void delsuper(String mid) {
zmanyRepositoryImpl.delsuper(mid);
} @Override
public SuperStarBean add(SuperStarBean superStar) {
return superDao.save(superStar);
} @Override
public MovieBean addm(MovieBean movie) {
return movieDao.save(movie);
} @Override
public SuperStarBean superdelall(SuperStarBean superbean) {
superDao.delete(superbean);
return null;
} @Override
public void moviedelall(MovieBean movieBean) {
// movieDao.deleteByMid("1");
// zmanyRepositoryImpl.delete(movieBean);
}
}
package com.allqj.calculator.repository;

import com.allqj.calculator.entity.MovieBean;
import com.allqj.calculator.entity.SuperStarBean;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import javax.transaction.Transactional; public interface ZmanyRepository extends JpaRepository<MovieBean, Long>, JpaSpecificationExecutor<MovieBean> { }

package com.allqj.calculator.repository;
import com.allqj.calculator.entity.MovieBean;
import com.allqj.calculator.entity.SuperStarBean;
import org.springframework.stereotype.Repository;
import javax.persistence.PersistenceContext;
import javax.persistence.EntityManager;
import javax.transaction.Transactional; /**
* @author Xing
* @CREATE 2018-03-17-15:33
*/
@Repository
public class ZmanyRepositoryImpl {
@PersistenceContext
private EntityManager em;
/**
* 解除关联关系调用集合对象的remove方法
* 集合的remove方法会删除的是关联关系,也就是删除中间表的某条记录
* 但是,它不会删除实体类对应数据表中的记录
*/
//解除演員信息中與某電影的關係
@Transactional
public void delmovie(String sid){
SuperStarBean superstar = em.find(SuperStarBean.class, sid);
MovieBean item = superstar.getMovies().iterator().next();
superstar.getMovies().remove(item);
}
//解除電影信息中與某演員的關係
@Transactional
public void delsuper(String mid) {
MovieBean movie = em.find(MovieBean.class, mid);
SuperStarBean item = movie.getSupers().iterator().next();
movie.getSupers().remove(item);
}
}

JpaManytoMany的更多相关文章

随机推荐

  1. 集合源码分析[1]-Collection 源码分析

    目录 Collection 1. 介绍 2. 继承关系 3. 方法 4. JDK8新增的方法 removeIf(Predicate<? super E> filter) Spliterat ...

  2. CF集萃2

    CF1155D - Beautiful Array 题意:给你一个序列和x,你可以选择任意一个子串(可以为空)乘上x,使得得到的序列最大子串和最大.求这个最大值.30w,2s. 解:设fi,0/1/2 ...

  3. Spring MVC通过AOP切面编程 来拦截controller 实现日志的写入

    首选需要参考的是:[参考]http://www.cnblogs.com/guokai870510826/p/5977948.html    http://www.cnblogs.com/guokai8 ...

  4. jemter+ant+jenkins进行集成测试

    一下为我学习的一些笔记: 一.安装配置ant 安装地址:http://ant.apache.org/ 1.下载ant一路傻瓜式安装 2.配置ant环境变量:path下配置ant的bin路径 3.将jm ...

  5. ACM-自学之旅

    分类 知识清单 数据结构 链式前向星 树状数组 线段树 线段树的区间合并 基于ST表格的RMQ 图论 最近公共祖先 树的直径.树的重心与树的点分治 树的最小支配集,最小点覆盖与最大独立集 求无向连通图 ...

  6. Oracle ORA-08104报错处理方法及注意事项

    [环境介绍] 系统环境:IBM P740 8205-E6C (AIX) + 11.2.0.3.0 Oracle RAC [背景介绍] 故障描述:数据库表空间超过90%,无法进行扩容表空间,需要业务侧清 ...

  7. js关于“call方法”百度,阿里超难面试题

    面试题:function fn(a,b){    console.log(this);    console.log(a);    console.log(a+b);}fn.call(1);fn.ca ...

  8. Python-re模块中一些重要函数

    re模块包含对正则表达式的支持.

  9. iMX6Q开发板的EIM接口的配置可以与FPGA通讯-交换数据-最常用的接口配置

    最近基于迅为的i.mx6Q开发板进行了一个FPGA项目的开发,下面给大家介绍一下EIM接口的配置,包括引脚的的选择和寄存器的配置 For the usage of WEIM on i.MX6, you ...

  10. Centos环境下安装mongoDB

    安装前注意: 此教程是通过yum安装的.仅限64位centos系统 安装步骤: 1.创建仓库文件: vi /etc/yum.repos.d/mongodb-org-3.4.repo 然后复制下面配置, ...