簡體   English   中英

Spring-如何將Hibernate數據保存到SQL Server?

[英]Spring - How to save Hibernate data to 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

@Table(name =“ Person”)公共類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數據庫訪問數據。 通過僅指定要使用Hibernate的數據庫,我不使用@TransactionalSession接口就解決了該問題。 這里找到-感謝@Master Slave。

腳步:

  1. 啟動我的SQL Server並創建一個數據庫
  2. 添加一個application.properties文件以配置Spring使用該數據庫
  3. 首次運行該應用程序時,將spring.jpa.hibernate.ddl-auto =設置為create 下次,將其設置為update 這將在會話之間保留該數據。

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

希望這可以幫助遇到類似問題的其他人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM