JpaManytoMany
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的更多相关文章
随机推荐
- 基于Python清除破损图片需求实现
处理同事爬取的图片时,其因爬取过程中因图片类型/网络等问题,获取到较大批次破损图片,现需清除破损文件,并做简要记录. 要点: 在python中,可以使⽤imghdr模块中的what()⽅法判断图⽚⽂件 ...
- Go语言中数组
数组是一个值类型 func ArrayTest1(){ var arryA [3]int = [3]int{1,2,3} //创建一个数组B,将B中第二个元素设置为200 arryB := arryA ...
- opencontrail—VXLAN模式下数据包的传输过程
在这篇文章中,我们将看到VM生成的数据包如何能够到达另一个VM或外部资源,Neutron使用OpenContrail插件的上下文中的关键概念/组件是什么. 我们将重点介绍OpenContrail,它如 ...
- [powershell]获取FCID&Port
Get-InitiatorID Get-InitiatorPort
- 【redis】redis5.0的一些新特性
redis5.0总共增加了12项新特性,如下: 1.新增加的Stream(流)数据类型,这样redis就有了6大数据类型,另外五种是String(字符串),Hash(哈希),List(列表),Set( ...
- fhq_treap 小结
简介 \(fhq\_treap\)是一种非旋平衡树.在学习这篇文章之前,还是先学习一下普通\(treap\)吧 优点 相比于普通的\(treap\),它可以处理区间操作. 相比于\(splay\),它 ...
- js重点--原型链继承详解
上篇说过了关于原型链继承的问题,这篇详解一下. 1. function animals(){ this.type = "animals"; } animals.prototype. ...
- fork子进程
title: fork子进程 data: 2019/3/21 20:24:39 toc: true --- 这里实在学习socket编程前的小知识点,用来创建多个服务端 学习文档 函数可以有两个返回值 ...
- TF用法指导
一.变量 1 .tf.Variables() 功能:创建变量 2.tf.get_variable() 功能:创建变量,注意和上面的区别 3.tf.variable_scope() 功能:其实只要记住一 ...
- 转载:在做datatable时候查询数据和条数只用一次sql就可以解决需求
前言:最近用datatable处理数据比较多,所以在使用时候想提升性能 select * from t_hr_leave SELECT FOUND_ROWS() //返回查询记录的总数 select ...