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

PK (idtax) を作成するための PrePersit アノテーションを使用して、税クラスにメソッドを追加できます。

お気に入り

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