【问题标题】:JpaRepository insert doesn't work [Spring Boot + MySQL]JpaRepository 插入不起作用 [Spring Boot + MySQL]
【发布时间】:2019-06-16 17:29:45
【问题描述】:

首先,我必须声明我的项目中启用了 3 个数据库。

任何关于插入数据库的方法似乎都不适用,但 select 方法可以。这很奇怪,并不是 Spring Boot 无法确定使用哪个数据库,因为同一个存储库从正确的数据库中选择但无法插入。此外,在 Java 环境中也没有在 MySQL 中出现错误(我在 application.properties 上启用了调试选项)

总而言之,save 方法不会插入数据库,但选择相同的存储库没有任何问题。我检查了我是否有插入数据库的权限,我确实这样做了(我还再次添加了它们以防万一)。 我使用相同的实体进行选择和插入。 我要访问的表名为log,数据库为db1。此外,插入适用于db3

另外,我已经为所有三个数据库配置了DataSource。 我想补充一点,三个数据库上有多个同名的表。由于其他原因,我不能给你确切的命名,但我当然会尝试任何关于命名的建议。但我不得不说,在所有三个数据库上,选择都完全按照需要进行。

application.properties

server.port=8086

db1.datasource.test-on-borrow=true
db1.datasource.validation-query=SELECT 1

db2.datasource.test-on-borrow=true
db2.datasource.validation-query=SELECT 1

db3.datasource.test-on-borrow=true
db3.datasource.validation-query=SELECT 1

spring.jpa.hibernate.ddl-auto=validate

jwt.header=Authorization
jwt.secret= //mysecret
jwt.expiration=14400
jwt.route.authentication.path=/login
jwt.route.authentication.refresh=/refresh

spring.profiles.active=prod

webapp.cors.allowedOrigins= //list of allowed origins


spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

application-prod.properties

server.port=8086

db1.datasource.url= //db1 url
db1.datasource.username= //username
db1.datasource.password= //password
db1.datasource.driverClassName=com.mysql.jdbc.Driver

db1.datasource.test-on-borrow=true
db1.datasource.validation-query=SELECT 1

db2.datasource.url= //db2 url
db2.datasource.username= //username
db2.datasource.password= //password
db2.datasource.driverClassName=com.mysql.jdbc.Driver

db2.datasource.test-on-borrow=true
db2.datasource.validation-query=SELECT 1

db3.datasource.url= //db3 url
db3.datasource.username= //username
db3.datasource.password= //password
db3.datasource.driverClassName=com.mysql.jdbc.Driver

db3.datasource.test-on-borrow=true
db3.datasource.validation-query=SELECT 1

LogJPA实体,log是数据库db1中的表名

@Entity
@Table(name = "log" , catalog = "db1")
public class Log implements Serializable{

    private static final long serialVersionUID = 1L;


    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long logID;
    private Integer uploadSeq;
    private String date;

    public Log() {
    }

    public Log(Integer uploadSeq, String date) {
        this.uploadSeq = uploadSeq;
        this.date = date;
    }


    @Column(name = "logID", unique = true, nullable = false)
    public Long getLogID() {
        return logID;
    }


    public void setLogID(Long logID) {
        this.logID = logID;
    }


    @Column(name = "uploadSeq", nullable = false)
    public Integer getUploadSeq() {
        return uploadSeq;
    }


    public void UploadSeq(Integer uploadSeq) {
        this.uploadSeq = uploadSeq;
    }


    @Column(name = "date", nullable = false)
    public String getDate() {
        return date;
    }


    public void setDate(String date) {
        this.date = date;
    }

}

Db1LogRepositorylog 表的存储库

public interface Db1LogRepository extends JpaRepository<Log,Long> {


    public Log findFirstByOrderByLogIDDesc(); //is being used on another part of the project

}

Db1LogComponent 用于访问存储库的组件

@Component
public class Db1LogComponent {

    @Autowired
    Db1logRepository db1LogRepository;


    public void addDate(Log log) {

        System.out.println(db1LogRepository.findAll().size()); //Retrieves the correct entities of the table log in db1
        db1LogRepository.save(log); //Doesn't save to the database
    }

}

编辑: DB3 在配置文件上有@Primary 注释,而关于其他两个数据库的其他两个配置则没有。

【问题讨论】:

    标签: java mysql spring spring-boot


    【解决方案1】:

    确保您使用的是@Repository 存储库顶部的@Transactional 注释,如下所示。

    @Repository
    @Transactional
    public interface Db1LogRepository extends JpaRepository<Log,Long> 
    

    【讨论】:

    • 我已经尝试了很多我必须说的注释,但你的建议似乎没有解决它。
    • 我需要添加@Transactional("db1TransactionManager")。我将您的答案标记为正确答案,因为它引导我搜索更多关于@Transactional
    • 很高兴它帮助了你。
    • 你是在哪个类中添加的,能否分享一下“db1TransactionManager”的定义,我也遇到同样的问题,无法解决
    猜你喜欢
    • 2020-04-10
    • 2019-06-16
    • 1970-01-01
    • 2020-03-09
    • 2019-02-09
    • 2016-05-14
    • 1970-01-01
    • 2018-11-14
    • 2020-08-08
    相关资源
    最近更新 更多