- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我最近开始 hibernate ,在我的第一个项目中,我在使用 @ManyToMany 时给出了 PropertyAccessException: Error accessing field
我不知道发生了什么,我仔细检查了 getter 和 setter 是否是公共(public)的, 使用 MySQL 和 SQLite 并使用 hibernate v4 和 v5
菜单类:
package ir.sadeghpro.breakfast;
import javax.persistence.*;
import java.util.HashMap;
import java.util.Map;
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
private int count;
private int min;
private int type;
@ManyToMany(targetEntity = Menu.class)
private Map<Menu,Integer> menus = new HashMap<>();
public Product(){}
public Product(int type) {
this.type = type;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getMin() {
return min;
}
public void setMin(int min) {
this.min = min;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public Map<Menu, Integer> getMenus() {
return menus;
}
public void setMenus(Map<Menu, Integer> menus) {
this.menus = menus;
}
}
产品类别:
package ir.sadeghpro.breakfast;
import javax.persistence.*;
import java.util.HashMap;
import java.util.Map;
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
private int count;
private int min;
private int type;
@ManyToMany(targetEntity = Menu.class)
private Map<Menu,Integer> menus = new HashMap<>();
public Product(){}
public Product(int type) {
this.type = type;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getMin() {
return min;
}
public void setMin(int min) {
this.min = min;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public Map<Menu, Integer> getMenus() {
return menus;
}
public void setMenus(Map<Menu, Integer> menus) {
this.menus = menus;
}
}
hibernate 配置文件:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:sqlite:sqlite.db</property>
<property name="dialect">org.hibernate.dialect.SQLiteDialect</property>
<property name="connection.driver_class">org.sqlite.JDBC</property>
<property name="show_sql">true</property>
<!-- <property name="connection.username"/> -->
<!-- <property name="connection.password"/> -->
<!-- DB schema will be updated if needed -->
<property name="hbm2ddl.auto">create</property>
</session-factory>
</hibernate-configuration>
hibernate 版本是 5.3.6.Final。错误是
Sep 07, 2018 11:47:10 AM org.hibernate.internal.ExceptionMapperStandardImpl mapManagedFlushFailure
ERROR: HHH000346: Error during managed flush [org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private int ir.sadeghpro.breakfast.Product.id] by reflection for persistent property [ir.sadeghpro.breakfast.Product#id] : 1]
Sep 07, 2018 11:47:10 AM org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release
INFO: HHH000010: On release of batch it still contained JDBC statements
Exception in Application start method
java.lang.reflect.InvocationTargetException
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:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
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:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException:
/Users/peter/IdeaProjects/breakfast/target/classes/main.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at ir.sadeghpro.breakfast.Run.start(Run.java:13)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Caused by: javax.persistence.PersistenceException: org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private int ir.sadeghpro.breakfast.Product.id] by reflection for persistent property [ir.sadeghpro.breakfast.Product#id] : 1
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:154)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:188)
at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1460)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:511)
at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3283)
at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2479)
at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:473)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:178)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$300(JdbcResourceLocalTransactionCoordinatorImpl.java:39)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:271)
at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:98)
at ir.sadeghpro.breakfast.controller.Main.initialize(Main.java:110)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
... 14 more
Caused by: org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private int ir.sadeghpro.breakfast.Product.id] by reflection for persistent property [ir.sadeghpro.breakfast.Product#id] : 1
at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:75)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:224)
at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:4931)
at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:4631)
at org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:226)
at org.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:276)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:495)
at org.hibernate.type.EntityType.nullSafeSet(EntityType.java:280)
at org.hibernate.persister.collection.AbstractCollectionPersister.writeElement(AbstractCollectionPersister.java:911)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1334)
at org.hibernate.action.internal.CollectionRecreateAction.execute(CollectionRecreateAction.java:50)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:604)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:478)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:356)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1454)
... 24 more
Caused by: java.lang.IllegalArgumentException: Can not set int field ir.sadeghpro.breakfast.Product.id to java.lang.Integer
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.UnsafeIntegerFieldAccessorImpl.getInt(UnsafeIntegerFieldAccessorImpl.java:56)
at java.lang.reflect.Field.getInt(Field.java:574)
at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:62)
... 39 more
Exception running application ir.sadeghpro.breakfast.Run
最佳答案
堆栈跟踪中打印的错误:
Caused by: java.lang.IllegalArgumentException: Can not set int field ir.sadeghpro.breakfast.Product.id to java.lang.Integer
您是否尝试过更改该字段?
您可以搜索similar errors也尝试那里的建议。
关于java - hibernate PropertyAccessException : Error accessing field when use @ManyToMany,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52217651/
我在尝试保持多对多关系时遇到了以下异常。任何线索将不胜感激。我正在使用 hibernate 的 @ManyToMany 注释,如下所示。 如果我从建筑实体中移除房屋然后坚持下去,它就可以正常工作。只有
当 HashMap 中的顶级键可能存在也可能不存在时,如何解决 PropertyAccessExceptions? 在下面的示例中,如果属性存在,它就可以正常工作,但如果变量映射中不存在该属性,则会抛
我有四个实体 RoomNight、BookingCorporateDetails、BookingDiscountDetails 和 BookingDetails,其中 BookingCorporate
我正在研究 hibernate 映射,除了我在 hibernate 尝试将我的对象持久化到系统时遇到的 PropertyAccessException 之外,其他一切都运行良好。 下面是堆栈跟踪 or
这是一个异常(exception): org.hibernate.PropertyAccessException:空值被分配给 com.FOO.hibernate.Tccl.MIN 的原始类型 set
尝试运行以下程序时: public class Runner { public static void main(String args[]) { Configuration
我有一个父表(MHS_USER)和关联的子表(GROUP),与ManyToMany关系映射如下(相关代码如下) @Entity @Table(name = "mhs_user") public cla
我收到以下错误: org.hibernate.PropertyAccessException: IllegalArgumentException 调用 Part.part_id 的 getter 时发
我想配置 Spring-ws 连接管理器以使用 PoolingHttpClientConnectionManager。这是我的配置 camel-context.xml
我已经做了一个在 hibernate 中使用数据库登录的示例。我的 Xml 和实体类是用户.java: import java.util.Date; import javax.persistence.
当我运行下面的代码以显示属于 JTable 中特定类别的项目时,我收到此异常: private void categoryComboBoxActionPerformed(java.awt.event.
我正在开发一个 Spring-boot 2 项目,该项目用于添加 MCQ 问题、选项和答案。每个问题的选项可能有 2 到 6 个选项。 我创建了 3 个表格:一张用于问题,一张用于选项,一张用于答案。
我最近开始 hibernate ,在我的第一个项目中,我在使用 @ManyToMany 时给出了 PropertyAccessException: Error accessing field 我不知道
我使用 spring-boot-1.5.3 和 spring-data-jpa,有两个实体,它们在 cloumn tags 和 nicknames 上都有 JPA Annoatation Elemen
我正在构建一个使用 java reSTLet 和 Hibernate 的应用程序。 我有用户实体类,每当我调用 http get 时。我收到错误。 { "error": "20000:Hibe
将 Spring Boot 与 Hibernate JPA 结合使用 我无法访问 @Entity 的 DAO,它有一个复合键,其中一列是外键。它给了我org.hibernate.PropertyAcc
这段执行 Hibernate 查询的代码作为 Java 程序运行正常,但如果我将它作为 Spring Boot 运行,它就会失败我在某处读到它可能是因为 Hibernate 5.1 中的错误。可能是吗
这是我的 hibernate POJO 类。 StockModel.java import java.util.Date; public class StockModel implements jav
是什么导致了这个异常,我无法找出。 Request processing failed; nested exception is javax.persistence.PersistenceExcept
我环顾四周寻找答案,看起来它与反射有关,但由于我没有处理过它们,所以我不确定如何进行。我正在尝试构建一个 HQL 查询,该查询仅返回表中与作为 FK 的列匹配的行到另一个表中的 PK。 这是我的代码:
我是一名优秀的程序员,十分优秀!