尝试在SpringBoot中使用Hibernate时遇到以下问题。 (我正在使用嵌入式H2 DB)。 另外它完美无缺。但是当我想在我的SpringBoot应用程序中进行CRUD操作时,我得到了这个错误:
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 java.lang.String com.h2test.demo.Customer.address] by reflection for persistent property [com.h2test.demo.Customer#address] : ID: 0
First Name: First Name
Last Name: Last Name
BirthDate: 01.01.01
Address: Russia
at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:43)
at org.hibernate.property.access.spi.GetterFieldImpl.getForInsert(GetterFieldImpl.java:58)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.getPropertyValuesToInsert(AbstractEntityTuplizer.java:522)
at org.hibernate.tuple.entity.PojoEntityTuplizer.getPropertyValuesToInsert(PojoEntityTuplizer.java:241)
at org.hibernate.persister.entity.AbstractEntityPersister.getPropertyValuesToInsert(AbstractEntityPersister.java:4743)
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:251)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:178)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:109)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:679)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:671)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:666)
at com.h2test.demo.DemoApplication.main(DemoApplication.java:16)
... 5 more
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.String field com.h2test.demo.Customer.address to com.h2test.demo.Customer
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)
... 21 more
这是我的 SpringBoot应用程序:
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SessionFactory sessionFactory = new Configuration().addAnnotatedClass(Customer.class).configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.save(new Customer("First Name","Last Name","01.01.01","Russia"));
session.close();
sessionFactory.close();
SpringApplication.run(DemoApplication.class, args);
}
}
这是 Customer
实体:
@Entity
@Table(name = "customers")
public class Customer implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "customer_id")
private int id;
@Column(name = "fname")
private String firstName;
@Column(name = "lname")
private String lastName;
@Column(name = "birthDate")
private String birthDate;
@Column(name = "address")
private String address;
public Customer() {
}
public Customer(int id, String firstName, String lastName, String birthDate, String address) {
this.firstName = firstName;
this.lastName = lastName;
this.birthDate = birthDate;
this.address = address;
this.id = id;
}
public Customer(String firstName, String lastName, String birthDate, String address) {
this.firstName = firstName;
this.lastName = lastName;
this.birthDate = birthDate;
this.address = address;
}
public void setId() {this.id = id;}
public long getId() {
return id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAddress() {
return address;
}
public String getBirthDate() {
return birthDate;
}
public void setAddress(String address) {
this.address = address;
}
public void setBirthDate(String birthDate) {
this.birthDate = birthDate;
}
@Override
public String toString() {
return "ID: " + getId() + "\n" +
"First Name: " + getFirstName() + "\n" +
"Last Name: " + getLastName() + "\n" +
"BirthDate: " + getBirthDate() + "\n" +
"Address: " + getAddress();
}
}
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<mapping class="com.h2test.demo.Customer"/>
</session-factory>
</hibernate-configuration>
hibernate.properties
hibernate.connection.driver=org.h2.Driver
hibernate.connection.url=jdbc:h2:mem:test
hibernate.connection.user=sa
hibernate.connection.password=""
hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.hbm2ddl.auto=create
hibernate.show_sql=true
application.properties为空
我读了很多关于这个主题的内容,但我无法解决问题。