Spring Boot JPA插入和更新 [英] Spring Boot JPA Insert and Update

查看:117
本文介绍了Spring Boot JPA插入和更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我环顾四周已经找到了这个答案大约一个小时,但我看不到它的发布.因此,我在28分钟内通过Udemy购买了Spring Framework Master Class.我们已经开始实施JPA.但是,Spring Boot的版本有所不同(他使用的是2.0.3,我使用的是2.4.0).现在我知道这就是问题所在.因此,任务是简单地连接到h2数据库并与数据进行交互.这是我正在使用的当前代码设置:

Alright, I've looked around to find this answer for about an hour and I can't see it posted. So I bought the Spring Framework Master Class from in28minutes on Udemy. We have started implementing JPA. However, the Spring Boot versions are different( he is using 2.0.3, I am using 2.4.0). Now I know that's the issue. So the task is to simply connect to a h2 database, and interact with the data. Here is the current code setup I am using:

JpaDemoApplication.java

package com.in28minutes.database.databasedemo;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.in28minutes.database.databasedemo.entity.Person;
import com.in28minutes.database.databasedemo.jpa.PersonJpaRepository;

@SpringBootApplication
public class JpaDemoApplication implements CommandLineRunner {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    PersonJpaRepository repository;

    public static void main(String[] args) {
        SpringApplication.run(JpaDemoApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        
        logger.info("User id 10001 -> {}", repository.findById(10001));
        
        logger.info("Inserting -> {}", 
                repository.insert(new Person("Tara", "Berlin", new Date())));
        /*
        logger.info("Update 10003 -> {}", 
                repository.update(new Person(10003, "Pieter", "Utrecht", new Date())));
        
        //repository.deleteById(10002);

        logger.info("All users -> {}", repository.findAll());
        */
    }
}

Person.java

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQuery;

@Entity
@NamedQuery(name="find_all_persons", query="select p from Person p")
public class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String name;
    private String location;
    private Date birthDate;

    public Person() {}

    public Person(int id, String name, String location, Date birthDate) {
        super();
        this.id = id;
        this.name = name;
        this.location = location;
        this.birthDate = birthDate;
    }

    public Person(String name, String location, Date birthDate) {
        super();
        this.name = name;
        this.location = location;
        this.birthDate = birthDate;
    }

    public int getId() { return id;}
    public void setId(int id) {this.id = id;}
    public String getName() {return name;}
    public void setName(String name) {this.name = name;}
    public String getLocation() {return location;}
    public void setLocation(String location) {this.location = location;}
    public Date getBirthDate() {return birthDate;}
    public void setBirthDate(Date birthDate) {this.birthDate = birthDate;}
    @Override
    public String toString() {
        return String.format("\nPerson [id=%s, name=%s, location=%s, birthDate=%s]", id, name, location, birthDate);
    }
}

PersonJpaRepository.java

package com.in28minutes.database.databasedemo.jpa;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.transaction.Transactional;

import org.springframework.stereotype.Repository;

import com.in28minutes.database.databasedemo.entity.Person;

@Repository
@Transactional
public class PersonJpaRepository {

    // connect to the database
    @PersistenceContext
    EntityManager entityManager;

    public List<Person> findAll() {
        TypedQuery<Person> namedQuery = entityManager.createNamedQuery("find_all_persons", Person.class);
        return namedQuery.getResultList();
    }

    public Person findById(int id) {
        return entityManager.find(Person.class, id);// JPA
    }

    public Person update(Person person) {
        return entityManager.merge(person);
    }

    public Person insert(Person person) {
        return entityManager.merge(person);
    }

    public void deleteById(int id) {
        Person person = findById(id);
        entityManager.remove(person);
    }

}

application.properties

spring.h2.console.enabled=true
spring.jpa.show-sql=true
#logging.level.root=debug
#spring.jpa.hibernate.use-new-id-generator-mappings=false
spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=jdbc:h2:mem:testdb
spring.data.jpa.repositories.bootstrap-mode=default

data.sql

INSERT INTO PERSON (ID, NAME, LOCATION, BIRTH_DATE)
VALUES(10001, 'Ranga', 'Hyderabad', sysdate());
INSERT INTO PERSON (ID, NAME, LOCATION, BIRTH_DATE)
VALUES(10002, 'James', 'New York', sysdate());
INSERT INTO PERSON (ID, NAME, LOCATION, BIRTH_DATE)
VALUES(10003, 'Pieter', 'Amsterdam', sysdate());

schema.sql

create table person
(
    id integer not null,
    name varchar(255) not null,
    location varchar(255),
    birth_date timestamp,
    primary key(id)
);

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.in28minutes.database</groupId>
    <artifactId>database-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>database-demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>15</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

我知道很多,但是我对Spring还是很陌生,由于我开始一份新工作,我必须在一月份之前将其减少.具体错误是创建了表,通过id进行的第一个选择有效,但是插入传递了空值:

I know this is a lot, but I am very new to Spring, and I HAVE to have this down by January, due to me starting a new job. The specific error is that the table is created, the first select by id works, but the insert passes a null value:

Hibernate: insert into person (id, birth_date, location, name) values (null, ?, ?, ?)
2020-12-03 10:59:16.724  WARN 14464 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 23502, SQLState: 23502
2020-12-03 10:59:16.724 ERROR 14464 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : NULL not allowed for column "ID"; SQL statement:
insert into person (id, birth_date, location, name) values (null, ?, ?, ?) [23502-200]

我尝试了5种不同的建议,但都没有奏效.我究竟做错了什么?指向可以解释其工作原理的东西,我一直在寻找更多当前的学习资源.

I have tried 5 different suggestions, but none have worked. What am I doing wrong? Pointing to something that can explain how to do it works as well, I'm always looking for more current learning resources.

推荐答案

  • @GeneratedValue批注用于自动生成主键值.共有4种生成类型:自动 IDENTITY SEQUENCE TABLE .

    • @GeneratedValue annotation is used to generate primary key value automatically. There are 4 generation types: AUTO, IDENTITY, SEQUENCE, TABLE.

      AUTO :持久性提供程序将基于主键属性的类型确定值. Numeric值是基于序列生成器生成的,而UUID值则使用UUIDGenerator.

      AUTO: The persistence provider will determine values based on the type of the primary key attribute. Numeric values are generated based on a sequence generator and UUID values use the UUIDGenerator.

      IDENTITY :它依赖于数据库中Identity列生成的值,这意味着它们是自动递增的.请注意, IDENTITY 生成会禁用批量更新.

      IDENTITY: It relies on the value generated by an identity column in the database, meaning they are auto-incremented. Note that IDENTITY generation disables batch updates.

      您正在使用 IDENTITY 策略,而架构中没有identity列.更改schema.sql如下:

      You are using IDENTITY strategy without an identity column in your schema. Change the schema.sql as follows:

      create table person
      (
          id integer generated by default as identity not null,
          name varchar(255) not null,
          location varchar(255),
          birth_date timestamp,
          primary key(id)
      );
      

      这篇关于Spring Boot JPA插入和更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆