Spring-如何将Hibernate数据保存到SQL server



我正在使用Hibernate开发一个Spring应用程序。我想保存数据,这样即使在启动和停止服务器时数据也会持续存在。在尝试将数据保存到SQL数据库时,我遇到了大量异常,因此我剥离了所有内容,并通过尝试将Person的实例保存到SQL数据库来创建一个简单的hello世界风格的示例。

我已经浏览了我能找到的每一个线索,但它们都与关系有关——这只是一个没有关系的单一实体。非常感谢任何建议!

以下是我保存条目的尝试:

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> {}

hibernate.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>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="com.wdw.core.Person"/>
</session-factory>
</hibernate-configuration>

编辑:当我创建一个没有表的空数据库并运行程序时,它会创建没有条目的表Person:

mysql> use test_1
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+---------------------------+
| Tables_in_test_1
+---------------------------+
| Person                    |
+---------------------------+
1 row in set (0.00 sec)
mysql> select * from Person;
Empty set (0.00 sec)
@Entity

@表(name="Person"(公共类人员{

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="person_id")
private long personId;
@Column(name="name")
private String name;
public Person() {
}
public Person(String name) {
this.name = name;
}
// getters and setters

}

您需要向这个实体添加更多的注释,如上面代码中所提到的。1( @Column这样hibernate就可以了解哪个列映射到实体中的哪个属性(如果列名和属性名相同,则不需要这样做(。需要如上所述提及表列名。

我在这里想做的就是在启动和停止服务器之间保持数据,并能够通过SQL数据库访问数据。我在不使用@TransactionalSession接口的情况下解决了这个问题,只需指定您希望Hibernate使用的数据库。在这里找到-感谢@Master-Slave。

步骤:

  1. 启动SQL服务器并创建数据库
  2. 添加一个application.properties文件以配置Spring使用该数据库
  3. 第一次运行应用程序时,请将spring.jpa.hibernate.ddl-auto =设置为create。下次,将其设置为update。这将使数据在会话中保持不变

应用程序。属性:-仅用于第一次运行:

spring.datasource.url=jdbc:mysql://localhost:8889/my_db
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create

bootRun之后,将用数据填充my_db。停止您的Spring服务器并重新启动它,但这次使用application.properties中的spring.jpa.hibernate.ddl-auto=update

希望这能帮助其他遇到类似问题的人。

最新更新