猿问

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

我正在使用 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
浏览 196回答 2
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) {&nbsp; &nbsp; this.name = name;}// getters and setters}您需要向该实体添加更多注释,如上述代码中所述。1)@Column 以便hibernate了解哪个列映射到实体中的哪个属性(如果列名和属性名相同,则不需要这样做)。表列名如上所述需要提及。

慕斯709654

我在这里要做的就是在启动和停止服务器之间保留数据,并能够通过 SQL 数据库访问数据。我通过指定您希望 Hibernate 使用的数据库,在没有使用@Transactional或Session接口的情况下解决了这个问题。在这里找到- 感谢@Master Slave。脚步:启动我的 SQL 服务器并创建数据库添加一个application.properties文件来配置 Spring 以使用该数据库第一次运行应用程序时,设置spring.jpa.hibernate.ddl-auto =为create.&nbsp;下一次,将其设置为update.&nbsp;这将在会话中保留该数据。application.properties:&nbsp;-仅在第一次运行时使用:spring.datasource.url=jdbc:mysql://localhost:8889/my_dbspring.datasource.username=rootspring.datasource.password=rootspring.datasource.driverClassName=com.mysql.jdbc.Driverspring.jpa.show-sql=truespring.jpa.hibernate.ddl-auto=create之后bootRun,my_db将填充数据。停止您的 Spring 服务器并重新启动它,但这次spring.jpa.hibernate.ddl-auto=update在您的application.properties.希望这可以帮助遇到类似问题的其他人。
随时随地看视频慕课网APP

相关分类

Java
我要回答