JPA查询不支持Spring data@Query insert、DBC样式参数(?)

JPA查询不支持Spring data@Query insert、DBC样式参数(?),spring,jpa,spring-data-jpa,spring-data,Spring,Jpa,Spring Data Jpa,Spring Data,我正在尝试在我的接口中创建一个自定义的insert查询,它是exnteds JpaRepository public interface CustomerCouponDAO extends JpaRepository<CustomerCoupons, Integer>{ @Query("INSERT into customer_coupons (customerId, couponId) values (?,?)") public void insert(Integ

我正在尝试在我的接口中创建一个自定义的insert查询,它是exnteds JpaRepository

public interface CustomerCouponDAO extends JpaRepository<CustomerCoupons, Integer>{

    @Query("INSERT into customer_coupons (customerId, couponId) values (?,?)")
    public void insert(Integer custid, Integer coup);

}
public接口CustomerCouponDAO扩展了JpaRepository{
@查询(“插入客户优惠券(customerId、couponId)值(?,)”)
公共无效插入(整数custid、整数coup);
}
但当我得到一个例外:

原因:java.lang.IllegalArgumentException:JDBC样式参数 JPA查询不支持(?)


关于如何进行插入查询有什么想法吗?

使用
PersistentContext
。 接口:

@Repository
public interface CustomerCouponDAOExtend {

    public void insert(Integer custid, Integer coup);

}
实施:

public class CustomerCouponDAOExtendImpl {

    @PersistenceContext
    private EntityManager em;

    @Transactional
    public void insert(Integer custid, Integer coup) {
        CustomerCoupons custCoupons = new CustomerCoupons();
        custCoupons.setCustid(custid);
        custCoupons.setCoup(coup);
        em.merge(custCoupons);
    }

}
您也可以使用
持久化
,但在这种情况下,您需要添加
CustomerCoupons custcolumps=em.find(CustomerCoupons.class,coup)以避免行已在DB中时出现问题。
扩展您自己的界面:

@Repository
public interface CustomerCouponDAO 
    extends JpaRepository<CustomerCoupons, Integer>,CustomerCouponDAOExtend{
}
@存储库
公共接口CustomerCouponDAO
扩展JpaRepository、CustomerCouponDAOExtend{
}
更新:
遵守Spring发现实现的命名约定:如果extend repository具有名称
CustomerCouponDAOExtend
,那么该实现应该被称为
CustomerCouponDAOExtendImpl

谢谢你的问题,我还发现了这个:在JPA中,EntityManager会自动处理从瞬态到托管状态的每个实体。EntityManager检查给定实体是否已经存在,然后决定是否应该插入或更新该实体。由于这种自动管理,JPA只允许使用SELECT、UPDATE和DELETE语句。另外,如果您希望扩展JpaRepository,请确保实现扩展JpaRepository的接口的类与使用Impl和end的接口具有相同的名称,示例:CustomerCouponDAO exnteds JpaRepository实现类:CustomerCouponDAOImpl实现CustomerCouponDAO,否则它将无法工作。来源:是的,我的意思是“遵守Spring发现实现的命名约定”。我想我应该更清楚地表达自己。哦,糟糕!另一件事我偶然发现了,感谢上帝我克服了它lol:我的CustomerCouponDAOImpl类给了我“应用程序上下文中一些bean的依赖关系形成了一个循环”经过大量测试,如果我没有弄错的话,问题是我试图实现JpaRepository接口,并且有一个自定义方法,它要求您对实现类进行相应的命名,我不知道为什么,但一旦我为JpaRepository创建了一个新的接口,新的实现(CustomerCouponDAO扩展了JpaRepository)如果没有自定义方法,错误将停止。是的,不需要向存储库中添加扩展方法,这仅在使用
@RepositoryRestResource
注释时才有必要,而不是
@repository
注释时才有必要。从
CustomerCouponDAO
中删除自定义方法,这样应该可以正常工作。请记住,将两个接口都标记为
@Repository
。补充了答案。