public interface EduCourseDao extends JpaRepository, JpaSpecificationExecutor {
    @Query(value = "SELECT ec.id,ec.title,ec.price,ec.lesson_num, " +
            "ecd.description, " +
            "es1.title AS oneSubject, " +
            "es2.title AS twoSubject " +
            "FROM edu_course ec LEFT JOIN edu_course_description ecd ON ec.id=ecd.id " +
            "LEFT JOIN edu_teacher et ON ec.teacher_id=et.id " +
            "LEFT JOIN edu_subject es1 ON ec.subject_parent_id=es1.id " +
            "LEFT JOIN edu_subject es2 ON ec.subject_id=es2.id " +
            "WHERE ec.id=?1 ",nativeQuery = true)
    List< Map> findPublishInfoById(String id);


因为涉及到其他表,所以返回类型使用List< Map> ,而不是用原先定义的vo类

每行最后的,与 " 之间要有一个空格,不然报错。

Spring data jpa 自定义SQL语句遇到错误

Not supported for DML operations

今天在自定义一个Update语句时运行遇到一个错误,显示Not supported for DML operations 也就是说不支持DML操作。



3.3.7. Modifying queries

All the sections above describe how to declare queries to access a given entity or collection of entities.Of course you can add custom modifying behaviour by using facilities described in Customimplementations for Spring Data repositories. As this approach is feasible for comprehensive customfunctionality, you can achieve the execution of modifying queries that actually only need parameterbinding by annotating the query method with @Modifying:

Example 45. Declaring manipulating queries

@Query("update User u set u.firstname = ?1 where u.lastname = ?2")
int setFixedFirstnameFor(String firstname, String lastname);
This will trigger the query annotated to the method as updating query instead of a selecting one. As theEntityManager might contain outdated entities after the execution of the modifying query, we do notautomatically clear it (see JavaDoc of EntityManager.clear() for details) since this will effectively dropall non-flushed changes still pending in the EntityManager. If you wish the EntityManager to be clearedautomatically you can set @Modifying annotation's clearAutomatically attribute to true.


@Query("update User u set u.u_name=?2,u.u_sex=?3,u.u_date = ?4,u.u_minzu = ?5,u.u_area=?6,u.u_country=?7 where u.u_id =?1")
public void updateUserById(String u_id,String u_name,String u_sex,
                               String u_date, String u_minzu,String u_area,
                               String u_country);