public class NonceId implements Serializable {
public static NonceId newInstance(final String udid,final String nonce) {
if (udid == null) {
throw new IllegalArgumentException("null udid");
}
if (nonce == null) {
throw new IllegalArgumentException("null nonce");
}
final NonceId instance = new NonceId();
instance.udid = udid;
instance.nonce = nonce;
return instance;
}
@Override
public int hashCode() {
int hash = 7;
hash = 23 * hash + (this.udid != null ? this.udid.hashCode() : 0);
hash = 23 * hash + (this.nonce != null ? this.nonce.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final NonceId other = (NonceId) obj;
if ((this.udid == null) ? (other.udid != null) : !this.udid.equals(other.udid)) {
return false;
}
if ((this.nonce == null) ? (other.nonce != null) : !this.nonce.equals(other.nonce)) {
return false;
}
return true;
}
@Override
public String toString() {
return udid + "/" + nonce;
}
private String udid;
private String nonce;
}
实体类.
@Entity
@IdClass(NonceId.class)
@Table(name = "NONCE2")
@XmlTransient
public class Nonce2 implements Serializable {
public static final int UDID_SIZE_MIN = 1;
public static final int UDID_SIZE_MAX = 255;
public static final int NONCE_SIZE_MIN = 1;
public static final int NONCE_SIZE_MAX = 255;
public static Nonce2 newInstance(final String udid,final String nonce) {
if (nonce == null) {
throw new NullPointerException("null value");
}
final Nonce2 instance = new Nonce2();
instance.udid = udid;
instance.nonce = nonce;
return instance;
}
public Date getCreatedDate() {
return createdDate;
}
public String getUdid() {
return udid;
}
public String getNonce() {
return nonce;
}
@PrePersist
protected void _PrePersist() {
createdDate = new Date();
}
@Override
public String toString() {
return udid + "/" + nonce;
}
@Column(name = "CREATED_DATE",nullable = false,updatable = false)
@Temporal(TemporalType.TIMESTAMP)
@NotNull
private Date createdDate;
@Id
@Column(name = "UDID",updatable = false)
@NotNull
@Size(min = UDID_SIZE_MIN,max = UDID_SIZE_MAX)
private String udid;
@Id
@Column(name = "NONCE",updatable = false)
@NotNull
@Size(min = NONCE_SIZE_MIN,max = NONCE_SIZE_MAX)
private String nonce;
}
在我的过滤器中我做到了
@WebFilter(urlPatterns = {"/*"})
public class Filter_ implements Filter {
@Override
public void doFilter(final ServletRequest request,final ServletResponse response,final FilterChain chain)
throws IOException,ServletException {
// check whether nonce is already exist via em.find();
chain.doFilter(request,response);
// store nonce via em.persist(); // EXCEPTION IS HERE
// THERE IS NO SUCH RECORD check direct SQL console.
}
}
我的JPA提供程序似乎执行此语句.
INSERT INTO NONCE2 (NONCE,?)
最佳答案
您在表上定义了复合主键.
可能是您尝试在同一会话期间批量插入记录.并且批处理可能包含所述键列的重复条目.请检查一下.
另外,请发布带有示例数据的插入代码.
(编辑:晋中站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|