2

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

我的例子:

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;
    }
}
4

1 回答 1

0

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

喜欢

@PrePersist
private void generateID() {
   // Read existing  Tax record, finde new id and set pk here. 
}
于 2012-10-15T15:38:17.000 回答