如何在不检查 MySQL 中的 AUTOINCREMENT 的情况下增加主键
Posted
技术标签:
【中文标题】如何在不检查 MySQL 中的 AUTOINCREMENT 的情况下增加主键【英文标题】:How to increment primary key without check AUTOINCREMENT in MySQL 【发布时间】:2012-10-05 14:52:01 【问题描述】:如何在不检查 AUTOINCREMENT 的情况下在 mysql 中增加主键?
我的例子:
Tax.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package newpackage;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.persistence.*;
import javax.transaction.UserTransaction;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
/**
*
* @author gile
*/
@Entity
@Table(name = "tax", catalog = "invoicedb", schema = "")
@XmlRootElement
@NamedQueries(
@NamedQuery(name = "Tax.findAll", query = "SELECT t FROM Tax t"),
@NamedQuery(name = "Tax.findByIdtax", query = "SELECT t FROM Tax t WHERE t.idtax = :idtax"),
@NamedQuery(name = "Tax.findByValue", query = "SELECT t FROM Tax t WHERE t.value = :value"),
**@NamedQuery(name = "Tax.findMaxID", query = "SELECT MAX (t.idtax) from Tax t"))** // I write this query to find max value of primary key. how to find max ?
public class Tax implements Serializable
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "idtax")
private Integer idtax;
@Basic(optional = false)
@NotNull
@Column(name = "value")
private BigDecimal value;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "taxid")
private List<Invoicedetails> invoicedetailsList;
public Tax()
public Tax(Integer idtax)
this.idtax = idtax;
public Tax(Integer idtax, BigDecimal value)
this.idtax = idtax;
this.value = value;
public Integer getIdtax()
return idtax;
public void setIdtax(Integer idtax)
this.idtax = idtax;
public BigDecimal getValue()
return value;
public void setValue(BigDecimal value)
this.value = value;
@XmlTransient
public List<Invoicedetails> getInvoicedetailsList()
return invoicedetailsList;
public void setInvoicedetailsList(List<Invoicedetails> invoicedetailsList)
this.invoicedetailsList = invoicedetailsList;
@Override
public int hashCode()
int hash = 0;
hash += (idtax != null ? idtax.hashCode() : 0);
return hash;
@Override
public boolean equals(Object object)
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Tax))
return false;
Tax other = (Tax) object;
if ((this.idtax == null && other.idtax != null) || (this.idtax != null && !this.idtax.equals(other.idtax)))
return false;
return true;
@Override
public String toString()
return String.format("%.2f %%",value);
@Transient
private String taxvalueString;
public String getTaxvalueString()
return String.format(" %.2f %%",value);
public void setTaxvalueString(String taxvalueString)
this.taxvalueString = taxvalueString;
【问题讨论】:
【参考方案1】:您可以在 Tax Class 中添加一个带有 PrePersit 注释的方法来创建 PK (idtax)
喜欢
@PrePersist
private void generateID()
// Read existing Tax record, finde new id and set pk here.
【讨论】:
以上是关于如何在不检查 MySQL 中的 AUTOINCREMENT 的情况下增加主键的主要内容,如果未能解决你的问题,请参考以下文章
如何在不使用对象的情况下检查 DataGridViewComboBoxColumn 中的项目?
如何在不使用 Angular 的 spyOn 的情况下检查服务中的方法是不是在 Jasmine 单元测试中被调用?
如何在不使用 Django 模型的情况下使用 MySQL 中的完整数据库
如何在不使用java发送任何邮件的情况下检查域中是不是存在电子邮件ID