Skip to content

Commit

Permalink
fix(jpa) 关联关系及外键操作实例
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhengjiaao committed Apr 24, 2024
1 parent 3b06bd4 commit ec63efb
Show file tree
Hide file tree
Showing 39 changed files with 2,233 additions and 376 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,4 @@ public interface UserRepo extends
Optional<User> findByLoginName(String loginName);

List<User> findByIdIn(List<String> ids);

//统计查询
long countByName(String name);

//删除查询
long deleteByName(String name);

List<User> removeByName(String name);

//实体类名称
@Query("select u from #{#entityName} u where u.name = ?1")
// @Query("select u from UserEntity u where u.name = ?1")
List<User> findByName(String name);

//分页查询
Page<User> findByName(String lastname, Pageable pageable);

List<User> findByName(String name, Sort sort);
//List<User> findByName(String lastname, Pageable pageable);

/* //使用 SpEL 表达式 - 通配符快捷方式
@Query("select u from UserEntity u where u.name like %:#{[0]}% and u.name like %:lastname%")
List<UserEntity> findByNameWithSpelExpression(@Param("name") String name);
//修改查询
@Modifying
@Query("update UserEntity u set u.firstname = ?1 where u.name = ?2")
int setFixedFirstnameFor(String firstname, String name);*/
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.zja.dao.relationship.manyToMany;

import com.zja.entitys.relationship.manyToMany.Course;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
* @author: zhengja
* @since: 2024/04/19 16:42
*/
@Repository
public interface CourseRepo extends
JpaRepository<Course, String>,
CrudRepository<Course, String>,
JpaSpecificationExecutor<Course> {

@Query("SELECT c FROM Course c JOIN c.students s WHERE s.id = :studentId")
Page<Course> findCoursesByStudentId(@Param("studentId") String studentId, Pageable pageable);

@Query("SELECT c FROM Course c JOIN c.students s WHERE s.id IN :studentIds")
Page<Course> findCoursesByStudentIds(@Param("studentIds") List<String> studentIds, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.zja.dao.relationship.manyToMany;

import com.zja.entitys.relationship.manyToMany.Course;
import com.zja.entitys.relationship.manyToMany.Student;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
* @author: zhengja
* @since: 2024/04/19 16:42
*/
@Repository
public interface StudentRepo extends
JpaRepository<Student, String>,
CrudRepository<Student, String>,
JpaSpecificationExecutor<Student> {

@Query("SELECT s FROM Student s JOIN s.courses c WHERE c.id = :courseId")
Page<Course> findStudentsByCourseId(@Param("courseId") String courseId, Pageable pageable);

@Query("SELECT s FROM Student s JOIN s.courses c WHERE c.id IN :courseIds")
Page<Course> findStudentsByCourseIds(@Param("courseIds") List<String> courseIds, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.zja.dao.relationship.manyToOne;

import com.zja.entitys.relationship.manyToOne.MOAChild;
import com.zja.entitys.relationship.manyToOne.MOAParent;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
* @author: zhengja
* @since: 2024/04/22 15:51
*/
@Repository
public interface MOAChildRepo extends
JpaRepository<MOAChild, Long>,
CrudRepository<MOAChild, Long>,
JpaSpecificationExecutor<MOAChild> {

// 按父实体分页查询其关联的子实体
@Query("SELECT c FROM MOAChild c WHERE c.parent = :parent")
Page<MOAChild> findChildrenByParent(@Param("parent") MOAParent parent, Pageable pageable);

// 按父实体删除关联的子实体
@Transactional
void deleteByParent(MOAParent parent);

// 删除父实体及其关联的子实体
@Transactional
@Modifying
@Query("DELETE FROM MOAChild c WHERE c.parent = :parent")
void deleteChildrenByParent(@Param("parent") MOAParent parent);

// 解除父实体与子实体之间的关联
@Transactional
@Modifying
@Query("UPDATE MOAChild c SET c.parent = null WHERE c.parent.id = :parentId")
void removeChildrenAssociation(@Param("parentId") Long parentId);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.zja.dao.relationship.manyToOne;

import com.zja.entitys.relationship.manyToOne.MOAChild;
import com.zja.entitys.relationship.manyToOne.MOAParent;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
* @author: zhengja
* @since: 2024/04/22 15:52
*/
@Repository
public interface MOAParentRepo extends
JpaRepository<MOAParent, Long>,
CrudRepository<MOAParent, Long>,
JpaSpecificationExecutor<MOAParent> {

// 解除父实体与子实体的关联
@Transactional
@Modifying
@Query("UPDATE MOAChild c SET c.parent = null WHERE c.parent.id = :parentId")
void removeChildrenAssociation(@Param("parentId") Long parentId);


// 删除父实体(存在外键,不能直接删除父实体,需要先解除绑定的子实体 或 先删除关联子实体)
@Transactional
@Modifying
@Query("DELETE FROM MOAParent p WHERE p.id = :parentId")
void deleteParent(@Param("parentId") Long parentId);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* @Company: 上海数慧系统技术有限公司
* @Department: 数据中心
* @Author: 郑家骜[ào]
* @Email: [email protected]
* @Date: 2023-09-28 15:19
* @Since:
*/
package com.zja.dao.relationship.oneToMany;

import com.zja.entitys.relationship.manyToMany.Course;
import com.zja.entitys.relationship.manyToOne.MOAChild;
import com.zja.entitys.relationship.manyToOne.MOAParent;
import com.zja.entitys.relationship.oneToMany.AChild;
import com.zja.entitys.relationship.oneToMany.AParent;
import com.zja.entitys.relationship.oneToMany.BChild;
import com.zja.entitys.relationship.oneToMany.BParent;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
* @author: zhengja
* @since: 2023/09/28 15:19
*/
@Repository
public interface AChildRepo extends
JpaRepository<AChild, Long>,
CrudRepository<AChild, Long>,
JpaSpecificationExecutor<AChild> {

// 解除父实体与子实体的关联
// delete from om_a_parent_childs where child_id=?
// delete from om_a_parent_childs where parent_id=?
@Transactional
@Modifying
@Query(value = "delete from om_a_parent_childs where child_id = :childId", nativeQuery = true)
void unbindChildren(@Param("childId") Long childId);

// todo 会导致中间表记录未删除,只是把 parent_id字段设置为null
// 解除父实体与子实体的关联
// @Transactional
// @Modifying
// @Query("UPDATE AChild c SET c.parent = null WHERE c.parent = :parent")
// void unbindChildren(@Param("parent") AParent parent);

// todo 会导致中间表记录未删除,只是把 parent_id字段设置为null
// 解除父实体与子实体的关联
// @Transactional
// @Modifying
// @Query("UPDATE AChild c SET c.parent = null WHERE c.parent.id = :parentId")
// void unbindChildren(@Param("parentId") Long parentId);

// todo 会导致中间表记录未删除,只是把 parent_id字段设置为null
// 解除父实体与子实体的关联
// @Transactional
// @Modifying
// @Query("UPDATE AChild c SET c.parent = null WHERE c.parent.id = :parentId")
// void removeChildrenAssociation(@Param("parentId") Long parentId);

// 按父实体分页查询其关联的子实体
@Query("SELECT c FROM AChild c WHERE c.parent = :parent")
Page<AChild> findChildrenByParent(@Param("parent") AParent parent, Pageable pageable);

// 按父实体分页查询其关联的子实体
@Query("SELECT c FROM AChild c WHERE c.parent.id = :parentId")
Page<AChild> findChildrenByParent(@Param("parentId") Long parentId, Pageable pageable);

// 删除关联的子实体
@Transactional
@Modifying
@Query("DELETE FROM AChild c WHERE c.parent.id = :parentId")
void deleteChildrenByParent(@Param("parentId") Long parentId);

// 删除关联的子实体
@Transactional
@Modifying
@Query("DELETE FROM AChild c WHERE c.parent = :parent")
void deleteChildrenByParent(@Param("parent") AParent parent);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* @Company: 上海数慧系统技术有限公司
* @Department: 数据中心
* @Author: 郑家骜[ào]
* @Email: [email protected]
* @Date: 2023-09-28 15:19
* @Since:
*/
package com.zja.dao.relationship.oneToMany;

import com.zja.entitys.relationship.manyToMany.Course;
import com.zja.entitys.relationship.manyToOne.MOAParent;
import com.zja.entitys.relationship.oneToMany.AParent;
import com.zja.entitys.relationship.oneToMany.BChild;
import com.zja.entitys.relationship.oneToMany.BParent;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
* @author: zhengja
* @since: 2023/09/28 15:19
*/
@Repository
public interface AParentRepo extends
JpaRepository<AParent, Long>,
CrudRepository<AParent, Long>,
JpaSpecificationExecutor<AParent> {

// 解除父实体与子实体的关联
// delete from om_a_parent_childs where child_id=?
// delete from om_a_parent_childs where parent_id=?
@Transactional
@Modifying
@Query(value = "DELETE FROM om_a_parent_childs WHERE parent_id = :parentId", nativeQuery = true)
void unbindChildren(@Param("parentId") Long parentId);

// todo 会导致中间表记录未删除,只是把 parent_id字段设置为null
// 解除父实体与子实体的关联
// @Transactional
// @Modifying
// @Query("UPDATE AChild c SET c.parent = null WHERE c.parent = :parent")
// void unbindChildren(@Param("parent") AParent parent);

// todo 会导致中间表记录未删除,只是把 parent_id字段设置为null
// 解除父实体与子实体的关联
// @Transactional
// @Modifying
// @Query("UPDATE AChild c SET c.parent = null WHERE c.parent.id = :parentId")
// void unbindChildren2(@Param("parentId") Long parentId);

// todo 会导致中间表记录未删除,只是把 parent_id字段设置为null
// 解除父实体与子实体的关联
// @Transactional
// @Modifying
// @Query("UPDATE AChild c SET c.parent = null WHERE c.parent.id = :parentId")
// void removeChildrenAssociation(@Param("parentId") Long parentId);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @Company: 上海数慧系统技术有限公司
* @Department: 数据中心
* @Author: 郑家骜[ào]
* @Email: [email protected]
* @Date: 2023-09-28 15:19
* @Since:
*/
package com.zja.dao.relationship.oneToMany;

import com.zja.entitys.relationship.manyToOne.MOAChild;
import com.zja.entitys.relationship.manyToOne.MOAParent;
import com.zja.entitys.relationship.oneToMany.BChild;
import com.zja.entitys.relationship.oneToMany.BParent;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
* @author: zhengja
* @since: 2023/09/28 15:19
*/
@Repository
public interface BChildRepo extends
JpaRepository<BChild, Long>,
CrudRepository<BChild, Long>,
JpaSpecificationExecutor<BChild> {

// 按父实体分页查询其关联的子实体
@Query("SELECT c FROM BChild c WHERE c.parent = :parent")
Page<BChild> findChildrenByParent(@Param("parent") BParent parent, Pageable pageable);

// 按父实体删除关联的子实体
@Transactional
void deleteByParent(BParent parent);

}
Loading

0 comments on commit ec63efb

Please sign in to comment.