hibernate 如何在Springboot应用程序中填充未使用@Id注解的Instancce变量的自动增量值

mnemlml8  于 2023-08-06  发布在  Spring
关注(0)|答案(1)|浏览(79)

这里我想为“transactionNumber”变量生成自动填充值并将其存储在数据库中,但我在另一个变量“accountNumber”上使用了@Id
注意:我正在使用MYSQL数据库
第一个月

@Entity
@SequenceGenerator(name = "seq", initialValue = 1, allocationSize = 1)
public class Accounts {
    
    @Id
    @GeneratedValue(generator = "seq")
    private Integer accountNumber;
    
    /*
     * @Retention(RetentionPolicy.RUNTIME)
     * 
     * @Target(ElementType.FIELD) public @interface InjectSequenceValue { String
     * sequencename(); }
     */
    
    /* @InjectSequenceValue(sequencename = "serialnum_sequence") */
    
    
    private int transactionNumber;
    
    @Column
    private String userId;
    
    @Column
    private int upiIdLimit;
    
    @Column
    private double accountBalance;
    
    @Column
    private String transactionLimitPerDay;
    
    
    public Integer getAccountNumber() {
        return accountNumber;
    }

    public void setAccountNumber(Integer accountNumber) {
        this.accountNumber = accountNumber;
    }

    public int getTransactionNumber() {
        return transactionNumber;
    }

    public void setTransactionNumber(int transactionNumber) {
        this.transactionNumber = transactionNumber;
    }

    public String  getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public int getUpiIdLimit() {
        return upiIdLimit;
    }

    public void setUpiIdLimit(int upiIdLimit) {
        this.upiIdLimit = upiIdLimit;
    }

    public double getAccountBalance() {
        return accountBalance;
    }

    public void setAccountBalance(double accountBalance) {
        this.accountBalance = accountBalance;
    }

    public String getTransactionLimitPerDay() {
        return transactionLimitPerDay;
    }

    public void setTransactionLimitPerDay(String transactionLimitPerDay) {
        this.transactionLimitPerDay = transactionLimitPerDay;
    }
    
}

字符串

dgenwo3n

dgenwo3n1#

MySQL表结构不能包含多个auto_increment字段。当您尝试创建具有2个自动递增字段的表或更改该表以尝试创建第二个自动递增字段时,查询将失败。

选项:

1.创建具有两个不同主键的两个表。然后,您将能够增加这两个列的值。
1.创建数据库触发器(MySQL 8.0 Reference Manual: 25.3.1 Trigger Syntax and Examples):

CREATE TRIGGER 'INCREMENT_AccountNumber' AFTER INSERT ON 'Accounts' FOR EACH ROW BEGIN UPDATE class SET AccountNumber = AccountNumber+1 WHERE ID= ID; END

字符串
1.您可以在MyISAM的多列索引中的辅助列上指定AUTO_INCREMENT(MyISAM针对读操作繁重、写操作很少或根本没有的环境进行了优化)。MySQL 8.0参考手册:3.6.9使用AUTO_INCREMENT - MyISAM Notes

相关问题