我有以下表格:
其中idclient
为unsigned
auto_increment
。
客户实体的代码:
import java.io.Serializable;
import java.util.List;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
@Entity
@Table(name = "CLIENT", catalog = "TEST", schema = "PUBLIC")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Client.findAll", query = "SELECT c FROM Client c"),
@NamedQuery(name = "Client.findByIdclient", query = "SELECT c FROM Client c WHERE c.idclient = :idclient"),
@NamedQuery(name = "Client.findByLibel", query = "SELECT c FROM Client c WHERE c.libel = :libel"),
@NamedQuery(name = "Client.findByAdresse", query = "SELECT c FROM Client c WHERE c.adresse = :adresse"),
@NamedQuery(name = "Client.findByNomResp", query = "SELECT c FROM Client c WHERE c.nomResp = :nomResp"),
@NamedQuery(name = "Client.findByTelPortable", query = "SELECT c FROM Client c WHERE c.telPortable = :telPortable"),
@NamedQuery(name = "Client.findByTelFixe", query = "SELECT c FROM Client c WHERE c.telFixe = :telFixe"),
@NamedQuery(name = "Client.findByFax", query = "SELECT c FROM Client c WHERE c.fax = :fax"),
@NamedQuery(name = "Client.findByCodeTva", query = "SELECT c FROM Client c WHERE c.codeTva = :codeTva"),
@NamedQuery(name = "Client.findByCodeExo", query = "SELECT c FROM Client c WHERE c.codeExo = :codeExo"),
@NamedQuery(name = "Client.findByBanque", query = "SELECT c FROM Client c WHERE c.banque = :banque"),
@NamedQuery(name = "Client.findByRib", query = "SELECT c FROM Client c WHERE c.rib = :rib"),
@NamedQuery(name = "Client.findByCredit", query = "SELECT c FROM Client c WHERE c.credit = :credit"),
@NamedQuery(name = "Client.findByEchance", query = "SELECT c FROM Client c WHERE c.echance = :echance"),
@NamedQuery(name = "Client.findByMail", query = "SELECT c FROM Client c WHERE c.mail = :mail"),
@NamedQuery(name = "Client.findByEtat", query = "SELECT c FROM Client c WHERE c.etat = :etat")})
public class Client implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "IDCLIENT", nullable = false)
private Integer idclient;
@Basic(optional = false)
@Column(name = "LIBEL", nullable = false, length = 100)
private String libel;
@Basic(optional = false)
@Column(name = "ADRESSE", nullable = false, length = 100)
private String adresse;
@Basic(optional = false)
@Column(name = "NOM_RESP", nullable = false, length = 60)
private String nomResp;
@Basic(optional = false)
@Column(name = "TEL_PORTABLE", nullable = false, length = 16)
private String telPortable;
@Basic(optional = false)
@Column(name = "TEL_FIXE", nullable = false, length = 16)
private String telFixe;
@Basic(optional = false)
@Column(name = "FAX", nullable = false, length = 16)
private String fax;
@Basic(optional = false)
@Column(name = "CODE_TVA", nullable = false, length = 30)
private String codeTva;
@Basic(optional = false)
@Column(name = "CODE_EXO", nullable = false, length = 30)
private String codeExo;
@Basic(optional = false)
@Column(name = "BANQUE", nullable = false, length = 60)
private String banque;
@Basic(optional = false)
@Column(name = "RIB", nullable = false, length = 22)
private String rib;
@Basic(optional = false)
@Column(name = "CREDIT", nullable = false)
private double credit;
@Basic(optional = false)
@Column(name = "ECHANCE", nullable = false)
private int echance;
@Basic(optional = false)
@Column(name = "MAIL", nullable = false, length = 70)
private String mail;
@Basic(optional = false)
@Column(name = "ETAT", nullable = false)
private char etat;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "clientIdclient")
private List<Facture> factureList;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "clientIdclient")
private List<FactProforma> factProformaList;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "clientIdclient")
private List<Bl> blList;
public Client() {
}
public Client(Integer idclient) {
this.idclient = idclient;
}
public Client(Integer idclient, String libel, String adresse, String nomResp, String telPortable, String telFixe, String fax, String codeTva, String codeExo, String banque, String rib, double credit, int echance, String mail, char etat) {
this.idclient = idclient;
this.libel = libel;
this.adresse = adresse;
this.nomResp = nomResp;
this.telPortable = telPortable;
this.telFixe = telFixe;
this.fax = fax;
this.codeTva = codeTva;
this.codeExo = codeExo;
this.banque = banque;
this.rib = rib;
this.credit = credit;
this.echance = echance;
this.mail = mail;
this.etat = etat;
}
public Integer getIdclient() {
return idclient;
}
public void setIdclient(Integer idclient) {
this.idclient = idclient;
}
public String getLibel() {
return libel;
}
public void setLibel(String libel) {
this.libel = libel;
}
public String getAdresse() {
return adresse;
}
public void setAdresse(String adresse) {
this.adresse = adresse;
}
public String getNomResp() {
return nomResp;
}
public void setNomResp(String nomResp) {
this.nomResp = nomResp;
}
public String getTelPortable() {
return telPortable;
}
public void setTelPortable(String telPortable) {
this.telPortable = telPortable;
}
public String getTelFixe() {
return telFixe;
}
public void setTelFixe(String telFixe) {
this.telFixe = telFixe;
}
public String getFax() {
return fax;
}
public void setFax(String fax) {
this.fax = fax;
}
public String getCodeTva() {
return codeTva;
}
public void setCodeTva(String codeTva) {
this.codeTva = codeTva;
}
public String getCodeExo() {
return codeExo;
}
public void setCodeExo(String codeExo) {
this.codeExo = codeExo;
}
public String getBanque() {
return banque;
}
public void setBanque(String banque) {
this.banque = banque;
}
public String getRib() {
return rib;
}
public void setRib(String rib) {
this.rib = rib;
}
public double getCredit() {
return credit;
}
public void setCredit(double credit) {
this.credit = credit;
}
public int getEchance() {
return echance;
}
public void setEchance(int echance) {
this.echance = echance;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
public char getEtat() {
return etat;
}
public void setEtat(char etat) {
this.etat = etat;
}
@XmlTransient
public List<Facture> getFactureList() {
return factureList;
}
public void setFactureList(List<Facture> factureList) {
this.factureList = factureList;
}
@XmlTransient
public List<FactProforma> getFactProformaList() {
return factProformaList;
}
public void setFactProformaList(List<FactProforma> factProformaList) {
this.factProformaList = factProformaList;
}
@XmlTransient
public List<Bl> getBlList() {
return blList;
}
public void setBlList(List<Bl> blList) {
this.blList = blList;
}
@Override
public int hashCode() {
int hash = 0;
hash += (idclient != null ? idclient.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 Client)) {
return false;
}
Client other = (Client) object;
if ((this.idclient == null && other.idclient != null) || (this.idclient != null && !this.idclient.equals(other.idclient))) {
return false;
}
return true;
}
@Override
public String toString() {
return "glob.entitys.Client[ idclient=" + idclient + " ]";
}
}
当我尝试在数据库中插入一行时:
Utilisateur user=new Utilisateur(loginActuel);
Client client=new Client(0);// the error comes from here
Facture fact=new Facture(null,new Date());
fact.setClientIdclient(client);
fact.setUtilisateurLogin(user);
FactureJpaController fjc=new FactureJpaController(emf);
fjc.create(fact);
我得到了这个丑陋的错误(但是当我设置new Client(1)
时效果很好):
Exception in thread "AWT-EventQueue-0" javax.persistence.RollbackException: Exception [EclipseLink-7197] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Null or zero primary key encountered in unit of work clone [glob.entitys.Client[ idclient=0 ]], primary key [0]. Set descriptors IdValidation or the "eclipselink.id-validation" property.
如何解决这个问题?
备注:客户端idclient = 0已插入数据库(但手动)
我想一劳永逸地克服这个“问题”,如何防止JPA或H2数据库从0
开始?
答案 0 :(得分:2)
H2允许使用0
作为主键。错误消息不是来自H2。
但是,在我看来,EclipseLink的某些(较旧的?)版本不允许使用0
。
客户端idclient = 0已插入数据库
此版本的EclipseLink似乎不支持此功能。看起来要解决此问题,您不应使用值0
。
答案 1 :(得分:2)
如何在Eclipselink中的主键中允许零:
persistence.xml
中的参数:
<property name="eclipselink.id-validation" value="NULL"/>
有关实体类的 PrimaryKey
注释:
@PrimaryKey(validation = IdValidation.NULL)
答案 2 :(得分:1)
对于具有(或负)id值的JPA(规范2.0)是好的。并且也是H2的主键值。
较早版本的EclipseLink确实将值0或更小视为无效主键。请参阅以下示例:Bug 249948。因此更新EclipseLink可以提供帮助。
顺便说一下,为什么要为应该生成的idclient设置构造函数值?
答案 3 :(得分:1)
我遇到了这个错误,并将以下注释添加到我的jpa身份中解决了它:
@Column(name = "ID_SEARCH_LOG", nullable = false, insertable = true, updatable = true, length = 10, precision = 0)
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int idSearchLog;
答案 4 :(得分:1)
文档说
默认情况下,对于不能为null的原始类型(例如int和long),EclipseLink将零解释为null,从而导致零成为主键的无效值。
但也可以在persistence.xml
或特定实体中更改此行为。