gpt4 book ai didi

java - Spring EntityManager.persist 问题

转载 作者:行者123 更新时间:2023-12-02 10:33:35 26 4
gpt4 key购买 nike

我正在使用 Spring 框架 (Boot+web+jpa) 来实现 REST 服务,并且需要保留一个名为 Sensor 的实体。由于我使用 Hibernate,所以我想继续使用EntityManager。从 Spring Data Access 文档和 Stackoverflow 中回答的一些问题,我可以看到我应该配置 LocalEntityManagerFactoryBeanLocalContainerEntityManagerFactoryBean

现在我能够读取 persist.xml 并通过依赖注入(inject) @PersistenceUnit(unitName="...") EntityManagerFactory 持久保存实体,以防 >LocalContainerEntityManagerFactoryBean。但是,每次当我尝试使用 LocalEntityManagerFactoryBean 持久保存 new Sensor() 实例时,Spring 都会抛出错误,如下所示。

// LocalEntityManagerFactoryBean
@Bean
public LocalEntityManagerFactoryBean entityManagerFactory() {
LocalEntityManagerFactoryBean em = new LocalEntityManagerFactoryBean();
em.setPersistenceUnitName("myunit");
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
return em;
}

持久化(transaction.begin和end在调用者方法中):

private Sensor getSensor(SimpleData sd) {
List<Sensor> sensors = entityManager.createQuery("select s from Sensor s where s.deviceId = :devid", Sensor.class)
.setParameter("devid", sd.getDeviceId())
.getResultList();
Sensor sensor = null;
if(sensors.isEmpty()) {
sensor = new Sensor();
sensor.setDeviceId(sd.getDeviceId());
sensor.setName(sd.getName());
entityManager.persist(sensor); // Throws an exception
deviceIdMapper.put(sensor.getDeviceId(), sensor.getId());
logger.trace("Cannot find the sensor in db, adding: "+sensor.getDeviceId());
} else {
sensor = sensors.get(0);
deviceIdMapper.put(sensor.getDeviceId(), sensor.getId());
logger.trace("Found a sensor from the db: "+sd.getDeviceId());
}
deviceIdMapper.putIfAbsent(sensor.getDeviceId(), sensor.getId());
logger.trace(sensor);
return sensor;
}

异常:

javax.persistence.PersistenceException: org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [protected int com.database.Sensor.id] by reflection for persistent property [com.database.Sensor#id] : com.database.Sensor@6de9600a
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.firePersist(SessionImpl.java:807)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:785)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:350)
at com.sun.proxy.$Proxy108.persist(Unknown Source)
at com.DataListener.getSensor(DataListener.java:69)
at com.DataListener.lambda$0(DataListener.java:87)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
at com.DataListener.processData(DataListener.java:86)
at com.DataListener.onMessage(DataListener.java:49)
at org.apache.activemq.ActiveMQMessageConsumer.dispatch(ActiveMQMessageConsumer.java:1321)
at org.apache.activemq.ActiveMQSessionExecutor.dispatch(ActiveMQSessionExecutor.java:131)
at org.apache.activemq.ActiveMQSessionExecutor.iterate(ActiveMQSessionExecutor.java:202)
at org.apache.activemq.thread.PooledTaskRunner.runTask(PooledTaskRunner.java:129)
at org.apache.activemq.thread.PooledTaskRunner$1.run(PooledTaskRunner.java:47)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [protected int com.database.Sensor.id] by reflection for persistent property [com.database.Sensor#id] : com.database.Sensor@6de9600a
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.event.internal.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:540)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:102)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:62)
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:800)
... 20 more
Caused by: java.lang.IllegalArgumentException: Can not set int field com.database.Sensor.id to com.database.Sensor
at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
at java.base/jdk.internal.reflect.UnsafeIntegerFieldAccessorImpl.getInt(UnsafeIntegerFieldAccessorImpl.java:56)
at java.base/java.lang.reflect.Field.getInt(Field.java:594)
at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:62)
... 28 more

传感器类别:

@Entity
public class Sensor {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected int id;
protected String deviceId;
protected String name;

@OneToMany(mappedBy = "owner", cascade=CascadeType.ALL)
private List<SimpleData> simpleDevices = new ArrayList<>();

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDeviceId() {
return deviceId;
}

public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}


public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public List<SimpleData> getSimpleDevices() {
return simpleDevices;
}

public void setSimpleDevices(List<SimpleData> simpleDevices) {
this.simpleDevices = simpleDevices;
}

}

有两个问题:

  1. 为什么会出现错误?
  2. 如果我没有为 LocalContainerEntityManagerFactoryBean 提供 DataSource,Spring 会抛出错误? LocalEntityManagerFactoryBean 使用 persistence.xml 中的数据源配置 为什么 LocalContainerEntityManagerFactoryBean 不执行相同操作?:

编辑 1

我想我已经找到解决办法了。如果我从依赖项中删除 spring-boot-devtools ,它就可以解决问题。事实上,当我使用 spring-boot-devtools 时,由于某种类加载器问题,我无法读取传感器。也许是某种错误?:

java.lang.ClassCastException: class com.database.Sensor cannot be cast to class com.database.Sensor (com.database.Sensor is in unnamed module of loader 'app'; com.database.Sensor is in unnamed module of loader org.springframework.boot.devtools.restart.classloader.RestartClassLoader @6228d409)

编辑2这是 SimpleData

@Entity
public class SimpleData {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

private double value;

@JsonIgnore
private Date time;

@Transient
private String deviceId;

@JsonIgnore
@Transient
private String name;

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="sensor_id")
private Sensor owner;

public double getValue() {
return value;
}

public void setValue(double value) {
this.value = value;
}

public Date getTime() {
return time;
}

public void setTime(Date time) {
this.time = time;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDeviceId() {
return deviceId;
}

public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}

public Sensor getOwner() {
return owner;
}

public void setOwner(Sensor owner) {
this.owner = owner;
}
}

编辑3

好的,我制作了一个重现问题的小测试程序:https://github.com/hjparker/spring-boot-devtools-issue .

服务器启动后,您可以向 localhost:8080 发出 GET 请求,这将引发错误。在build.gradle中注释掉spring-boot-devtools即可解决问题。我发现这与 spring-dev-tools ( https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-devtools.html ) 中的重新启动类加载器有关,每次重新启动 spring 时,它都会重新加载开发代码。但我认为文件的某些部分不会自动重新加载,从而导致类转换异常,例如加载 CL1 的实体!= 加载 CL2 的实体。有人可以解释一下我是否做错了什么吗?

最佳答案

似乎由 hibernate/Spring ORM 管理的实体或 Bean 类的一些反序列化器没有被 spring-boot-devtools 重新启动类加载器正确重新加载。我必须通过在 META-INF/spring-devtools.properties 中设置以下属性来显式地将执行反序列化的类包含到重新启动类加载器中:

restart.include.hibernate=hibernate.*
restart.include.spring-orm=spring-orm.*

来源:

https://github.com/AxonFramework/AxonFramework/issues/344

https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-devtools.html#using-boot-devtools-customizing-classload

关于java - Spring EntityManager.persist 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53463874/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com