Spring-如何将Hibernate数据保存到SQL Server? [英] Spring - How to save Hibernate data to SQL server?
问题描述
我正在使用Hibernate开发Spring应用程序.我想保存数据,以便即使在启动和停止服务器时也能保留数据.尝试将数据保存到SQL数据库时,我遇到了很多异常,因此我尝试将Person的实例保存到SQL数据库中,剥离了所有内容,并整理了一个简单的hello世界风格示例.
I'm working on a Spring application with Hibernate. I'd like to save the data so that it persists even when starting and stopping the server. When attempting to save data to an SQL database, I run into a ton of exceptions so I stripped everything back and put together a simple, hello world style example by attempting to save an instance of a Person to the SQL database.
我遍历了所有可以找到的线程,但是它们都与关系有关-这只是一个没有关系的实体.任何建议,不胜感激!
I've been through every thread I can find but they're all relating to relationships - this is just a single entity with no relationships. Any advice greatly appreciated!
这是我保存条目的尝试:
Here's my attempt at saving an entry:
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:
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:
When I create an empty database with no tables and run the program, it creates the table Person with no entries:
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)
推荐答案
我在这里要做的就是在启动和停止服务器之间保留数据,并能够通过SQL数据库访问数据.通过仅指定要让Hibernate使用的数据库,我无需使用@Transactional
或Session
接口就解决了该问题.在在此处找到-感谢@Master Slave.
All I wanted to do here was persist the data between starting and stopping the server, and be able to access the data via an SQL database. I solved the issue without using @Transactional
or the Session
interface, by just specifying the database you want Hibernate to use. Found here - thank you to @Master Slave.
步骤:
- 启动我的SQL服务器并创建数据库
- 添加
application.properties
文件以将Spring配置为使用该数据库 - 第一次运行应用程序时,将
spring.jpa.hibernate.ddl-auto =
设置为create
.下次,将其设置为update
.这将在会话之间保留该数据.
- Spin up a my SQL server and create a database
- Add an
application.properties
file to configure Spring to use that database - The first time you run the app, set
spring.jpa.hibernate.ddl-auto =
tocreate
. The next time, set it toupdate
. This will persist that data across sessions.
application.properties:-仅用于首次运行:
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
.
After bootRun
, my_db
will be populated with data. Stop your Spring server and restart it, but this time with spring.jpa.hibernate.ddl-auto=update
in your application.properties
.
希望这可以帮助遇到类似问题的其他人.
Hope this helps somebody else who runs into similar issues.
这篇关于Spring-如何将Hibernate数据保存到SQL Server?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!