【spring data jpa】ID生成策略(GeneratedValue)

一、添加依赖

在pom文件中添加spring data jpa依赖


    org.springframework.data
    spring-data-jpa
    3.1.4


    org.hibernate
    hibernate-core
    6.3.1.Final


    org.apache.derby
    derby
    10.14.2.0
    test


    junit
    junit
    4.13.1
    test

这里使用的是spring data jpa的3.1.4版本,另外引入了hibernate、derby、junit等包

二、创建实体

在src/test/java的cn.horse.chapter02.entity包下创建User实体类

package cn.horse.chapter02.entity;

import jakarta.persistence.*;

@Entity(name = "T_USER")
public class User {

    @Id
    private Integer id;
    private String username;

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getUsername() {
        return username;
    }
}

@Entity:实体表映射,name用于指定表名称

@Id:指定主键

三、创建持久化类

在src/test/java的cn.horse.chapter02.repository包下创建UserRepository持久化类

package cn.horse.chapter02.repository;

import cn.horse.chapter02.entity.User;
import org.springframework.data.repository.CrudRepository;

public interface UserRepository extends CrudRepository {
}

UserRepository继承CrudRepository类,这样UserRepository类将可以使用父类中提供的CRUD方法;另外继承CrudRepository类需要指定泛型,第一个泛型类型是持久化实体类型,第二个泛型类型是持久化主键类型

四、应用配置

在src/test/java的cn.horse.chapter01.config包下创建ApplicationConfig配置类

package cn.horse.chapter02.config;

import jakarta.persistence.EntityManagerFactory;
import org.hibernate.cfg.AvailableSettings;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.Properties;

@Configuration
@EnableJpaRepositories(basePackages = "cn.horse.chapter02.repository")
@EnableTransactionManagement
class ApplicationConfig {

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
        driverManagerDataSource.setDriverClassName("org.apache.derby.jdbc.EmbeddedDriver");
        driverManagerDataSource.setUrl("jdbc:derby:demo;create=true");
        driverManagerDataSource.setUsername("");
        driverManagerDataSource.setPassword("");
        return driverManagerDataSource;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setGenerateDdl(false);
        vendorAdapter.setShowSql(true);

        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setPackagesToScan("cn.horse.chapter02.entity");
        factory.setDataSource(dataSource());

        Properties jpaProperties = new Properties();
        jpaProperties.put(AvailableSettings.HBM2DDL_AUTO, "create");
        factory.setJpaProperties(jpaProperties);
        return factory;
    }

    @Bean
    public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {

        JpaTransactionManager txManager = new JpaTransactionManager();
        txManager.setEntityManagerFactory(entityManagerFactory);
        return txManager;
    }
}

@EnableJpaRepositories:配置可用持久化类,另外我们还配置了持久化类扫描的包路径(cn.horse.chapter01.repository
@EnableTransactionManagement:配置可用事务管理

另外还将数据源(DataSource)、实体管理工厂(EntityManagerFactory)、Jpa事务管理(PlatformTransactionManager)实例注册到Spring容器中。

其中实体管理工厂(EntityManagerFactory)实例的创建由LocalContainerEntityManagerFactoryBean对象完成,LocalContainerEntityManagerFactoryBean对象创建的过程中设置了实体类扫描的包路径(cn.horse.chapter01.entity),属性中还设置了DDL方式(create),此方式会先删除表后再创建新表。

五、单元测试类

在src/test/java的cn.horse.chapter01包下创建SpringDataJpaTest测试类

package cn.horse.chapter02;

import cn.horse.chapter02.entity.User;
import cn.horse.chapter02.repository.UserRepository;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class SpringDataJpaTest {

    static final ApplicationContext applicationContext;
    static final UserRepository userRepository;
    static {
        applicationContext = new AnnotationConfigApplicationContext("cn.horse.chapter02.config");
        userRepository = applicationContext.getBean(UserRepository.class);
    }

    @Test
    public void testInsert() {
        User user = new User();
        user.setUsername("张三");
        User saveUser = userRepository.save(user);
        Assert.assertNotNull(saveUser.getId());
        Assert.assertEquals(saveUser.getId(), user.getId());
        Assert.assertEquals(user.getUsername(), saveUser.getUsername());
    }
}

六、ID生成策略

1、AUTO

修改User实体类中的属性id

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

执行单元测试:

【spring data jpa】ID生成策略(GeneratedValue)_第1张图片

可以看到创建了T_USER_SEQ序列、T_USER表;

AUTO生成策略是默认的生成策略,在没有设置strategy时使用的就是AUTO生成策略

2、TABLE

修改User实体类中的属性id

@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private Integer id;

执行单元测试:

【spring data jpa】ID生成策略(GeneratedValue)_第2张图片

可以看到创建了hibernate_sequences表、T_USER表;并且初始化了hibernate_sequences表中的默认数据;

3、SEQUENCE

修改User实体类中的属性id

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Integer id;

执行单元测试:

【spring data jpa】ID生成策略(GeneratedValue)_第3张图片

可以看到创建了T_USER_SEQ序列、T_USER表;

4、IDENTITY

修改User实体类中的属性id

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

执行单元测试:

【spring data jpa】ID生成策略(GeneratedValue)_第4张图片

可以看到在创建T_USER表时,id字段自动生成;

5、UUID

修改User实体类中的属性id

@Id
@GeneratedValue(strategy = GenerationType.UUID)
private String id;

public void setId(String id) {
    this.id = id;
}

public String getId() {
    return id;
}

执行单元测试:

【spring data jpa】ID生成策略(GeneratedValue)_第5张图片

UUID生成策略生成的值是String类型,这里我们修改了id字段的类型。

七、自定义ID生成策略

1、定义ID生成器

在src/test/java的cn.horse.chapter02.entity包下创建TimestampIdentifierGenerator类(需要实现IdentifierGenerator接口)


public class TimestampIdentifierGenerator implements IdentifierGenerator {
    @Override
    public Long generate(SharedSessionContractImplementor session, Object object) {
        return System.currentTimeMillis();
    }
}

2、修改实体类

修改User实体类中的属性id

@Id
@GenericGenerator(name = "timestampGenerator", type = TimestampIdentifierGenerator.class)
@GeneratedValue(generator = "timestampGenerator")
private Long id;

public void setId(Long id) {
    this.id = id;
}

public Long getId() {
    return id;
}

需要注意的是属性id的类型需要与ID生成器生成的ID类型一致

3、单元测试

执行单元测试:

【spring data jpa】ID生成策略(GeneratedValue)_第6张图片

单元测试执行通过

你可能感兴趣的:(#,spring,data,jpa,spring,data,jpa,java)