我有四个实体RoomNight
,BookingCorporateDetails
,BookingDiscountDetails
和BookingDetails
,其中BookingCorporateDetails
,BookingDiscountDetails
和BookingDetails
引用列(RoomNight
)上的room_night_booking_id
。
我试图在BookingCorporateDetails
,BookingDiscountDetails
和BookingDetails
上创建外键联接,以便一起保存/获取/删除所有数据。我能够将数据保存到数据库中,但是当我尝试检索时,我得到了Hibernate PropertyAccessException。实体表结构为-
AbstractJpaEntity.java
@MappedSuperclass
@SuppressWarnings("serial")
public abstract class AbstractJpaEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected Long id;
@Column(name = "created_at")
@Temporal(TemporalType.TIMESTAMP)
protected Date createdAt;
@Column(name = "created_by", length = 32, nullable = true)
protected String createdBy;
@Column(name = "updated_at")
@Temporal(TemporalType.TIMESTAMP)
protected Date updatedAt;
@Column(name = "updated_by", length = 32, nullable = true)
protected String updatedBy;
@Column(name = "is_published", columnDefinition = "tinyint(1) default 1")
protected boolean isPublished;
@Column(name = "is_deleted", columnDefinition = "tinyint(1) default 0")
protected boolean isDeleted;
// Getters and Setters
}
AbstractBookingEntity.java
@MappedSuperclass
public abstract class AbstractBookingEntity extends AbstractJpaEntity {
private static final long serialVersionUID = 6980195228404112444L;
@Column(name = "booking_id", nullable = false)
protected String bookingId;
@Column(name = "room_night_booking_id", nullable = false)
protected String roomNightBookingId;
public String getBookingId() {
return bookingId;
}
public void setBookingId(String bookingId) {
this.bookingId = bookingId;
}
public String getRoomNightBookingId() {
return roomNightBookingId;
}
public void setRoomNightBookingId(String roomNightBookingId) {
this.roomNightBookingId = roomNightBookingId;
}
}
RoomNightEntity.java
@Entity(name = "room_night")
@Table(name = "room_night", uniqueConstraints = { @UniqueConstraint(name = "key_rm_order_sub_order", columnNames = { "booking_id", "room_night_booking_id" }) })
public class RoomNightEntity extends AbstractBookingEntity {
private static final long serialVersionUID = 7661960864475909694L;
@Column(name = "ota_booking_id", nullable = false)
private String otaBookingId;
@Column(name = "property_id", nullable = false)
private Long propertyId;
@Column(name = "room_type_id", nullable = false)
private Integer roomTypeId;
@Column(name = "booking_status", nullable = false)
private Integer bookingStatus;
@Column(name = "occupancy", nullable = false)
private Integer occupancy;
@Column(name = "user_id", nullable = false)
private Long userId;
@Column(name = "payment_mode", nullable = false)
private String paymentMode;
@Column(name = "stay_date")
@Temporal(TemporalType.DATE)
private Date stayDate;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "roomNightEntity", fetch = FetchType.LAZY)
private BookingDetailsEntity bookingDetailsEntity;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "roomNightEntity", fetch = FetchType.LAZY)
private BookingCorporateDetailsEntity bookingCorporateDetailsEntity;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "roomNightEntity", fetch = FetchType.LAZY)
private BookingDiscountDetailsEntity bookingDiscountDetailsEntity;
// Getters and Setters
}
BookingCorporateDetailsEntity.java
@Entity(name = "booking_corporate_details")
@Table(name = "booking_corporate_details", uniqueConstraints = { @UniqueConstraint(name = "key_bcd_order_sub_order", columnNames = { "booking_id", "room_night_booking_id" }) })
public class BookingCorporateDetailsEntity extends AbstractBookingEntity {
private static final long serialVersionUID = -5577224607005257364L;
@Column(name = "btc_company")
private String billToCompany;
@Column(name = "cr_company_name", nullable = false)
private String companyName;
@Column(name = "cr_manager_name")
private String managerName;
@Column(name = "cr_manager_email")
private String managerEmail;
@Column(name = "cr_manager_phone")
private String managerPhone;
@OneToOne(optional = true, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "room_night_booking_id", referencedColumnName = "room_night_booking_id", insertable = false, updatable = false)
private RoomNightEntity roomNightEntity;
// Getters And Setters
}
BookingDetailsEntity.java
@Entity(name = "booking_details")
@Table(name = "booking_details", uniqueConstraints = { @UniqueConstraint(name = "key_bd_order_sub_order", columnNames = { "booking_id", "room_night_booking_id" }) })
public class BookingDetailsEntity extends AbstractBookingEntity {
private static final long serialVersionUID = -4487373751934892176L;
@Column(name = "guest_status", nullable = false)
private int guestStatus;
@Column(name = "is_vip")
private boolean isVip;
@Column(name = "is_walkin")
private int isWalkin;
@Column(name = "room_number")
private String roomNumber;
@Column(name = "shifted_from")
private long shiftedFrom;
@Column(name = "checkin_datetime")
private Date checkinDateTime;
@Column(name = "checkout_datetime")
private Date checkoutDateTime;
@Column(name = "noshow_datetime")
private Date noShowDateTime;
@Column(name = "cancel_datetime")
private Date cancelDateTime;
@Column(name = "cancelled_reason")
private String cancelReason;
@OneToOne(optional = true, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "room_night_booking_id", referencedColumnName = "room_night_booking_id", insertable = false, updatable = false)
private RoomNightEntity roomNightEntity;
// Getters and Setters
}
BookingDiscountDetailsEntity.java
@Entity(name = "booking_discount_details")
@Table(name = "booking_discount_details", uniqueConstraints = { @UniqueConstraint(name = "key_bdd_order_sub_order", columnNames = { "booking_id", "room_night_booking_id" }) })
public class BookingDiscountDetailsEntity extends AbstractBookingEntity {
private static final long serialVersionUID = -756417075758184555L;
@Column(name = "discount", nullable = false)
private double discount;
@Column(name = "discount_type")
private int discountType;
@Column(name = "coupon_code")
private String couponCode;
@OneToOne(optional = true, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "room_night_booking_id", referencedColumnName = "room_night_booking_id", insertable = false, updatable = false)
private RoomNightEntity roomNightEntity;
// Getters and Setters
}
当执行get / delete
RoomNightEntity
代码时,出现以下异常-org.springframework.orm.jpa.JpaSystemException: Error accessing field [public java.lang.String com.casa.fabbookingbase.entity.AbstractBookingEntity.roomNightBookingId] by reflection for persistent property [com.casa.fabbookingbase.entity.RoomNightEntity#roomNightBookingId] : 2; nested exception is org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [public java.lang.String com.casa.fabbookingbase.entity.AbstractBookingEntity.roomNightBookingId] by reflection for persistent property [com.casa.fabbookingbase.entity.RoomNightEntity#roomNightBookingId] : 2
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:314)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:225)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:436)
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.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy65.deleteByBookingId(Unknown Source)
at com.casa.fabbooking.service.BookingServiceTest.testDeleteBooking(BookingServiceTest.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.test.context.junit4.SpringTestMethod.invoke(SpringTestMethod.java:160)
at org.springframework.test.context.junit4.SpringMethodRoadie.runTestMethod(SpringMethodRoadie.java:233)
at org.springframework.test.context.junit4.SpringMethodRoadie$RunBeforesThenTestThenAfters.run(SpringMethodRoadie.java:333)
at org.springframework.test.context.junit4.SpringMethodRoadie.runWithRepetitions(SpringMethodRoadie.java:217)
at org.springframework.test.context.junit4.SpringMethodRoadie.runTest(SpringMethodRoadie.java:197)
at org.springframework.test.context.junit4.SpringMethodRoadie.run(SpringMethodRoadie.java:143)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:160)
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:97)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [public java.lang.String com.casa.fabbookingbase.entity.AbstractBookingEntity.roomNightBookingId] by reflection for persistent property [com.casa.fabbookingbase.entity.RoomNightEntity#roomNightBookingId] : 2
at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:43)
at org.hibernate.tuple.component.AbstractComponentTuplizer.getPropertyValue(AbstractComponentTuplizer.java:58)
at org.hibernate.type.ComponentType.getPropertyValue(ComponentType.java:414)
at org.hibernate.type.ComponentType.getHashCode(ComponentType.java:238)
at org.hibernate.engine.spi.EntityUniqueKey.generateHashCode(EntityUniqueKey.java:67)
at org.hibernate.engine.spi.EntityUniqueKey.<init>(EntityUniqueKey.java:48)
at org.hibernate.type.EntityType.loadByUniqueKey(EntityType.java:679)
at org.hibernate.type.EntityType.resolve(EntityType.java:427)
at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:151)
at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:125)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1132)
at org.hibernate.loader.Loader.processResultSet(Loader.java:992)
at org.hibernate.loader.Loader.doQuery(Loader.java:930)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:336)
at org.hibernate.loader.Loader.doList(Loader.java:2610)
at org.hibernate.loader.Loader.doList(Loader.java:2593)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2422)
at org.hibernate.loader.Loader.list(Loader.java:2417)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:501)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:371)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:216)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1339)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:87)
at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:606)
at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:483)
at org.hibernate.jpa.criteria.compile.CriteriaQueryTypeQueryAdapter.getResultList(CriteriaQueryTypeQueryAdapter.java:50)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:368)
at com.sun.proxy.$Proxy90.getResultList(Unknown Source)
at org.springframework.data.jpa.repository.query.JpaQueryExecution$DeleteExecution.doExecute(JpaQueryExecution.java:275)
at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:82)
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:116)
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:106)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:482)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:460)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)
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:281)
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)
... 31 more
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.String field com.casa.fabbookingbase.entity.AbstractBookingEntity.roomNightBookingId to java.lang.Long
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36)
at java.lang.reflect.Field.get(Field.java:393)
at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:39)
... 76 more
PS:我在MySQL 5.7上将Spring Data JPA 1.10.7.RELEASE与Hibernate 5.1.0.Final一起使用。
最佳答案
在RoomNightEntity的表中检查room_night_booking_id
列的类型(如果可以正确看到,请检查room_night
。)。它不能是数字类型。
看起来Java尝试将类解析为Long
,然后尝试将Long
分配给String
属性。