我尝试使用具有复合键的表在spring-boot中工作。
(在我的情况下为整数和整数)
我使用这个答案来了解如何使用复合键写到表:
https://stackoverflow.com/a/3588400/7658754
但我无法理解如何阅读。
作为函数" findOne()" CrudRepository只能得到一个参数(在我的例子中是Integer)但是然后EntityManager会抛出一个Exception,因为它希望得到一个" Composite-Key-Bean" (比如" TimePK"在上面的答案中)而不是整数。
我要求的实体:
CustomersCouponsPK.class:
package beans;
import java.io.Serializable;
import javax.persistence.Embeddable;
@Embeddable
public class CustomersCuoponsPK implements Serializable {
private Integer cust_id;
private Integer coupon_id;
public CustomersCuoponsPK() {
}
public CustomersCuoponsPK(int cust_id, int coupon_id) {
super();
this.cust_id = cust_id;
this.coupon_id = coupon_id;
}
public int getCust_id() {
return cust_id;
}
public void setCust_id(int cust_id) {
this.cust_id = cust_id;
}
public int getCoupon_id() {
return coupon_id;
}
public void setCoupon_id(int coupon_id) {
this.coupon_id = coupon_id;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((coupon_id == null) ? 0 : coupon_id.hashCode());
result = prime * result + ((cust_id == null) ? 0 : cust_id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
CustomersCuoponsPK other = (CustomersCuoponsPK) obj;
if (coupon_id == null) {
if (other.coupon_id != null)
return false;
} else if (!coupon_id.equals(other.coupon_id))
return false;
if (cust_id == null) {
if (other.cust_id != null)
return false;
} else if (!cust_id.equals(other.cust_id))
return false;
return true;
}
}

CustomersCoupons.class:
package beans;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
@Entity
public class CustomersCoupons {
@EmbeddedId
private CustomersCuoponsPK customersCuoponsPK;
public CustomersCoupons() {
}
public CustomersCoupons(CustomersCuoponsPK customersCuoponsPK) {
super();
this.customersCuoponsPK = customersCuoponsPK;
}
public CustomersCuoponsPK getCustomersCuoponsPK() {
return customersCuoponsPK;
}
public void setCustomersCuoponsPK(CustomersCuoponsPK customersCuoponsPK) {
this.customersCuoponsPK = customersCuoponsPK;
}
}

CustomersCouponsRepo:
package repos;
import org.springframework.data.jpa.repository.JpaRepository;
import beans.CustomersCoupons;
import beans.CustomersCuoponsPK;
public interface CustomersCouponsRepo extends JpaRepository<CustomersCoupons, Integer> {
public default CustomersCoupons findOne(int cust_id, int coupon_id){
if(this.getOne(cust_id).equals(this.getOne(coupon_id))){
CustomersCuoponsPK pk = new CustomersCuoponsPK();
pk.setCoupon_id(coupon_id);
pk.setCust_id(cust_id);
CustomersCoupons cc = new CustomersCoupons();
cc.setCustomersCuoponsPK(pk);
return cc;
}
return null;
}
}
&#13;
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 {
@Autowired
private CustomersCouponsRepo repo;
public void createCustomerCoupon(int cust_id, int coupon_id) {
CustomersCoupons customersCoupons = new CustomersCoupons(new CustomersCuoponsPK(cust_id, coupon_id));
repo.save(customersCoupons);
}
public List<CustomersCoupons> getAllCustomersCoupons() {
return (List<CustomersCoupons>) repo.findAll();
}
public CustomersCoupons readCustomersCoupons(int cust_id, int coupon_id) {
return repo.findOne(cust_id, coupon_id);
}
public void updateCustomersCoupons(CustomersCoupons customersCoupons) {
repo.save(customersCoupons);
}
public void deleteC(int cust_id, int coupon_id) {
}
}
&#13;
例外:
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
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:384)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:246)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:488)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:57)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy76.getOne(Unknown Source)
at repos.CustomersCouponsRepo.findOne(CustomersCouponsRepo.java:11)
at java.lang.invoke.MethodHandle.invokeWithArguments(Unknown Source)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:62)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:57)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy76.findOne(Unknown Source)
at DAOs.CustomersCouponsDAO.readCustomersCoupons(CustomersCouponsDAO.java:28)
at DAOs.CustomersCouponsDAO$$FastClassBySpringCGLIB$$d50d5f2d.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673)
at DAOs.CustomersCouponsDAO$$EnhancerBySpringCGLIB$$1907e960.readCustomersCoupons(<generated>)
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
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.getReference(AbstractEntityManagerImpl.java:1019)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:298)
at com.sun.proxy.$Proxy69.getReference(Unknown Source)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.getOne(SimpleJpaRepository.java:278)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:504)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:489)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:461)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:56)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
... 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
at org.hibernate.event.internal.DefaultLoadEventListener.checkIdClass(DefaultLoadEventListener.java:166)
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:86)
at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1129)
at org.hibernate.internal.SessionImpl.access$2600(SessionImpl.java:164)
at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.getReference(SessionImpl.java:2669)
at org.hibernate.internal.SessionImpl.load(SessionImpl.java:965)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.getReference(AbstractEntityManagerImpl.java:1013)
... 59 more
&#13;
答案 0 :(得分:0)
您的实体的ID是CustomerCuoponsPK类型。所以你的repo应该实现JpaRepository,而不是JpaRepository。这正是异常的错误消息告诉你的。根据以下内容更改实施。
package repos;
import org.springframework.data.jpa.repository.JpaRepository;
import beans.CustomersCoupons;
import beans.CustomersCuoponsPK;
public interface CustomersCouponsRepo extends JpaRepository<CustomersCoupons, CustomersCuoponsPK> {
public default CustomersCoupons findOne(CustomersCuoponsPK pk){
return findOne(pk);
}