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
JPA查詢是從來沒有想過的表。他們是關於實體的。發佈你的實體,並告訴你想做什麼。 –
添加到主線程。 我想從我的數據庫中讀取CustomersCoupons實體。 –
您的實體的ID是CustomerCuoponsPK類型。所以你的回購應該實現JpaRepository,而不是JpaRepository 。這正是異常的錯誤信息告訴你的。 –