为什么 Hibernate 尝试更改我的主键和外键会出现错误

问题描述 投票:0回答:1

Hibernate 正在尝试更改我的主键,但我不想更新,任何人都可以帮助我解决我做错的事情。

我试图与我的数据库进行交互,以获取与用户提供的给定密钥匹配的所有行,并将该信息发送回客户端,收到后客户端应发送带有另一个密钥的发布请求,我将使用该密钥更新投票表并候选人表中的 vote_count 列;

my controller

package com.onlinevoting.leadiconbackend.Controller;

import com.onlinevoting.leadiconbackend.Entity.Candidate;
import com.onlinevoting.leadiconbackend.Services.CandidateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/candidates")
public class CandidateController {

    @Autowired
    private CandidateService candidateService;

    // Endpoint to get candidates by constituency ID
    @GetMapping("/{constituencyId}")
    public List<Candidate> getCandidatesByConstituency(@PathVariable String constituencyId) {
        return candidateService.getCandidatesByConstituency(constituencyId);
    }
}


my entities
@Entity
@Table(name = "candidates")
@JsonIgnoreProperties({"constituency.state.constituencies"})
public class Candidate {
    @Id
    @Column(name="candidate_id")
    @JsonProperty("candidateId")
    private String candidateId;
    @Column(name="candidate_name")
    @JsonProperty("candidateName")
    private String candidateName;

    @Column(name="constituency_id")
    @JsonProperty("constituencyId")
    private String constituencyId;

}

@Entity
@Table(name = "constituencies")
public class Constituency {

    @Id
    @Column(name = "constituency_id")
    private String constituencyId;

    @Column(name = "constituency_name")
    private String constituencyName;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "state_id")
    private State state;
}

@Entity
@Table(name = "votes")
public class Vote {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "vote_id")
    private Long voteId;

    @Column(name = "voter_ip")
    private String voterIp;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "candidate_id")
    private Candidate candidate;

}

错误

plementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
Hibernate: 
    alter table candidates
       modify column constituency_id  varchar(255)
2023-09-11T17:44:16.566+05:30  WARN 14708 --- [           main] o.h.t.s.i.ExceptionHandlerLoggedImpl     : GenerationTarget encountered exception accepting command : Error executing DDL "
    alter table candidates
       modify column constituency_id  varchar(255)" via JDBC [Cannot change column 'constituency_id': used in a foreign key constraint 'candidates_ibfk_1']

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "
    alter table candidates
       modify column constituency_id  varchar(255)" via JDBC [Cannot change column 'constituency_id': used in a foreign key constraint 'candidates_ibfk_1']
   
java spring spring-boot hibernate jdbc
1个回答
0
投票

Hibernate 尝试构建实体中定义的数据库。

尝试将此环境属性设置为

validate
spring.jpa.hibernate.ddl-auto

文档:https://docs.spring.io/spring-boot/docs/1.1.0.M1/reference/html/howto-database-initialization.html

© www.soinside.com 2019 - 2024. All rights reserved.