如何在springjpa中按顺序自动生成id?
How to automatically generate id in sequence in spring jpa?
我需要做一个一对多的序列如下:
例如:
category
sub category
1
1
1
2
2
1
2
2
2
3
3
1
数据模型:
enter image description here
Java code/Mapping:
类别class
package br.com.bank.adapter.repository.entity;
import lombok.Getter;
import lombok.Setter;
import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Set;
@Entity
@Getter
@Setter
@Table(name = "category")
public class CategoryEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name", nullable = false)
private String name;
@Column(name = "update_at")
private LocalDateTime update;
@OneToMany(mappedBy = "category", orphanRemoval = true, cascade = CascadeType.ALL)
private Set<SubCategoryEntity> subCategoryEntity = new HashSet<>();
}
子类别class
package br.com.bank.adapter.repository.entity;
import lombok.Getter;
import lombok.Setter;
import javax.persistence.*;
import java.time.LocalDateTime;
@Entity
@Getter
@Setter
@Table(name = "sub_category")
public class SubCategoryEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name", nullable = false)
private String name;
@Column(name = "update_at")
private LocalDateTime update;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "category_id", nullable = false)
private CategoryEntity category;
}
使用上面的代码,我得到以下结果:
category
sub category
1
1
1
2
2
3
2
4
2
5
3
6
尝试使用以下配置让 JPA 供应商(例如 Hibernate)设置您自己的序列。
public class SubCategoryEntity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SequenceSubCategoryId")
@SequenceGenerator(name = "SequenceSubCategoryId", sequenceName = "SUB_CATEGORY_SEQ")
private Long id;
....
}
这样 id
将从在数据库中创建的相同序列中检索,并且在每个新请求中总是增加 1。
如果您设置了 属性
,这将有效
spring.jpa.hibernate.ddl-auto
具有以下值之一 create
、create-drop
、update
以便允许休眠自动为您在模式中创建此序列。
如果您没有这样配置 属性 您还需要在数据库中执行 DDL 脚本以创建名为 SUB_CATEGORY_SEQ
的序列
我需要做一个一对多的序列如下:
例如:
category | sub category |
---|---|
1 | 1 |
1 | 2 |
2 | 1 |
2 | 2 |
2 | 3 |
3 | 1 |
数据模型:
enter image description here
Java code/Mapping:
类别class
package br.com.bank.adapter.repository.entity;
import lombok.Getter;
import lombok.Setter;
import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Set;
@Entity
@Getter
@Setter
@Table(name = "category")
public class CategoryEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name", nullable = false)
private String name;
@Column(name = "update_at")
private LocalDateTime update;
@OneToMany(mappedBy = "category", orphanRemoval = true, cascade = CascadeType.ALL)
private Set<SubCategoryEntity> subCategoryEntity = new HashSet<>();
}
子类别class
package br.com.bank.adapter.repository.entity;
import lombok.Getter;
import lombok.Setter;
import javax.persistence.*;
import java.time.LocalDateTime;
@Entity
@Getter
@Setter
@Table(name = "sub_category")
public class SubCategoryEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name", nullable = false)
private String name;
@Column(name = "update_at")
private LocalDateTime update;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "category_id", nullable = false)
private CategoryEntity category;
}
使用上面的代码,我得到以下结果:
category | sub category |
---|---|
1 | 1 |
1 | 2 |
2 | 3 |
2 | 4 |
2 | 5 |
3 | 6 |
尝试使用以下配置让 JPA 供应商(例如 Hibernate)设置您自己的序列。
public class SubCategoryEntity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SequenceSubCategoryId")
@SequenceGenerator(name = "SequenceSubCategoryId", sequenceName = "SUB_CATEGORY_SEQ")
private Long id;
....
}
这样 id
将从在数据库中创建的相同序列中检索,并且在每个新请求中总是增加 1。
如果您设置了 属性
,这将有效spring.jpa.hibernate.ddl-auto
具有以下值之一 create
、create-drop
、update
以便允许休眠自动为您在模式中创建此序列。
如果您没有这样配置 属性 您还需要在数据库中执行 DDL 脚本以创建名为 SUB_CATEGORY_SEQ