问题:每次我运行程序时,由于重复的实体而引发异常。我必须抓住它还是有更好的方法来处理它?与hashcode- && equal-method有关系吗?

这是我的persistence.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">

<persistence-unit name="PersistenceUnit">
    <!--<provider>org.hibernate.ejb.HibernatePersistence</provider>-->

<class>entities.Benutzer</class>

    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>

        <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/dbwebanw_sose15_03"/>
        <property name="hibernate.connection.username" value="root"/>
        <property name="hibernate.connection.password" value="admin"/>

        <property name="hibernate.hbm2ddl.auto" value="update"/>


    </properties>

</persistence-unit>




这是我的实体类:

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

@Entity
@Table(name ="benutzer", catalog = "dbwebanw_sose15_03")
@SecondaryTable(name="benutzerdaten",
    pkJoinColumns = @PrimaryKeyJoinColumn(name = "benutzer_id"))
public class Benutzer implements Serializable {

    private static final long serialVersionUID = 986052564986612458L;
    private int id;
    private String name;
    private String email;
    private String passwort;
    private byte istAdministrator;

    public Benutzer() {
    }

    public Benutzer(String name, String email, String passwort, byte istAdministrator) {
        this.name = name;
        this.email = email;
        this.passwort = passwort;
        this.istAdministrator = istAdministrator;
    }

    @Id
    @GeneratedValue
    @Column(name = "benutzer_id")
    public int getId() {
        return id;
    }

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

    @Basic
    @Column(table="benutzerdaten",name = "name")
    public String getName() {
        return name;
    }

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

    @Basic
    @Column(name="email", unique = true)
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Basic
    @Column(table="benutzerdaten",name = "passwort")
    public String getPasswort() {
        return passwort;
    }

    public void setPasswort(String passwort) {
        this.passwort = passwort;
    }

    @Basic
    @Column(table="benutzerdaten")
    public byte getIstAdministrator() {
        return istAdministrator;
    }

    public void setIstAdministrator(byte istAdministator) {
        this.istAdministrator = istAdministator;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Benutzer that = (Benutzer) o;

        if (id != that.id) return false;
        if (istAdministrator != that.istAdministrator) return false;
        if (name != null ? !name.equals(that.name) : that.name != null) return false;
        if (email != null ? !email.equals(that.email) : that.email != null) return false;
        if (passwort != null ? !passwort.equals(that.passwort) : that.passwort != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (email != null ? email.hashCode() : 0);
        result = 31 * result + (passwort != null ? passwort.hashCode() : 0);
        result = 31 * result + (int) istAdministrator;
        return result;
    }
}


这是我的主要班级:

import javax.persistence.*;
import entities.Benutzer;

public class Main {

    public static void main( String[ ] args ) {

        EntityManagerFactory emfactory =     Persistence.createEntityManagerFactory("PersistenceUnit");

        EntityManager entitymanager = emfactory.createEntityManager();
        entitymanager.getTransaction( ).begin( );

        Benutzer benutzer = new Benutzer("Mahmut Demir", "[email protected]", "meric",(byte)1);

        entitymanager.persist(benutzer);

        entitymanager.getTransaction( ).commit( );

        entitymanager.close( );
        emfactory.close();
    }
}

最佳答案

email是映射中的唯一列,但是您始终插入同一封电子邮件。

09-11 16:34