2017-08-30 42 views
0

我正在尝试在具有组合键的表格的spring-boot中工作。
(在我的情况整数&整数)springJPA - 如何从连接表读取?

我用这个答案来了解如何表具有复合键:

https://stackoverflow.com/a/3588400/7658754

,但我不明白怎么从它读取

作为函数CrudRepository的“findOne()”只能得到一个参数(在我的情况下是整数),但是然后EntityManager抛出一个Exception,因为它期望得到一个“Composite-Key-Bean”(比如“TimePK”在上面的答案),而不是一个整数。

我的要求的实体:

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

异常:

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

+0

JPA查询是从来没有想过的表。他们是关于实体的。发布你的实体,并告诉你想做什么。 –

+0

添加到主线程。 我想从我的数据库中读取CustomersCoupons实体。 –

+1

您的实体的ID是CustomerCuoponsPK类型。所以你的回购应该实现JpaRepository ,而不是JpaRepository 。这正是异常的错误信息告诉你的。 –

回答

0

你的实体的ID类型CustomerCuoponsPK的。所以你的回购应该实现JpaRepository,而不是JpaRepository。这正是异常的错误信息告诉你的。根据以下内容更改实施。

package repos; 
 

 
import org.springframework.data.jpa.repository.JpaRepository; 
 

 
import beans.CustomersCoupons; 
 
import beans.CustomersCuoponsPK; 
 

 
public interface CustomersCouponsRepo extends JpaRepository<CustomersCoupons, CustomersCuoponsPK> { 
 

 
\t public default CustomersCoupons findOne(CustomersCuoponsPK pk){ 
 
\t \t return findOne(pk); 
 
\t }