我正在使用 Hibernate 开发 Spring 应用程序。我想保存数据,以便即使在启动和停止服务器时它仍然存在。在尝试将数据保存到 SQL 数据库时,我遇到了大量异常,因此我剥离了所有内容并通过尝试将 Person 的实例保存到 SQL 数据库来组合一个简单的、hello world 风格的示例。
我已经浏览了我能找到的每一个线程,但它们都与关系有关——这只是一个没有关系的单一实体。任何建议都非常感谢!
这是我保存条目的尝试:
Person person = new Person("Some Person");
personRepository.save(person);
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(person);
session.getTransaction().commit();
例外:
Caused by: org.hibernate.property.access.spi.PropertyAccessException:
Error accessing field [private java.lang.String com.wdw.core.Person.name] by reflection for persistent property [com.wdw.core.Person#name] : com.wdw.core.Person@4a232870
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.String field com.wdw.core.Person.name to com.wdw.core.Person
模型:
@Entity
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long personId;
private String name;
public Person() {
}
public Person(String name) {
this.name = name;
}
// getters and setters
}
存储库:
public interface PersonRepository extends CrudRepository<Person, Long> {}
休眠.cfg.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.bytecode.use_reflection_optimizer">true</property>
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:8889/the_sideline</property>
<property name="hibernate.connection.username">root</property>
</session-factory>
</hibernate-configuration>
qq_笑_17
森林海
慕斯709654
随时随地看视频慕课网APP
相关分类