JPA Java - ошибка @JoinColumns - PullRequest
       21

JPA Java - ошибка @JoinColumns

1 голос
/ 22 марта 2011

Я сейчас пытаюсь отобразить информацию, запрошенную через JPA 2.0 (eclipselink).Я использую Glassfish 3.0.1 и NetBeans 6.9.1 в качестве IDE.Мои персистентные сущности создаются под опцией netbeans "создавать классы сущностей из базы данных".Мой shcema разработан с верстаком mysql, и мой сервер, конечно, mysql.Я действительно не могу понять, что создает эту ошибку @JoinColumns.

javax.servlet.ServletException: Exception [EclipseLink-30005] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while searching for persistence archives with ClassLoader: WebappClassLoader (delegate=true; repositories=WEB-INF/classes/)
Internal Exception: javax.persistence.PersistenceException: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [myprojectPU] failed.
Internal Exception: Exception [EclipseLink-7220] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.ValidationException
Exception Description: The @JoinColumns on the annotated element [field userInfo] from the entity class [class com.myproject.jpa.UserId] is incomplete. When the source entity class uses a composite primary key, a @JoinColumn must be specified for each join column using the @JoinColumns. Both the name and the referencedColumnName elements must be specified in each such @JoinColumn.

Эта часть случайным образом переключается с [поле userInfo] из класса сущности [class com.myproject.jpa.UserId] на [поле userIdList] из класса сущности[class com.myproject.jpa.Cell]. Таким образом, оба класса, вероятно, имеют одну и ту же проблему.

Вот мои классы:

@Entity
@XmlRootElement
@Table(name = "user_id", catalog = "workitout", schema = "")
public class UserId implements Serializable {
    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected UserIdPK userIdPK;
    @Basic(optional = false)
    @Column(name = "email", nullable = false, length = 45)
    private String email;
    @Basic(optional = false)
    @Column(name = "password", nullable = false, length = 45)
    private String password;

    @ManyToMany(mappedBy = "userIdList")
    private List<Cell> cellList;

    @JoinColumn(name = "USER_INFO_id_info", referencedColumnName = "id_info", nullable = false, insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private UserInfo userInfo;

    ...    
}

@Entity
@XmlRootElement
@Table(name = "user_info", catalog = "workitout", schema = "")
public class UserInfo implements Serializable {
    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected UserInfoPK userInfoPK;
    @Basic(optional = false)
    @Column(name = "fullName", nullable = false, length = 45)
    private String fullName;
    @Basic(optional = false)
    @Column(name = "city", nullable = false, length = 45)
    private String city;
    @Basic(optional = false)
    @Column(name = "gender", nullable = false, length = 10)
    private String gender;
    @Basic(optional = false)
    @Column(name = "isCoach", nullable = false)
    private boolean isCoach;
    @Column(name = "age")
    private Integer age;
    @Column(name = "description", length = 200)
    private String description;
    @Column(name = "linkImage", length = 45)
    private String linkImage;
    @Column(name = "friendList", length = 500)
    private String friendList;
    @Column(name = "coachList", length = 500)
    private String coachList;

    @JoinColumn(name = "USER_SPORT_id_sport", referencedColumnName = "id_sport", nullable = false, insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private UserSport userSport;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "userInfo")
    private List<UserId> userIdList;

    ...  
}

* Первая ошибка была устранена, но [поле userIdList] из класса сущностей [class com.myproject.jpa.Cell]. осталось.Я добавил код моей сотовой таблицы:

import java.io.Serializable;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

@Entity
@XmlRootElement
@Table(name = "cell", catalog = "workitout", schema = "")
@NamedQueries({
    @NamedQuery(name = "Cell.findAll", query = "SELECT c FROM Cell c"),
    @NamedQuery(name = "Cell.findByIdCell", query = "SELECT c FROM Cell c WHERE c.idCell = :idCell"),
    @NamedQuery(name = "Cell.findByName", query = "SELECT c FROM Cell c WHERE c.name = :name")})
public class Cell implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "id_cell", nullable = false)
    private Integer idCell;
    @Basic(optional = false)
    @Column(name = "name", nullable = false, length = 45)
    private String name;

    @ManyToMany
    @JoinTable(name = "user_id_has_cell", joinColumns = {
        @JoinColumn(name = "cell_id_cell", referencedColumnName = "id_cell", nullable = false)}, inverseJoinColumns = {
        @JoinColumn(name = "USER_ID_id", referencedColumnName = "id", nullable = false),
        @JoinColumn(name = "USER_INFO_id_info", referencedColumnName = "id_info", nullable = false, insertable = false, updatable = false),
        @JoinColumn(name = "USER_SPORT_id_sport", referencedColumnName = "id_sport", nullable = false, insertable = false, updatable = false)
    })
    private List<UserId> userIdList;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "cell")
    private List<WorkoutSession> workoutSessionList;

    public Cell() {
    }

    public Cell(Integer idCell) {
        this.idCell = idCell;
    }

    public Cell(Integer idCell, String name) {
        this.idCell = idCell;
        this.name = name;
    }

    public Integer getIdCell() {
        return idCell;
    }

    public void setIdCell(Integer idCell) {
        this.idCell = idCell;
    }

    public String getName() {
        return name;
    }

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

    public List<UserId> getUserIdList() {
        return userIdList;
    }

    public void setUserIdList(List<UserId> userIdList) {
        this.userIdList = userIdList;
    }

    public List<WorkoutSession> getWorkoutSessionList() {
        return workoutSessionList;
    }

    public void setWorkoutSessionList(List<WorkoutSession> workoutSessionList) {
        this.workoutSessionList = workoutSessionList;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idCell != null ? idCell.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 Cell)) {
            return false;
        }
        Cell other = (Cell) object;
        if ((this.idCell == null && other.idCell != null) || (this.idCell != null && !this.idCell.equals(other.idCell))) {
            return false;
        }
        return true;
    }

Ответы [ 2 ]

2 голосов
/ 22 марта 2011

Сообщение об ошибке довольно четко описывает проблему - UserInfo имеет составной ключ, поэтому вам нужно указать несколько @JoinColumn s, по одному столбцу для каждого поля составного ключа:

@JoinColumns({
    @JoinColumn(...),
    @JoinColumn(...)
})
@ManyToOne(optional = false)
private UserInfo userInfo;
0 голосов
/ 22 марта 2011

Из сообщения об ошибке я бы попытался добавить в ваш класс следующее: UserId:

@JoinColumn(name = "USER_INFO_uSERSPORTidsport", referencedColumnName = "uSERSPORTidsport", nullable = false, insertable = false, updatable = false)
@ManyToOne(optional = false)
private UserSport usersport;
...