如何在不检查MySQL中的AUTO_INCREMENT的情况下增加主键

时间:2012-10-15 15:27:09

标签: mysql jpa auto-increment

如何在不检查MySQL中的AUTO_INCREMENT的情况下增加主键?

我的例子:

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 个答案:

答案 0 :(得分:0)

您可以在Tax Class中使用PrePersit注释添加方法来创建PK(idtax)

@PrePersist
private void generateID() {
   // Read existing  Tax record, finde new id and set pk here. 
}