2017-09-01 96 views
0

刪除操作我看看下面的實體:嵌入式對象春天JPA

@Entity(name = "USRGRP_MAP") 
public class UserGroupMapping { 

    @Id 
    @Column(name = "USRGRP_ID") 
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "USER_GRP_MAP_SEQ") 
    @SequenceGenerator(sequenceName = "usrgrp_map_seq",allocationSize = 1,name = "USER_GRP_MAP_SEQ") 
    private Long mappingId; 

    @ManyToOne(fetch = FetchType.EAGER) 
    @JoinColumn(name = "USER_ID", referencedColumnName = "USER_ID") 
    private User user; 

    @ManyToOne(fetch = FetchType.EAGER) 
    @JoinColumn(name = "USR_GRP_ID", referencedColumnName = "USR_GRP_ID") 
    private UserGroup group; 

    @Column(name = "USR_USRGRP_ACT") 
    private String userGroupAct; 

    getter/setters 
} 

@Entity(name = "USER") 
public class User { 

    @Id 
    @Column(name = "USER_ID") 
    private Long userId; 

    @Column(name = "LOGIN_ID") 
    private String userName; 

    getter/setters 
} 

@Entity(name = "USR_GRP") 
public class UserGroup { 

    @Id 
    @Column(name = "USR_GRP_ID") 
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "USER_GRP_SEQ") 
    @SequenceGenerator(sequenceName = "usr_grp_seq",allocationSize = 1,name = "USER_GRP_SEQ") 
    private long groupId; 

    @Column(name = "GRP_NM") 
    private String groupName; 

    @Column(name = "GRP_DESC") 
    private String groupDesc; 

    getter/setters 
} 

UserGroupMapping包含有許多與用戶和組的一個關係。 現在我想要做UserGroupMapping爲我創造庫如下CRUD操作:

public interface UserGroupMappingRepository extends JpaRepository<UserGroupMapping, Long> { 
    List<UserGroupMapping> findByGroup(UserGroup group); 
    List<UserGroupMapping> findByUser(User user); 
} 

現在我想要寫,刪除操作(特別是用戶和組)上UserGroupMapping而不刪除用戶和USR_GRP中的任何條目表,只需要從USRGRP_MAP表中刪除條目。

我試圖使用本機查詢來實現它:

@Query(value = "delete from USR_USRGRP_MAP where user_id = :userId and usr_grp_id = :groupId",nativeQuery = true) 
    void deleteUserGroupMappingByUserAndGroup(@Param("userId") Long userId, @Param("groupId") Long groupId); 

面對異常無效的SQL語法,但在SQL Developer中查詢做工精細。

下面是我的服務類:

@Service 

public class UserGroupMappingServiceImpl implements UserGroupMappingService{ 

    @Autowired 
    private UserGroupMappingRepository repository; 


    @Override 
    public void deleteUserGroupMapping(Long userId, Long groupId) { 
     repository.deleteUserGroupMappingByUserAndGroup(userId,groupId); 
    } 


} 

可能有人建議刪除而不刪除用戶和組UserGroupMapping進入正確的方法是什麼?

下面是USRGRP_MAP表:

USRGRP_ID USER_ID USR_USRGRP_ID USR_USRGRP_ACT 
------------- ---------- ------------- - 
      41  306106   41 Y 
      14  108527   14 Y 
      8  295597    8 N 
      10  296518   10 Y 
      11  295597   11 Y 

在此先感謝。

+0

請提供完整的堆棧跟蹤 –

回答

1

試圖改變 @Query(值= 「從USR_USRGRP_MAP刪除其中USER_ID =:帳戶及usr_grp_id =:的groupId」,nativeQuery = TRUE) 無效deleteUserGroupMappingByUserAndGroup(@Param( 「用​​戶id」)長期用戶id,@Param( 「groupId」)Long groupId);

向該:

@Modifying

@Query(值= 「從USR_USRGRP_MAP刪除其中USER_ID =:帳戶及usr_grp_id =:的groupId」,nativeQuery =真) 空隙deleteUserGroupMappingByUserAndGroup(@Param (「userId」)long userId,@Param(「groupId」)Long groupId);

乾杯

〜周華健

+0

添加@Modifying給出了以下異常: 「異常」: 「org.springframework.dao.InvalidDataAccessApiUsageException」, 「消息」:「執行的更新/刪除查詢;嵌套異常是javax.persistence.TransactionRequiredException:執行更新/刪除查詢「, – Abhij

+0

您可以粘貼使用查詢的代碼 – vRaptor