J'essaye de travailler en spring-boot avec une table qui a une clé composite.
(entier & entier dans mon cas)springJPA - Comment lire depuis une table jointe?
J'ai utilisé cette réponse à comprendre comment écrire à table avec une clé composite:
https://stackoverflow.com/a/3588400/7658754
mais je ne peux pas comprendre comment lire à partir de celui-ci. Comme la fonction "findOne()" de CrudRepository ne peut obtenir qu'un seul paramètre (Integer dans mon cas) mais alors EntityManager lève une exception car il s'attend à obtenir un "Composite-Key-Bean" (comme "TimePK" dans la réponse ci-dessus) et non un nombre entier.
Mes entités comme l'a demandé:
CustomersCouponsPK.class:
package beans;
import java.io.Serializable;
import javax.persistence.Embeddable;
@Embeddable
public class CustomersCuoponsPK implements Serializable {
\t private Integer cust_id;
\t private Integer coupon_id;
\t public CustomersCuoponsPK() {
\t }
\t
\t public CustomersCuoponsPK(int cust_id, int coupon_id) {
\t \t super();
\t \t this.cust_id = cust_id;
\t \t this.coupon_id = coupon_id;
\t }
\t public int getCust_id() {
\t \t return cust_id;
\t }
\t public void setCust_id(int cust_id) {
\t \t this.cust_id = cust_id;
\t }
\t public int getCoupon_id() {
\t \t return coupon_id;
\t }
\t public void setCoupon_id(int coupon_id) {
\t \t this.coupon_id = coupon_id;
\t }
\t @Override
\t public int hashCode() {
\t \t final int prime = 31;
\t \t int result = 1;
\t \t result = prime * result + ((coupon_id == null) ? 0 : coupon_id.hashCode());
\t \t result = prime * result + ((cust_id == null) ? 0 : cust_id.hashCode());
\t \t return result;
\t }
\t @Override
\t public boolean equals(Object obj) {
\t \t if (this == obj)
\t \t \t return true;
\t \t if (obj == null)
\t \t \t return false;
\t \t if (getClass() != obj.getClass())
\t \t \t return false;
\t \t CustomersCuoponsPK other = (CustomersCuoponsPK) obj;
\t \t if (coupon_id == null) {
\t \t \t if (other.coupon_id != null)
\t \t \t \t return false;
\t \t } else if (!coupon_id.equals(other.coupon_id))
\t \t \t return false;
\t \t if (cust_id == null) {
\t \t \t if (other.cust_id != null)
\t \t \t \t return false;
\t \t } else if (!cust_id.equals(other.cust_id))
\t \t \t return false;
\t \t return true;
\t }
}
CustomersCoupons.class:
package beans;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
@Entity
public class CustomersCoupons {
\t @EmbeddedId
\t private CustomersCuoponsPK customersCuoponsPK;
\t public CustomersCoupons() {
\t }
\t public CustomersCoupons(CustomersCuoponsPK customersCuoponsPK) {
\t \t super();
\t \t this.customersCuoponsPK = customersCuoponsPK;
\t }
\t public CustomersCuoponsPK getCustomersCuoponsPK() {
\t \t return customersCuoponsPK;
\t }
\t public void setCustomersCuoponsPK(CustomersCuoponsPK customersCuoponsPK) {
\t \t this.customersCuoponsPK = customersCuoponsPK;
\t }
}
CustomersCouponsRepo:
package repos;
import org.springframework.data.jpa.repository.JpaRepository;
import beans.CustomersCoupons;
import beans.CustomersCuoponsPK;
public interface CustomersCouponsRepo extends JpaRepository<CustomersCoupons, Integer> {
\t public default CustomersCoupons findOne(int cust_id, int coupon_id){
\t \t if(this.getOne(cust_id).equals(this.getOne(coupon_id))){
\t \t \t CustomersCuoponsPK pk = new CustomersCuoponsPK();
\t \t \t pk.setCoupon_id(coupon_id);
\t \t \t pk.setCust_id(cust_id);
\t \t \t CustomersCoupons cc = new CustomersCoupons();
\t \t \t cc.setCustomersCuoponsPK(pk);
\t \t \t return cc;
\t \t }
\t \t return null;
\t }
}
CustomerCouponsDAO:
package DAOs;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import beans.CustomersCoupons;
import beans.CustomersCuoponsPK;
import repos.CustomersCouponsRepo;
@Repository
public class CustomersCouponsDAO {
\t @Autowired
\t private CustomersCouponsRepo repo;
\t public void createCustomerCoupon(int cust_id, int coupon_id) {
\t \t CustomersCoupons customersCoupons = new CustomersCoupons(new CustomersCuoponsPK(cust_id, coupon_id));
\t \t repo.save(customersCoupons);
\t }
\t public List<CustomersCoupons> getAllCustomersCoupons() {
\t \t return (List<CustomersCoupons>) repo.findAll();
\t }
\t public CustomersCoupons readCustomersCoupons(int cust_id, int coupon_id) {
\t \t return repo.findOne(cust_id, coupon_id);
\t }
\t public void updateCustomersCoupons(CustomersCoupons customersCoupons) {
\t \t repo.save(customersCoupons);
\t }
\t public void deleteC(int cust_id, int coupon_id) {
\t }
}
l'exception:
Exception in thread "main" org.springframework.dao.InvalidDataAccessApiUsageException: Provided id of the wrong type for class beans.CustomersCoupons. Expected: class beans.CustomersCuoponsPK, got class java.lang.Integer; nested exception is java.lang.IllegalArgumentException: Provided id of the wrong type for class beans.CustomersCoupons. Expected: class beans.CustomersCuoponsPK, got class java.lang.Integer
\t at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:384)
\t at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:246)
\t at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:488)
\t at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
\t at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
\t at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)
\t at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
\t at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133)
\t at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
\t at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
\t at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
\t at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:57)
\t at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
\t at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
\t at com.sun.proxy.$Proxy76.getOne(Unknown Source)
\t at repos.CustomersCouponsRepo.findOne(CustomersCouponsRepo.java:11)
\t at java.lang.invoke.MethodHandle.invokeWithArguments(Unknown Source)
\t at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:62)
\t at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
\t at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
\t at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
\t at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
\t at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
\t at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
\t at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
\t at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133)
\t at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
\t at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
\t at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
\t at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:57)
\t at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
\t at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
\t at com.sun.proxy.$Proxy76.findOne(Unknown Source)
\t at DAOs.CustomersCouponsDAO.readCustomersCoupons(CustomersCouponsDAO.java:28)
\t at DAOs.CustomersCouponsDAO$$FastClassBySpringCGLIB$$d50d5f2d.invoke(<generated>)
\t at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
\t at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738)
\t at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
\t at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
\t at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
\t at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673)
\t at DAOs.CustomersCouponsDAO$$EnhancerBySpringCGLIB$$1907e960.readCustomersCoupons(<generated>)
\t at com.example.CouponSystem.CouponSystemApplication.main(CouponSystemApplication.java:35)
Caused by: java.lang.IllegalArgumentException: Provided id of the wrong type for class beans.CustomersCoupons. Expected: class beans.CustomersCuoponsPK, got class java.lang.Integer
\t at org.hibernate.jpa.spi.AbstractEntityManagerImpl.getReference(AbstractEntityManagerImpl.java:1019)
\t at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
\t at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
\t at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
\t at java.lang.reflect.Method.invoke(Unknown Source)
\t at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:298)
\t at com.sun.proxy.$Proxy69.getReference(Unknown Source)
\t at org.springframework.data.jpa.repository.support.SimpleJpaRepository.getOne(SimpleJpaRepository.java:278)
\t at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
\t at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
\t at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
\t at java.lang.reflect.Method.invoke(Unknown Source)
\t at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:504)
\t at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:489)
\t at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:461)
\t at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
\t at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:56)
\t at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
\t at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
\t at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
\t at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
\t at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
\t at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
\t ... 37 more
Caused by: org.hibernate.TypeMismatchException: Provided id of the wrong type for class beans.CustomersCoupons. Expected: class beans.CustomersCuoponsPK, got class java.lang.Integer
\t at org.hibernate.event.internal.DefaultLoadEventListener.checkIdClass(DefaultLoadEventListener.java:166)
\t at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:86)
\t at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1129)
\t at org.hibernate.internal.SessionImpl.access$2600(SessionImpl.java:164)
\t at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.getReference(SessionImpl.java:2669)
\t at org.hibernate.internal.SessionImpl.load(SessionImpl.java:965)
\t at org.hibernate.jpa.spi.AbstractEntityManagerImpl.getReference(AbstractEntityManagerImpl.java:1013)
\t ... 59 more
requêtes JPA ne sont jamais sur les tables. Ils concernent des entités. Publiez vos entités et dites ce que vous voulez faire. –
ajouté au fil principal. Je souhaite lire une entité CustomersCoupons de ma base de données. –
L'ID de votre entité est de type CustomerCuoponsPK. Votre dépôt doit donc implémenter JpaRepository, et non JpaRepository . C'est exactement ce que le message d'erreur de l'exception vous dit. –