执行 Hibernate 查询的代码作为 Java 程序运行正常,但如果我作为 Spring Boot 运行它,它会失败 我在某处读过这可能是因为 Hibernate 5.1 中的错误。可能是吗? 我正在使用 Spring Boot 2.0。 会不会有问题?
@SpringBootApplication
public class SpringCd244ManyToManyBootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCd244ManyToManyBootApplication.class, args);
// create session factory
SessionFactory factory = new Configuration()
.configure("hibernate.cfg.xml")
.addAnnotatedClass(Instructor.class)
.addAnnotatedClass(InstructorDetail.class)
.addAnnotatedClass(Course.class)
.addAnnotatedClass(Review.class)
.addAnnotatedClass(Student.class)
.buildSessionFactory();
// create session
Session session = factory.getCurrentSession();
try {....
在 Spring Boot 中运行它时出现错误。有什么想法吗?
2018-01-11 20:45:33.261 INFO 5772 --- [ restartedMain] org.hibernate.orm.connections.pooling : HHH10001008: Cleaning up connection pool [jdbc:mysql://localhost:3306/webapp?useSSL=false]
Exception in thread "restartedMain" 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 org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private io.scalando.Instructor io.scalando.Course.instructor] by reflection for persistent property [io.scalando.Course#instructor] : Course [id=0, title=Pacman - How To Score One Million Points]
at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:74)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.getPropertyValue(AbstractEntityTuplizer.java:626)
我有一个类似的问题,通过删除此依赖项解决了:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
我使用的是springboot 3.3.6和jdk 21。上述问题仍然存在