嘿伙计们,
我有一个Java应用程序,它读取.csv文件,用信息创建实例,然后我将实例上传到MySQL数据库。 第一次上传总是很完美。
但如果我决定再次上传相同的文件,则会发生错误。直到今天下午没有错误,但现在我无法摆脱它。
所以我使用Envers (@Audited)
,它允许我跟踪对表执行的更改。
- >我希望能够再次上传完全相同的数据,但事实并非如此。
从今天下午开始,我一直收到这个错误:
javax.persistence.PersistenceException: org.hibernate.property.access.spi.PropertyAccessException:错误 通过反射访问字段[private long dto.Project.id] 持久属性[dto.Project#id]:1
然后,有时候,我也会收到这个错误:
javax.persistence.PersistenceException: org.hibernate.property.access.spi.PropertyAccessException:错误 通过反射访问字段[private long dto.Device.ID_FDEV] 持久属性[dto.Device#ID_FDEV]:152372
我检查过每个字段都有它的getter和setter,就是这样。
正如上面所提到的,这种情况在几小时前一直运行良好。我真的不记得改变了任何可能对它产生任何影响的东西。 我检查了代码/将其与早期版本进行了比较,但这没有帮助。
根据https://hibernate.atlassian.net/browse/HHH-10618,这应该是Hibernate中的一个错误,但是因为它之前已经有用,我无法相信。
以防我刚刚尝试过所有旧版本,出于某种原因我不断收到此错误。
提前致谢并希望以某种方式有一个想法,因为它让我发疯。
@Edit: 当我想使用相同的应用程序实例将第二个不同的项目上传到数据库时,也会发生错误。我突然之间做错了什么!
@ EDIT2: 对不起,我忘记发布一些代码了。
public void add(Object object) {
Session session = sessionFactory.openSession();
Transaction t = null;
try {
t = session.beginTransaction();
session.saveOrUpdate(object);
session.flush();
t.commit();
} catch (Exception e) {
System.err.println(e.toString());
} finally {
session.close();
}
}
显然,这是我的功能,将我的数据从应用程序上传到数据库。
private void upload() {
Project project = new Project();
String[] details = projectDetails.split(";");
project.setTitle(details[0]);
project.setPROJEKT1(details[1]);
project.setPROJEKT2(details[2]);
project.setPROJEKT3(details[3]);
project.setDATUM(details[4]);
if (connection.checkIfProjectExistant(project)) {
project = connection.getProject(project.getTitle());
}
for (Device device : deviceList) {
device.setProject(project);
for (DeviceElement element : device.getDeviceElement()) {
element.setUploadTime(new Date());
}
}
project.setDeviceList(deviceList);
connection.add(project);
}
因此,在本部分中,我在上传项目之前设置了项目的一些字段。 如果项目已经存在于DB中,我再次“下载”该项目并创建它的实例,然后将其传递给当前项目。基本上这只是避免在DB中有10个不同的项目,它们都是相同的(因为Project_ID是一个自动生成的递增值)。 所以我的目标是始终更新这个项目。
package dto;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.envers.Audited;
@Audited(withModifiedFlag = true)
@Entity(name = "Project")
@Table(name = "Project")
public class Project {
// Attribute
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "PROJECT_ID", unique = true)
private long id;
这就是“Project”类的样子。
@ EDIT3:
我注意到我没有使用printstacktrace,所以我现在可以给你更多关于错误的细节:
javax.persistence.PersistenceException: org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private int dto.Device.id] by reflection for persistent property [dto.Device#id] : 1
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:149)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:157)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:164)
at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1443)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1423)
at application.MyConnection.add(MyConnection.java:107)
at application.MainController.upload(MainController.java:956)
at application.MainController.btnUpload(MainController.java:144)
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 sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.control.MenuItem.fire(MenuItem.java:462)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1405)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$343(ContextMenuContent.java:1358)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Unknown Source)
Caused by: org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private int dto.Device.id] by reflection for persistent property [dto.Device#id] : 1
at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:71)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:230)
at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:4735)
at org.hibernate.envers.internal.tools.EntityTools.getIdentifier(EntityTools.java:38)
at org.hibernate.envers.internal.tools.EntityTools.entitiesEqual(EntityTools.java:23)
at org.hibernate.envers.internal.entities.mapper.relation.ToOneIdMapper.checkModified(ToOneIdMapper.java:85)
at org.hibernate.envers.internal.entities.mapper.relation.ToOneIdMapper.mapToMapFromEntity(ToOneIdMapper.java:59)
at org.hibernate.envers.internal.entities.mapper.MultiPropertyMapper.map(MultiPropertyMapper.java:88)
at org.hibernate.envers.internal.synchronization.work.ModWorkUnit.<init>(ModWorkUnit.java:43)
at org.hibernate.envers.event.spi.EnversPostUpdateEventListenerImpl.onPostUpdate(EnversPostUpdateEventListenerImpl.java:46)
at org.hibernate.action.internal.EntityUpdateAction.postUpdate(EntityUpdateAction.java:264)
at org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:211)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:589)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:463)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:337)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1437)
... 57 more
Caused by: java.lang.IllegalArgumentException: Can not set int field dto.Device.id to java.lang.Integer
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(Unknown Source)
at sun.reflect.UnsafeIntegerFieldAccessorImpl.getInt(Unknown Source)
at java.lang.reflect.Field.getInt(Unknown Source)
at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:58)
... 73 more
@ Edit4: 映射.cfg.xml文件:
<mapping class="dto.Project"/>
<mapping class="dto.DeviceElement"/>
<mapping class="dto.Device"/>
Device.java中的映射:
@Entity
@Audited(withModifiedFlag = true)
@Table(name = "DEVICE")
public class Device {
// Attribute
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID", unique = true)
private int id;
。 。
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "PROJECT_ID", referencedColumnName = "PROJECT_ID")
private Project project;
@OneToMany(mappedBy = "device", fetch = FetchType.EAGER)
@Cascade(CascadeType.ALL)
private List<DeviceElement> deviceElement = new ArrayList<DeviceElement>();
DeviceElement.java中的映射:
@Entity
@Audited(withModifiedFlag = true)
@Table(name = "DEVICE_ELEMENT")
public class DeviceElement {
// Attribute
@Id
private long ID;
@ManyToOne(fetch = FetchType.EAGER)
private Device device;
好的,所以我发现了一些奇怪的东西。 我想使用envers,因为我将上传相同的项目,但也许子类的一些属性已经改变。
但我不明白的是我如何不断收到错误
javax.persistence.PersistenceException: org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private int dto.Device.id] by reflection for persistent property [dto.Device#id] : 1
当我实际上甚至没有触摸设备ID时? 因为它是一个自动增量主键,它应该增加? 但由于某种原因,某些东西试图设置id,我只是没有得到什么......