【问题标题】:springJPA - How to read from a joined table?spring JPA - 如何从连接表中读取?
【发布时间】:2018-02-08 10:26:47
【问题描述】:

我正在尝试使用具有复合键的表在 spring-boot 中工作。
(在我的例子中是整数和整数)

我使用这个答案来了解如何使用复合键写入到表中:

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

但我不明白如何从中阅读

因为 CrudRepository 的函数“findOne()”只能获取一个参数(在我的例子中是整数),但是 EntityManager 会抛出一个异常,因为它希望得到一个“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;
	}

}

客户优惠券DAO:

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

例外:

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

【问题讨论】:

  • JPA 查询与表无关。它们是关于实体的。发布您的实体,并说明您想做什么。
  • 添加到主线程。我想从我的数据库中读取一个 CustomersCoupons 实体。
  • 您的实体的 ID 是 CustomerCuoponsPK 类型。所以你的 repo 应该实现 JpaRepository,而不是 JpaRepository。这正是异常的错误消息告诉您的内容。

标签: jpa spring-data-jpa composite-key


【解决方案1】:

您的实体的 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);
	}

【讨论】:

    猜你喜欢
    • 2022-09-28
    • 1970-01-01
    • 2019-02-05
    • 2017-11-08
    • 2017-03-07
    • 2018-11-30
    • 1970-01-01
    • 2022-09-24
    • 2017-08-08
    相关资源
    最近更新 更多