插入实体时发生约束违规

时间:2017-08-29 14:58:59

标签: hibernate mapping unique-constraint

我遇到了违反约束的行为,如下所示:

MySQLIntegrityConstraintViolationException: Duplicate entry '1' for key 'PRIMARY'

我的java课程是:

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
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.ManyToOne;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;


@Entity    
@Table(name = "channel")
public class Channel
{
    //@GeneratedValue(generator="gen")
    //@GenericGenerator(name="gen", strategy="foreign", parameters=@Parameter(name="property", value="employee"))
    @Id
    @GeneratedValue(generator="channel_gen")
    @GenericGenerator(name="channel_gen", strategy="increment")
    @Column(name = "channel_id", unique=true, nullable=false)
    @JsonIgnore
    private Long id;

    @JsonProperty(value = "name")
    @Column(name = "name")
    private String name;

    @JsonProperty(value = "start_freq")
    @Column(name = "start_freq")
    private int startFreq;

    @JsonProperty(value = "end_freq")
    @Column(name = "end_freq")
    private int endFreq;

    @JsonProperty(value = "center")
    @Column(name = "center")
    private int center;

    @JsonProperty(value = "bandwidth")
    @Column(name = "bandwidth")
    private int bandwidth;

    @ManyToOne
    @JoinColumn(name = "ID", nullable = false)
    private Report report;

    public Channel() {

    }

    public Long getId() {
        return id;
    }

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

    public Report getReport() {
        return report;
    }

    public void setReport(Report report) {
        this.report = report;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getStartFreq() {
        return startFreq;
    }

    public void setStartFreq(int startFreq) {
        this.startFreq = startFreq;
    }

    public int getEndFreq() {
        return endFreq;
    }

    public void setEndFreq(int endFreq) {
        this.endFreq = endFreq;
    }

    public int getCenter() {
        return center;
    }

    public void setCenter(int center) {
        this.center = center;
    }

    public int getBandwidth() {
        return bandwidth;
    }

    public void setBandwidth(int bandwidth) {
        this.bandwidth = bandwidth;
    }   
}

另一个java类

import com.dgs.reports.utils.States;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;

@Entity    
@Table(name = "report") 
public class Report
{
    @Id
    @GenericGenerator(name="report_gen" , strategy="increment")
    @GeneratedValue(generator="report_gen")
    @Column(name = "ID")
    @JsonIgnore
    private Long id;

@JsonIgnore
@Column(name = "state")
@Enumerated(EnumType.STRING)
private States state;

@JsonProperty(value = "keyspace")
@Column(name = "keyspace")
private String keyspace;

@JsonProperty(value = "quantum")
@Column(name = "quantum")
private int quantum;

@JsonProperty(value = "quantum_string")
@Column(name = "quantum_string")
private String quantumString;

@JsonProperty("report_type")
@Column(name = "report_type")
private String reportType;

@JsonProperty("start_date")
@Column(name = "start_date")
private long startDate;

@JsonProperty("end_date")
@Column(name = "end_date")
private long endDate;

@JsonProperty("channels")
@OneToMany(cascade = CascadeType.ALL, mappedBy = "report")
private List<Channel> channels;

@JsonProperty("start_freq")
@Column(name = "start_freq")
private int startFreq;

@JsonProperty("start_freq_unit")
@Column(name = "start_freq_unit")
private String startFreqUnit;

@JsonProperty("end_freq")
@Column(name = "end_freq")
private int endFreq;

@JsonProperty("end_freq_unit")
@Column(name = "end_freq_unit")
private String endFreqUnit;

@JsonProperty("power")
@Column(name = "power")
private int power;



public Long getId() {
    return id;
}

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

public String getKeyspace() {
    return keyspace;
}

public void setKeyspace(String keyspace) {
    this.keyspace = keyspace;
}

public int getQuantum() {
    return quantum;
}

public void setQuantum(int quantum) {
    this.quantum = quantum;
}

public String getQuantumString() {
    return quantumString;
}

public void setQuantumString(String quantumString) {
    this.quantumString = quantumString;
}

public String getReportType() {
    return reportType;
}

public void setReportType(String reportType) {
    this.reportType = reportType;
}

public long getStartDate() {
    return startDate;
}

public void setStartDate(long startDate) {
    this.startDate = startDate;
}

public long getEndDate() {
    return endDate;
}

public void setEndDate(long endDate) {
    this.endDate = endDate;
}

public List<Channel> getChannels() {
    return channels;
}

public void setChannels(List<Channel> channels) {
    this.channels = channels;
}

public int getStartFreq() {
    return startFreq;
}

public void setStartFreq(int startFreq) {
    this.startFreq = startFreq;
}

public String getStartFreqUnit() {
    return startFreqUnit;
}

public void setStartFreqUnit(String startFreqUnit) {
    this.startFreqUnit = startFreqUnit;
}

public int getEndFreq() {
    return endFreq;
}

public void setEndFreq(int endFreq) {
    this.endFreq = endFreq;
}

public String getEndFreqUnit() {
    return endFreqUnit;
}

public void setEndFreqUnit(String endFreqUnit) {
    this.endFreqUnit = endFreqUnit;
}

public int getPower() {
    return power;
}

public void setPower(int power) {
    this.power = power;
}

public States getState() {
    return state;
}

public void setState(States state) {
    this.state = state;
}

}

在DB上插入时,我在Catalina日志文件中得到了这个: enter image description here

我的桌子结构是: enter image description here

enter image description here

所以问题是,似乎自动增量功能似乎不起作用,因为它总是为通道实体分配相同的值...我缺少什么?。

0 个答案:

没有答案