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的更多相关文章
随机推荐
- linux 系统工具图
- UNION的使用方法 (表与表直接数据和在一起的示例)
SELECT o.CATEGORY CATEGORY,o.KEY_WORK KEY_WORK FROM BO_EU_KEY_WORK wo RIGHT OUTER JOIN BO_EU_WORK_ON ...
- windows无法完成安装,若要在此计算机上安装,请重新启动安装
当出现如上提示的时候,不要重启,按下shift+f10 会打开命令窗口,先输入cd oobe 进入到C:\windows\system32\oobe文件夹,输入msoobe回车然后输入msoobe即可 ...
- BZOJ 3157: 国王奇遇记 (数学)
题面:BZOJ3157 一句话题意: 求: \[ \sum_{i=1}^ni^m\ \times m^i\ (mod\ 1e9+7)\ \ (n \leq 1e9,m\leq200)\] 题解 令 \ ...
- 您必须知道的 Git 分支开发规范
Git 是目前最流行的源代码管理工具. 为规范开发,保持代码提交记录以及 git 分支结构清晰,方便后续维护,现规范 git 的相关操作. 分支管理 分支命名 master 分支 master 为主分 ...
- python类方法以及类调用实例方法的理解
classmethod类方法 1) 在python中.类方法 @classmethod 是一个函数修饰符,它表示接下来的是一个类方法,而对于平常我们见到的则叫做实例方法. 类方法的第一个参数cls,而 ...
- volatile&synchronized&diff
1. 三大性质简介 在并发编程中分析线程安全的问题时往往需要切入点,那就是两大核心:JMM抽象内存模型以及happens-before规则(在这篇文章中已经经过了),三条性质:原子性,有序性和可见性. ...
- @JoinColumn解释
@JoinColumn与@Column标记一样,是用于注释表中的字段的.它的属性与@Column属性有很多相同之处,这里就不详细讲述.请读者参阅5.2.2小节中有关@Column属性的部分. l ...
- PMP知识点(五)——资源管理表示方法
资源矩阵 最常用的为RACI责任矩阵 如下: 资源数据表: 即时间和资源类型的交叉表,是资源直方图的拓展: 资源甘特图: 一种是以任务为索引 一级目录为任务,二级目录为资源,横坐标为时间进度 一种是以 ...
- JS“盒子模型”
列举几个常用的属性 client系列 clientWidth - 盒子真实内容的宽度[content+padding左右],不包括边线和滚动条 clientHeight - 盒子真实内容的高度[con ...