(应使用insert =" false" update =" false")映射

时间:2017-06-25 02:13:43

标签: java hibernate intellij-idea

我知道这个问题经常出现在这里,但解决方案要么不是我理解的,要么就是没有帮助。我有一个"牧师"实体,基本上是一名教师。然后我们有参与者,或者学生。在参与者中,我想存储他们被分配到的牧师的电子邮件地址。他们应该只在这个领域有一个电子邮件地址。这是相关代码:

我收到此错误消息:

  

HTTP状态500 - 请求处理失败;嵌套异常是   org.hibernate.MappingException:实体映射中的重复列:   com.grandcircus.spring.models.ChaplainsEntity专栏:电子邮件(应该是   用insert =" false"映射更新="假&#34)

以下是有问题的代码:

CHAPLAINENTITY.JAVA

package com.grandcircus.spring.models;

import javax.persistence.*;
import java.util.Collection;

/**
 * Class description
 *
 * @author Sarah Guarino
 * @version 1.0
 */
@Entity
@Table(name = "chaplains", schema = "firststepfreedom", catalog = "")
public class ChaplainsEntity {
    private String id;
    private String firstname;
    private String lastname;
    private String email;
    private String phonenumber;
    private String addresslineone;
    private String addresslinetwo;
    private String addresscountry;
    private String addressstate;
    private String addresscounty;
    private String addresscity;
    private String addresszip;
    private int deprecated;
    private int activated;
    private String passcode;
    private Collection<ClassesEntity> classesById;
    private ParticipantsEntity participantsByEmail;

    @Id
    @Column(name = "id", nullable = false, length = 10)
    public String getId() {
        return id;
    }

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

    @Basic
    @Column(name = "firstname", nullable = false, length = -1)
    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    @Basic
    @Column(name = "lastname", nullable = false, length = -1)
    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    @Basic
    @Column(name = "email", nullable = false, length = 60)
    public String getEmail() {
        return email;
    }

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

    @Basic
    @Column(name = "phonenumber", nullable = false, length = 25)
    public String getPhonenumber() {
        return phonenumber;
    }

    public void setPhonenumber(String phonenumber) {
        this.phonenumber = phonenumber;
    }

    @Basic
    @Column(name = "addresslineone", nullable = false, length = -1)
    public String getAddresslineone() {
        return addresslineone;
    }

    public void setAddresslineone(String addresslineone) {
        this.addresslineone = addresslineone;
    }

    @Basic
    @Column(name = "addresslinetwo", nullable = false, length = -1)
    public String getAddresslinetwo() {
        return addresslinetwo;
    }

    public void setAddresslinetwo(String addresslinetwo) {
        this.addresslinetwo = addresslinetwo;
    }

    @Basic
    @Column(name = "addresscountry", nullable = false, length = -1)
    public String getAddresscountry() {
        return addresscountry;
    }

    public void setAddresscountry(String addresscountry) {
        this.addresscountry = addresscountry;
    }

    @Basic
    @Column(name = "addressstate", nullable = false, length = 2)
    public String getAddressstate() {
        return addressstate;
    }

    public void setAddressstate(String addressstate) {
        this.addressstate = addressstate;
    }

    @Basic
    @Column(name = "addresscounty", nullable = false, length = -1)
    public String getAddresscounty() {
        return addresscounty;
    }

    public void setAddresscounty(String addresscounty) {
        this.addresscounty = addresscounty;
    }

    @Basic
    @Column(name = "addresscity", nullable = false, length = -1)
    public String getAddresscity() {
        return addresscity;
    }

    public void setAddresscity(String addresscity) {
        this.addresscity = addresscity;
    }

    @Basic
    @Column(name = "addresszip", nullable = false, length = 10)
    public String getAddresszip() {
        return addresszip;
    }

    public void setAddresszip(String addresszip) {
        this.addresszip = addresszip;
    }

    @Basic
    @Column(name = "deprecated", nullable = false)
    public int getDeprecated() {
        return deprecated;
    }

    public void setDeprecated(int deprecated) {
        this.deprecated = deprecated;
    }

    @Basic
    @Column(name = "activated", nullable = false)
    public int getActivated() {
        return activated;
    }

    public void setActivated(int activated) {
        this.activated = activated;
    }

    @Basic
    @Column(name = "passcode", nullable = false, length = -1)
    public String getPasscode() {
        return passcode;
    }

    public void setPasscode(String passcode) {
        this.passcode = passcode;
    }

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

        ChaplainsEntity that = (ChaplainsEntity) o;

        if (phonenumber != that.phonenumber) return false;
        if (deprecated != that.deprecated) return false;
        if (activated != that.activated) return false;
        if (id != null ? !id.equals(that.id) : that.id != null) return false;
        if (firstname != null ? !firstname.equals(that.firstname) : that.firstname != null) return false;
        if (lastname != null ? !lastname.equals(that.lastname) : that.lastname != null) return false;
        if (email != null ? !email.equals(that.email) : that.email != null) return false;
        if (addresslineone != null ? !addresslineone.equals(that.addresslineone) : that.addresslineone != null)
            return false;
        if (addresslinetwo != null ? !addresslinetwo.equals(that.addresslinetwo) : that.addresslinetwo != null)
            return false;
        if (addresscountry != null ? !addresscountry.equals(that.addresscountry) : that.addresscountry != null)
            return false;
        if (addressstate != null ? !addressstate.equals(that.addressstate) : that.addressstate != null) return false;
        if (addresscounty != null ? !addresscounty.equals(that.addresscounty) : that.addresscounty != null)
            return false;
        if (addresscity != null ? !addresscity.equals(that.addresscity) : that.addresscity != null) return false;
        if (addresszip != null ? !addresszip.equals(that.addresszip) : that.addresszip != null) return false;
        if (passcode != null ? !passcode.equals(that.passcode) : that.passcode != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id != null ? id.hashCode() : 0;
        result = 31 * result + (firstname != null ? firstname.hashCode() : 0);
        result = 31 * result + (lastname != null ? lastname.hashCode() : 0);
        result = 31 * result + (email != null ? email.hashCode() : 0);
        result = 31 * result + (phonenumber != null ? addresslineone.hashCode() : 0);
        result = 31 * result + (addresslineone != null ? addresslineone.hashCode() : 0);
        result = 31 * result + (addresslinetwo != null ? addresslinetwo.hashCode() : 0);
        result = 31 * result + (addresscountry != null ? addresscountry.hashCode() : 0);
        result = 31 * result + (addressstate != null ? addressstate.hashCode() : 0);
        result = 31 * result + (addresscounty != null ? addresscounty.hashCode() : 0);
        result = 31 * result + (addresscity != null ? addresscity.hashCode() : 0);
        result = 31 * result + (addresszip != null ? addresszip.hashCode() : 0);
        result = 31 * result + deprecated;
        result = 31 * result + activated;
        result = 31 * result + (passcode != null ? passcode.hashCode() : 0);
        return result;
    }

    @OneToMany(mappedBy = "chaplainsByChaplainsid")
    public Collection<ClassesEntity> getClassesById() {
        return classesById;
    }

    public void setClassesById(Collection<ClassesEntity> classesById) {
        this.classesById = classesById;
    }

    @ManyToOne
    @JoinColumn(name = "email", referencedColumnName = "chaplainemail", nullable = false)
    public ParticipantsEntity getParticipantsByEmail() {
        return participantsByEmail;
    }

    public void setParticipantsByEmail(ParticipantsEntity participantsByEmail) {
        this.participantsByEmail = participantsByEmail;
    }
}

PARTICIPANTENTITY.JAVA

package com.grandcircus.spring.models;

import javax.persistence.*;
import java.sql.Date;
import java.util.Collection;

/**
 * Class description
 *
 * @author Sarah Guarino
 * @version 1.0
 */
@Entity
@Table(name = "participants", schema = "firststepfreedom", catalog = "")
public class ParticipantsEntity {
    private String firstname;
    private String lastname;
    private String email;
    private String phonenumber;
    private String casenum;
    private int weekscompleted;
    private int weeksneeded;
    private String offensetype;
    private String addresscountry;
    private String addressstate;
    private String addresscounty;
    private String addresscity;
    private String addresszip;
    private int deprecated;
    private int id;
    private String chaplainemail;
    private String addresslinetwo;
    private String passcode;
    private String addresslineone;
    private int activated;
    private Date sentencestart;
    private Date sentenceend;
    private Collection<AppointmentsEntity> appointmentsById;
    private Collection<ChaplainsEntity> participantsByEmail;

    @Basic
    @Column(name = "firstname", nullable = false, length = -1)
    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    @Basic
    @Column(name = "lastname", nullable = false, length = -1)
    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    @Basic
    @Column(name = "email", nullable = false, length = 60)
    public String getEmail() {
        return email;
    }

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

    @Basic
    @Column(name = "phonenumber", nullable = false, length = 25)
    public String getPhonenumber() {
        return phonenumber;
    }

    public void setPhonenumber(String phonenumber) {
        this.phonenumber = phonenumber;
    }

    @Basic
    @Column(name = "casenum", nullable = false, length = 45)
    public String getCasenum() {
        return casenum;
    }

    public void setCasenum(String casenum) {
        this.casenum = casenum;
    }

    @Basic
    @Column(name = "weekscompleted", nullable = false)
    public int getWeekscompleted() {
        return weekscompleted;
    }

    public void setWeekscompleted(int weekscompleted) {
        this.weekscompleted = weekscompleted;
    }

    @Basic
    @Column(name = "weeksneeded", nullable = false)
    public int getWeeksneeded() {
        return weeksneeded;
    }

    public void setWeeksneeded(int weeksneeded) {
        this.weeksneeded = weeksneeded;
    }

    @Basic
    @Column(name = "offensetype", nullable = true, length = -1)
    public String getOffensetype() {
        return offensetype;
    }

    public void setOffensetype(String offensetype) {
        this.offensetype = offensetype;
    }

    @Basic
    @Column(name = "addresscountry", nullable = false, length = -1)
    public String getAddresscountry() {
        return addresscountry;
    }

    public void setAddresscountry(String addresscountry) {
        this.addresscountry = addresscountry;
    }

    @Basic
    @Column(name = "addressstate", nullable = false, length = 2)
    public String getAddressstate() {
        return addressstate;
    }

    public void setAddressstate(String addressstate) {
        this.addressstate = addressstate;
    }

    @Basic
    @Column(name = "addresscounty", nullable = false, length = -1)
    public String getAddresscounty() {
        return addresscounty;
    }

    public void setAddresscounty(String addresscounty) {
        this.addresscounty = addresscounty;
    }

    @Basic
    @Column(name = "addresscity", nullable = false, length = -1)
    public String getAddresscity() {
        return addresscity;
    }

    public void setAddresscity(String addresscity) {
        this.addresscity = addresscity;
    }

    @Basic
    @Column(name = "addresszip", nullable = false, length = 10)
    public String getAddresszip() {
        return addresszip;
    }

    public void setAddresszip(String addresszip) {
        this.addresszip = addresszip;
    }

    @Basic
    @Column(name = "deprecated", nullable = false)
    public int getDeprecated() {
        return deprecated;
    }

    public void setDeprecated(int deprecated) {
        this.deprecated = deprecated;
    }

    @Id
    @Column(name = "id", nullable = false)
    public int getId() {
        return id;
    }

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

    @Basic
    @Column(name = "chaplainemail", nullable = true, length = 60)
    public String getChaplainemail() {
        return chaplainemail;
    }

    public void setChaplainemail(String chaplainemail) {
        this.chaplainemail = chaplainemail;
    }

    @Basic
    @Column(name = "addresslinetwo", nullable = true, length = -1)
    public String getAddresslinetwo() {
        return addresslinetwo;
    }

    public void setAddresslinetwo(String addresslinetwo) {
        this.addresslinetwo = addresslinetwo;
    }

    @Basic
    @Column(name = "passcode", nullable = false, length = -1)
    public String getPasscode() {
        return passcode;
    }

    public void setPasscode(String passcode) {
        this.passcode = passcode;
    }

    @Basic
    @Column(name = "addresslineone", nullable = false, length = -1)
    public String getAddresslineone() {
        return addresslineone;
    }

    public void setAddresslineone(String addresslineone) {
        this.addresslineone = addresslineone;
    }

    @Basic
    @Column(name = "activated", nullable = false)
    public int getActivated() {
        return activated;
    }

    public void setActivated(int activated) {
        this.activated = activated;
    }

    @Basic
    @Column(name = "sentencestart", nullable = false)
    public Date getSentencestart() {
        return sentencestart;
    }

    public void setSentencestart(Date sentencestart) {
        this.sentencestart = sentencestart;
    }

    @Basic
    @Column(name = "sentenceend", nullable = false)
    public Date getSentenceend() {
        return sentenceend;
    }

    public void setSentenceend(Date sentenceend) {
        this.sentenceend = sentenceend;
    }

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

        ParticipantsEntity that = (ParticipantsEntity) o;

        if (weekscompleted != that.weekscompleted) return false;
        if (weeksneeded != that.weeksneeded) return false;
        if (deprecated != that.deprecated) return false;
        if (id != that.id) return false;
        if (activated != that.activated) return false;
        if (firstname != null ? !firstname.equals(that.firstname) : that.firstname != null) return false;
        if (lastname != null ? !lastname.equals(that.lastname) : that.lastname != null) return false;
        if (email != null ? !email.equals(that.email) : that.email != null) return false;
        if (phonenumber != null ? !phonenumber.equals(that.phonenumber) : that.phonenumber != null) return false;
        if (casenum != null ? !casenum.equals(that.casenum) : that.casenum != null) return false;
        if (offensetype != null ? !offensetype.equals(that.offensetype) : that.offensetype != null) return false;
        if (addresscountry != null ? !addresscountry.equals(that.addresscountry) : that.addresscountry != null)
            return false;
        if (addressstate != null ? !addressstate.equals(that.addressstate) : that.addressstate != null) return false;
        if (addresscounty != null ? !addresscounty.equals(that.addresscounty) : that.addresscounty != null)
            return false;
        if (addresscity != null ? !addresscity.equals(that.addresscity) : that.addresscity != null) return false;
        if (addresszip != null ? !addresszip.equals(that.addresszip) : that.addresszip != null) return false;
        if (chaplainemail != null ? !chaplainemail.equals(that.chaplainemail) : that.chaplainemail != null)
            return false;
        if (addresslinetwo != null ? !addresslinetwo.equals(that.addresslinetwo) : that.addresslinetwo != null)
            return false;
        if (passcode != null ? !passcode.equals(that.passcode) : that.passcode != null) return false;
        if (addresslineone != null ? !addresslineone.equals(that.addresslineone) : that.addresslineone != null)
            return false;
        if (sentencestart != null ? !sentencestart.equals(that.sentencestart) : that.sentencestart != null)
            return false;
        if (sentenceend != null ? !sentenceend.equals(that.sentenceend) : that.sentenceend != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = firstname != null ? firstname.hashCode() : 0;
        result = 31 * result + (lastname != null ? lastname.hashCode() : 0);
        result = 31 * result + (email != null ? email.hashCode() : 0);
        result = 31 * result + (phonenumber != null ? phonenumber.hashCode() : 0);
        result = 31 * result + (casenum != null ? casenum.hashCode() : 0);
        result = 31 * result + weekscompleted;
        result = 31 * result + weeksneeded;
        result = 31 * result + (offensetype != null ? offensetype.hashCode() : 0);
        result = 31 * result + (addresscountry != null ? addresscountry.hashCode() : 0);
        result = 31 * result + (addressstate != null ? addressstate.hashCode() : 0);
        result = 31 * result + (addresscounty != null ? addresscounty.hashCode() : 0);
        result = 31 * result + (addresscity != null ? addresscity.hashCode() : 0);
        result = 31 * result + (addresszip != null ? addresszip.hashCode() : 0);
        result = 31 * result + deprecated;
        result = 31 * result + id;
        result = 31 * result + (chaplainemail != null ? chaplainemail.hashCode() : 0);
        result = 31 * result + (addresslinetwo != null ? addresslinetwo.hashCode() : 0);
        result = 31 * result + (passcode != null ? passcode.hashCode() : 0);
        result = 31 * result + (addresslineone != null ? addresslineone.hashCode() : 0);
        result = 31 * result + activated;
        result = 31 * result + (sentencestart != null ? sentencestart.hashCode() : 0);
        result = 31 * result + (sentenceend != null ? sentenceend.hashCode() : 0);
        return result;
    }

    @OneToMany(mappedBy = "participantsByParticipantid")
    public Collection<AppointmentsEntity> getAppointmentsById() {
        return appointmentsById;
    }

    public void setAppointmentsById(Collection<AppointmentsEntity> appointmentsById) {
        this.appointmentsById = appointmentsById;
    }

    @OneToMany(mappedBy = "participantsByEmail")
    public Collection<ChaplainsEntity> getParticipantsByEmail() {
        return participantsByEmail;
    }

    public void setParticipantsByEmail(Collection<ChaplainsEntity> participantsByEmail) {
        this.participantsByEmail = participantsByEmail;
    }
}

CHAPLAINENTITY hibernate xm

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

    <class name="com.grandcircus.spring.models.ChaplainsEntity" table="chaplains" schema="firststepfreedom">
        <id name="id">
            <column name="id" sql-type="varchar(10)" length="10"/>
        </id>
        <property name="firstname">
            <column name="firstname" sql-type="tinytext"/>
        </property>
        <property name="lastname">
            <column name="lastname" sql-type="tinytext"/>
        </property>
        <property name="email">
            <column name="email" sql-type="varchar(60)" length="60"/>
        </property>
        <property name="phonenumber">
            <column name="phonenumber" sql-type="varchar(25)" length="25"/>
        </property>
        <property name="addresslineone">
            <column name="addresslineone" sql-type="tinytext"/>
        </property>
        <property name="addresslinetwo">
            <column name="addresslinetwo" sql-type="tinytext"/>
        </property>
        <property name="addresscountry">
            <column name="addresscountry" sql-type="tinytext"/>
        </property>
        <property name="addressstate">
            <column name="addressstate" sql-type="varchar(2)" length="2"/>
        </property>
        <property name="addresscounty">
            <column name="addresscounty" sql-type="tinytext"/>
        </property>
        <property name="addresscity">
            <column name="addresscity" sql-type="tinytext"/>
        </property>
        <property name="addresszip">
            <column name="addresszip" sql-type="varchar(10)" length="10"/>
        </property>
        <property name="deprecated">
            <column name="deprecated" sql-type="int(1)"/>
        </property>
        <property name="activated">
            <column name="activated" sql-type="int(1)"/>
        </property>
        <property name="passcode">
            <column name="passcode" sql-type="tinytext"/>
        </property>
        <many-to-one name="participantsByEmail" class="com.grandcircus.spring.models.ParticipantsEntity">
            <column name="email"/>
        </many-to-one>
        <set name="classesById" inverse="true">
            <key>
                <column name="chaplainsid"/>
            </key>
            <one-to-many not-found="ignore" class="com.grandcircus.spring.models.ClassesEntity"/>
        </set>
    </class>
</hibernate-mapping>

PARTICIPANTENTITY hibernate xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

    <class name="com.grandcircus.spring.models.ParticipantsEntity" table="participants" schema="firststepfreedom">
        <id name="id">
            <column name="id" sql-type="int(11) unsigned zerofill"/>
        </id>
        <property name="firstname">
            <column name="firstname" sql-type="tinytext"/>
        </property>
        <property name="lastname">
            <column name="lastname" sql-type="tinytext"/>
        </property>
        <property name="email">
            <column name="email" sql-type="varchar(60)" length="60"/>
        </property>
        <property name="phonenumber">
            <column name="phonenumber" sql-type="varchar(25)" length="25"/>
        </property>
        <property name="casenum">
            <column name="casenum" sql-type="varchar(45)" length="45"/>
        </property>
        <property name="weekscompleted">
            <column name="weekscompleted" sql-type="int(3)"/>
        </property>
        <property name="weeksneeded">
            <column name="weeksneeded" sql-type="int(3)"/>
        </property>
        <property name="offensetype">
            <column name="offensetype" sql-type="tinytext" not-null="true"/>
        </property>
        <property name="addresscountry">
            <column name="addresscountry" sql-type="tinytext"/>
        </property>
        <property name="addressstate">
            <column name="addressstate" sql-type="varchar(2)" length="2"/>
        </property>
        <property name="addresscounty">
            <column name="addresscounty" sql-type="tinytext"/>
        </property>
        <property name="addresscity">
            <column name="addresscity" sql-type="tinytext"/>
        </property>
        <property name="addresszip">
            <column name="addresszip" sql-type="varchar(10)" length="10"/>
        </property>
        <property name="deprecated">
            <column name="deprecated" sql-type="int(1)"/>
        </property>
        <property name="chaplainemail">
            <column name="chaplainemail" sql-type="varchar(60)" length="60" not-null="true"/>
        </property>
        <property name="addresslinetwo">
            <column name="addresslinetwo" sql-type="tinytext" not-null="true"/>
        </property>
        <property name="passcode">
            <column name="passcode" sql-type="tinytext"/>
        </property>
        <property name="addresslineone">
            <column name="addresslineone" sql-type="tinytext"/>
        </property>
        <property name="activated">
            <column name="activated" sql-type="int(1)"/>
        </property>
        <property name="sentencestart">
            <column name="sentencestart" sql-type="date"/>
        </property>
        <property name="sentenceend">
            <column name="sentenceend" sql-type="date"/>
        </property>
        <set name="appointmentsById" inverse="true">
            <key>
                <column name="participantid"/>
            </key>
            <one-to-many not-found="ignore" class="com.grandcircus.spring.models.AppointmentsEntity"/>
        </set>
        <set name="participantsByEmail" inverse="true">
            <key>
                <column name="email"/>
            </key>
            <one-to-many not-found="ignore" class="com.grandcircus.spring.models.ChaplainsEntity"/>
        </set>
    </class>
</hibernate-mapping>

我做错了什么?

1 个答案:

答案 0 :(得分:1)

您的问题是,在ChaplainsEntity课程中,您已将email列映射到两个不同的字段 - emailparticipantsByEmail

这是一个错误,因为如果您在这两个字段中有一个ChaplainsEntity对象具有不同的email值,那么Hibernate将无法知道要在{{1}中保存哪一个}列。唯一可以接受的方法是,如果此列从Hibernate的角度来看是只读的 - 也就是说,您永远不会要求Hibernate插入或更新此值。