[英]How to save with default value using Spring JPA?
Recently I switched into Spring Data JPA and I am wondering how is it possible to save a new object into the database with some default values.最近我切换到 Spring 数据 JPA 我想知道如何使用一些默认值将新的 object 保存到数据库中。
In my example I have to save a book into my database, but, in the owner column, I need to put the value 0. So, this how I did that with JDBC, and it works amazingly well.在我的示例中,我必须将一本书保存到我的数据库中,但是,在所有者列中,我需要将值设为 0。因此,这就是我对 JDBC 所做的,并且效果非常好。
public void save(Book book){
jdbcTemplate.update("INSERT INTO book(name,author,yearOfProduction,owner) VALUES (?, ?, ?, 0)",
book.getName(),book.getAuthor(),book.getYearOfProduction());
}
Now I want to do the same with Spring Data JPA. Here is my save function:现在我想对 Spring 数据 JPA 做同样的事情。这是我的保存 function:
BookService图书服务
@Transactional
public void save(Book book)
{
bookRepository.save(book);
}
I have two objects: Person and Book.我有两个对象:人和书。 The relationships are: one person can have many books and one book has one owner.
关系是:一个人可以拥有多本书,一本书有一个所有者。 Here are my
Person
and Book
classes:这是我的
Person
和Book
类:
Book书
@Entity
@Table(name = "book")
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id",nullable = false)
private int id;
@Column(name="name")
@NotEmpty(message = "Book name can't bee empty")
private String name;
@Column(name="author")
@NotEmpty(message = "Author name can't bee empty")
private String author;
@Column(name="yearOfProduction")
@NotNull(message = "Year of production can't bee empty")
@Min(value = 0,message = "year must be more than 1900")
private int yearOfProduction;
@ManyToOne
@JoinColumn(name = "owner_id", referencedColumnName = "id")
private Person owner;
public Book(String name, String author, int yearOfProduction) {
this.name = name;
this.author = author;
this.yearOfProduction = yearOfProduction;
}
public Book(){
}
public Person getOwner() {
return owner;
}
public void setOwner(Person owner) {
this.owner = owner;
}
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 getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getYearOfProduction() {
return yearOfProduction;
}
public void setYearOfProduction(int yearOfProduction) {
this.yearOfProduction = yearOfProduction;
}
}
Person|人|
@Entity
@Table(name = "person")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "name")
@NotEmpty(message = "Please enter the name")
@Size(min = 1, max = 30, message = "Length must be 2-30 symbols")
//make regex with ФИО;
private String name;
@Column(name = "ageOfBirth")
@Min(value = 0, message = "age of birth must be more than 0")
private int ageOfBirth;
@OneToMany(mappedBy = "owner")
private List<Book> books;
public Person() {
}
public Person(String name, int ageOfBirth) {
this.name = name;
this.ageOfBirth = ageOfBirth;
}
public List<Book> getBooks() {
return books;
}
public void setBooks(List<Book> books) {
this.books = books;
}
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 int getAgeOfBirth() {
return ageOfBirth;
}
public void setAgeOfBirth(int ageOfBirth) {
this.ageOfBirth = ageOfBirth;
}
}
I guess this is impossible to make with Spring Data JPA?我想这不可能用 Spring 数据 JPA 制作? So, I made it with by adding
JdbcTemplate
and I definitely think it is a hard-coding approach to use Spring DATA and JdbcTemplate
together.因此,我通过添加
JdbcTemplate
实现了它,我绝对认为将 Spring DATA 和JdbcTemplate
一起使用是一种硬编码方法。
It's also unable to make with database.它也无法与数据库一起制作。 Down below i am using the default definition of postgres and still get null's when create a new book.
在下面,我正在使用 postgres 的默认定义,并且在创建新书时仍然得到 null。 https://www.baeldung.com/jpa-default-column-values
https://www.baeldung.com/jpa-default-column-values
create table book(
id int primary key generated by default as identity,
name varchar(250) not null,
author varchar(250) not null,
yearOfProduction int not null,
owner_id int default 0 references person(id)
)
@egeorge answered my question. @egeorge 回答了我的问题。 It is impossible to input 0 into owner table.
所有者表中不可能输入 0。
Since 0 has a special value, and is not a real person record, there should not be any value in that field.由于 0 具有特殊值,并且不是真实的个人记录,因此该字段不应该有任何值。 null is the appropriate value for a join column that is not joined to an actual record.
null 是未连接到实际记录的连接列的适当值。 You will need to change the logic that interprets 0 as "free" to check for null instead.
您将需要更改将 0 解释为“免费”的逻辑,以改为检查 null。 (I am surprised your database let you do that to begin with. Foreign key constraints will normally reject a value that is not present in the referred table.)
(我很惊讶你的数据库让你开始这样做。外键约束通常会拒绝引用表中不存在的值。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.