1

I am trying to make a custom insert query in my interface that exnteds JpaRepository

public interface CustomerCouponDAO extends JpaRepository<CustomerCoupons, Integer>{

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

}

but when I get the exception:

Caused by: java.lang.IllegalArgumentException: JDBC style parameters (?) are not supported for JPA queries.

any ideas on how to make that insert query?

1

2 Answers 2

1

Use PersistentContext. Interface:

@Repository
public interface CustomerCouponDAOExtend {

    public void insert(Integer custid, Integer coup);

}

Implementation:

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);
    }

}

Also you can use persist, but in this case your necessary add CustomerCoupons custCoupons = em.find(CustomerCoupons.class, coup); to avoid problems if the row is already in DB. Extend your own interface:

@Repository
public interface CustomerCouponDAO 
    extends JpaRepository<CustomerCoupons, Integer>,CustomerCouponDAOExtend{
}

UPDATE: Observe the naming convention that Spring finds implementation: if extend repository has name CustomerCouponDAOExtend then the implementation should be called CustomerCouponDAOExtendImpl.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for your question I also found this : In JPA, every entity going from a transient to managed state is automatically handled by the EntityManager. The EntityManager checks whether a given entity already exists and then decides if it should be inserted or updated. Because of this automatic management, the only statements allowed by JPA are SELECT, UPDATE and DELETE.
and another thing, if you wish to extend the JpaRepository, make sure that the class that implements the interface that extends the JpaRepository has the same name as the interface with Impl and the end , example: CustomerCouponDAO exnteds JpaRepository<Customer, Integer> the implementing clasS: CustomerCouponDAOImpl implements implements CustomerCouponDAO otherwise it would not work. source: docs.spring.io/spring-data/jpa/docs/current/reference/html/…
Yes, I meant it "Observe the naming convention that Spring finds implementation". I think I should have expressed myself more clearly.
Oh my bad! another thing I stumbled upon and thank god I got over it lol : My CustomerCouponDAOImpl class was giving me the "The dependencies of some of the beans in the application context form a cycle" after alot of testing if Im not mistaken the problem is that I was trying to implement the JpaRepository interface and have a custom method which requires you to name your implementing class accordinly, I don't know why but as soon as I made a new interface new implementation for the JpaRepository ( CustomerCouponDAO extends JpaRepository) without the custom method, the error stopped.
Yes, there is no need to add an extended method to your repository, this is only necessary when using the @RepositoryRestResource annotation, but not @Repository. Remove custom method from CustomerCouponDAO and this should work correctly. And remember mark both interfaces as @Repository. Complemented the answer.
|
0

it's too late to answer anycase: in JPA for custom method you must set the number of parameter in the query annotation:

public interface CustomerCouponDAO extends JpaRepository<CustomerCoupons, Integer>{

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.