为了账号安全,请及时绑定邮箱和手机立即绑定

Spring - 如何将休眠数据保存到 SQL 服务器?

Spring - 如何将休眠数据保存到 SQL 服务器?

qq_笑_17 2022-01-12 16:40:45
我正在使用 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@4a232870Caused by: java.lang.IllegalArgumentException: Can not set java.lang.String field com.wdw.core.Person.name to com.wdw.core.Person模型:@Entitypublic 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>
查看完整描述

2 回答

?
森林海

TA贡献2011条经验 获得超2个赞

@Entity

@Table(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了解哪个列映射到实体中的哪个属性(如果列名和属性名相同,则不需要这样做)。表列名如上所述需要提及。


查看完整回答
反对 回复 2022-01-12
?
慕斯709654

TA贡献1840条经验 获得超5个赞

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

脚步:

  1. 启动我的 SQL 服务器并创建数据库

  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 服务器并重新启动它,但这次spring.jpa.hibernate.ddl-auto=update在您的application.properties.


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


查看完整回答
反对 回复 2022-01-12
  • 2 回答
  • 0 关注
  • 195 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号