Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何防止Hibernate使id列自动递增?_Java_Mysql_Hibernate - Fatal编程技术网

Java 如何防止Hibernate使id列自动递增?

Java 如何防止Hibernate使id列自动递增?,java,mysql,hibernate,Java,Mysql,Hibernate,我希望我的ID列是一个字符串,然后在某个点(保存对象之前)自己分配ID。然而,当我将class属性设置为private String id时,hibernate忽略了这一点,并最终在MySQL中生成了一个int字段并将其设置为AI。我确保没有将GenerationType.AUTO放入类中,但没有任何效果 更奇怪的是,当我试图直接在MySQL中更改列的数据类型时,它不允许我这样做 有人有过类似的问题或有快速解决方案吗 休眠:4.3.6.4 MySQL:org.hibernate.dialogue

我希望我的ID列是一个字符串,然后在某个点(保存对象之前)自己分配ID。然而,当我将class属性设置为private String id时,hibernate忽略了这一点,并最终在MySQL中生成了一个int字段并将其设置为AI。我确保没有将GenerationType.AUTO放入类中,但没有任何效果

更奇怪的是,当我试图直接在MySQL中更改列的数据类型时,它不允许我这样做

有人有过类似的问题或有快速解决方案吗

休眠:4.3.6.4 MySQL:org.hibernate.dialogue.mysqldialogue,5.1.31连接器

package com.online_exchange.model;

import java.io.Serializable;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author re5pect
 */
@Entity
@Table(name = "transactionrequest")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Transactionrequest.findAll", query = "SELECT t FROM Transactionrequest t")
    ,
    @NamedQuery(name = "Transactionrequest.findById", query = "SELECT t FROM Transactionrequest t WHERE t.id = :id")
    ,
    @NamedQuery(name = "Transactionrequest.findByAmount", query = "SELECT t FROM Transactionrequest t WHERE t.amount = :amount")
    ,
    @NamedQuery(name = "Transactionrequest.findByRate", query = "SELECT t FROM Transactionrequest t WHERE t.rate = :rate")})
public class Transactionrequest implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @Column(name = "id")
    private String id;
    @Basic(optional = false)
    @Column(name = "amount")
    private double amount;
    @Basic(optional = false)
    @Column(name = "rate")
    private double rate;
    @JoinColumn(name = "client_id", referencedColumnName = "id")
    @OneToOne
    private User client;
    @OneToMany(mappedBy = "transactionRequest")
    private Collection<Transactionoffer> transactionofferCollection;
    @Transient
    private boolean alreadyOffered = false;

    public Transactionrequest() {
    }

    public Transactionrequest(String id) {
        this.id = id;
    }

    public Transactionrequest(String id, double amount, double rate) {
        this.id = id;
        this.amount = amount;
        this.rate = rate;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public double getAmount() {
        return amount;
    }

    public void setAmount(double amount) {
        this.amount = amount;
    }

    public double getRate() {
        return rate;
    }

    public void setRate(double rate) {
        this.rate = rate;
    }

    public User getClient() {
        return client;
    }

    public void setClient(User client) {
        this.client = client;
    }

    @XmlTransient
    public Collection<Transactionoffer> getTransactionofferCollection() {
        return transactionofferCollection;
    }

    public void setTransactionofferCollection(Collection<Transactionoffer> transactionofferCollection) {
        this.transactionofferCollection = transactionofferCollection;
    }

    public void addTransactionoffer(Transactionoffer offer) {
        this.transactionofferCollection.add(offer);
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.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 Transactionrequest)) {
            return false;
        }
        Transactionrequest other = (Transactionrequest) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.online_exchange.model.Transactionrequest[ id=" + id + " ]";
    }

    /**
     * @return the alreadyOffered
     */
    public boolean isAlreadyOffered() {
        return alreadyOffered;
    }

    /**
     * @param alreadyOffered the alreadyOffered to set
     */
    public void setAlreadyOffered(boolean alreadyOffered) {
        this.alreadyOffered = alreadyOffered;
    }

    public void purge() {
        this.setClient(null);
        this.setTransactionofferCollection(null);
    }

    public void prune() {
        this.getClient().purge();
        if(this.getTransactionofferCollection() != null){
            for(Transactionoffer offer : this.getTransactionofferCollection()){
                offer.purge();
            }
        }
    }
}

发布代码和生成的表定义,而不是描述它们@Id@Column(name=“Id”)私有字符串Id;MySQL(自动生成的脚本-假设由Hibernate实现):创建表
transactionrequest
id
int(11)NOTNULL自动递增,
amount
double NOTNULL,
rate
double NOTNULL,
client\u id
int(11)默认NULL,主键(
id
),KEY
FK_92xewutta5u8mr2ds6iqy20ga
client_id
),CONSTRAINT
FK_92xewutta5u8mr2ds6iqy20ga
外键(
client_id
)REFERENCES
app_user
id
)ENGINE=InnoDB DEFAULT CHARSET=utf8发布问题中的所有相关代码,格式正确。注释中的不小部分。使用相关类编辑的OP,以及MySQL Bit。您使用的是什么版本的Hibernate和哪种MySQL方言?
CREATE TABLE `transactionrequest` (
   `id` int(11) NOT NULL AUTO_INCREMENT,
   `amount` double NOT NULL,
   `rate` double NOT NULL,
   `client_id` int(11) DEFAULT NULL,
   PRIMARY KEY (`id`),
   KEY `FK_92xewutta5u8mr2ds6iqy20ga` (`client_id`),
   CONSTRAINT `FK_92xewutta5u8mr2ds6iqy20ga` FOREIGN KEY (`client_id`) REFERENCES `app_user` (`id`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8