使用spring data jpa保存字段丢失结果

zpjtge22  于 2023-04-30  发布在  Spring
关注(0)|答案(1)|浏览(122)

描述

我有一个使用spring data jpa的SpringBoot应用程序。

id 'org.springframework.boot' version '2.7.11'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'

java version: openjdk version "1.8.0_362"
mysql version: 8.0

我的想法很简单,我有一些结构相同的表(除了不同的表名)。以后可能会添加更多的表(我不想重复编码),所以我想直接根据类名插入其对应的表。但我测试发现,当我使用repository.save()时,jpa只为我插入了id,我设置的其他两个字段没有写入。这让我很困惑。下面是我的代码。

代码

package com.example.demo;

import com.example.demo.dto.BaseClass;
import com.example.demo.dto.Other;
import com.example.demo.dto.User;
import org.springframework.beans.BeansException;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.data.repository.CrudRepository;

import java.util.HashMap;
import java.util.Map;

@SpringBootApplication
public class DemoApplication implements ApplicationContextAware {
    static Map<String, Class<? extends BaseClass>> CLAZZ_MAP = new HashMap<>();

    static {
        CLAZZ_MAP.put("user", User.class);
        CLAZZ_MAP.put("other", Other.class);
    }

    ApplicationContext context;

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

    @Bean
    public CommandLineRunner runner() {
        return args -> {
            Map<String, CrudRepository> repositoryMap = context.getBeansOfType(CrudRepository.class);
            repositoryMap.forEach((k, v) -> {
                Class<? extends BaseClass> clazz = CLAZZ_MAP.get(k.substring(0, k.indexOf("Repository")));
                try {
                    BaseClass baseClass = clazz.newInstance();
                    baseClass.setOther("a");
                    baseClass.setPhone("b");
                    v.save(baseClass);
                } catch (IllegalAccessException | InstantiationException e) {
                    throw new RuntimeException(e);
                }
            });
        };
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }

}
package com.example.demo.dto;

import javax.persistence.Column;

public class BaseClass {
    @Column
    String phone;
    @Column
    String other;

    // ignore getter and setter
}

@Entity
@Table(name = "other")
public class Other extends BaseClass{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Integer id;

    // ignore getter and setter
}

@Entity
@Table(name = "user")
public class User extends BaseClass{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Integer id;

    // ignore getter and setter 
}
package com.example.demo.repository;
import org.springframework.data.repository.CrudRepository;

public interface BaseRepository {
}

public interface OtherRepository extends CrudRepository<Other, Integer>, BaseRepository{

}

public interface UserRepository extends CrudRepository<User, Integer>, BaseRepository {

}

数据库查询结果

> select * from user;
id phone other
1  ,    , 

> select * from other;
id phone other
1  ,    ,

那么我该怎么做才能找回我丢失的那些字段呢?我覆盖了子类中的phone和其他字段(user和other),然后使用保存,它们都保存成功。这让我更加困惑。

相关问题