3

我遇到了约束违规:

run:
DEBUG: nntp: newsrc loading /home/thufir/.newsrc
DEBUG: nntp: newsrc load: 6 groups in 20ms
[EL Info]: 2012-07-29 07:07:55.308--ServerSession(11070985)--EclipseLink, version: Eclipse Persistence Services - 2.3.0.v20110604-r9504
[EL Info]: 2012-07-29 07:07:57.621--ServerSession(11070985)--file:/home/thufir/NetBeansProjects/USENET/build/classes/_USENETPU login successful
[EL Warning]: 2012-07-29 07:07:58.0--UnitOfWork(1697152)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'gwene.ca.craigslist.vancouver.labour.general' for key 'NEWSGROUP'
Exception in thread "main" javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'gwene.ca.craigslist.vancouver.labour.general' for key 'NEWSGROUP'
Error Code: 1062
Call: INSERT INTO NEWSGROUPS (NEWSGROUP) VALUES (?)
    bind => [1 parameter bound]
Query: InsertObjectQuery(net.bounceme.dur.usenet.model.Newsgroups[ id=null ])
Error Code: 1062
Call: INSERT INTO NEWSGROUPS (NEWSGROUP) VALUES (?)
    bind => [1 parameter bound]
Query: InsertObjectQuery(net.bounceme.dur.usenet.model.Newsgroups[ id=null ])
    at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commitInternal(EntityTransactionImpl.java:102)
    at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:63)
    at net.bounceme.dur.usenet.driver.NewMain.folders(NewMain.java:41)
    at net.bounceme.dur.usenet.driver.NewMain.<init>(NewMain.java:26)
    at net.bounceme.dur.usenet.driver.NewMain.main(NewMain.java:22)
Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'gwene.ca.craigslist.vancouver.labour.general' for key 'NEWSGROUP'
Error Code: 1062
Call: INSERT INTO NEWSGROUPS (NEWSGROUP) VALUES (?)
    bind => [1 parameter bound]
Query: InsertObjectQuery(net.bounceme.dur.usenet.model.Newsgroups[ id=null ])
    at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:324)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeDirectNoSelect(DatabaseAccessor.java:840)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeNoSelect(DatabaseAccessor.java:906)

我尝试包装持久性以捕获 MySQLIntegrityConstraintViolationException:

private void folders() {
    List<Folder> folders = u.getFolders();
    emf = Persistence.createEntityManagerFactory("USENETPU");
    em = emf.createEntityManager();
    for (Folder folder : folders) {
        Newsgroups n = new Newsgroups(folder);
        em.getTransaction().begin();
        try {
            em.persist(n);
        } catch (Exception e) {
            LOG.info("duplicate");
        }
        em.getTransaction().commit();
    }
    em.close();
}

我该如何处理这个异常?

实体:

package net.bounceme.dur.usenet.model;

import java.io.Serializable;
import javax.mail.Folder;
import javax.persistence.*;

@Entity
@NamedQueries({
    @NamedQuery(name = "Newsgroups.findAll", query = "SELECT n FROM Newsgroups n"),
    @NamedQuery(name = "Newsgroups.findById", query = "SELECT n FROM Newsgroups n WHERE n.id = :id")})
public class Newsgroups implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id", nullable = false)
    private Long id;
    //@Column
    @Column(unique = true)
    private String newsgroup;

    public Newsgroups() {
    }

    public Newsgroups(Folder folder) {
        newsgroup = folder.getFullName();
    }

    public Long getId() {
        return id;
    }

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

    @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 Newsgroups)) {
            return false;
        }
        Newsgroups other = (Newsgroups) 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 "net.bounceme.dur.usenet.model.Newsgroups[ id=" + id + " ]";
    }
}
4

0 回答 0