-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Zhengjiaao
committed
Apr 24, 2024
1 parent
3b06bd4
commit ec63efb
Showing
39 changed files
with
2,233 additions
and
376 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...r-data/starter-data-jpa/src/main/java/com/zja/dao/relationship/manyToMany/CourseRepo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
31 changes: 31 additions & 0 deletions
31
...-data/starter-data-jpa/src/main/java/com/zja/dao/relationship/manyToMany/StudentRepo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
46 changes: 46 additions & 0 deletions
46
...-data/starter-data-jpa/src/main/java/com/zja/dao/relationship/manyToOne/MOAChildRepo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
...data/starter-data-jpa/src/main/java/com/zja/dao/relationship/manyToOne/MOAParentRepo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
87 changes: 87 additions & 0 deletions
87
...er-data/starter-data-jpa/src/main/java/com/zja/dao/relationship/oneToMany/AChildRepo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
66 changes: 66 additions & 0 deletions
66
...r-data/starter-data-jpa/src/main/java/com/zja/dao/relationship/oneToMany/AParentRepo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
...er-data/starter-data-jpa/src/main/java/com/zja/dao/relationship/oneToMany/BChildRepo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
Oops, something went wrong.