开发者

JPA an Java - @JoinColumns error

I am currently trying to display information requested via JPA 2.0 (eclipselink). I am using glassfish 3.0.1 and netbeans 6.9.1 as IDE. My persistence entities are created under netbeans option "create entity classes from database". My shcema is designed with workbench mysql, and my server is of course mysql. I really cant figure out what creates this @JoinColumns error.

javax.servlet.ServletException: Exception [EclipseLink-30005] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.PersistenceUni开发者_开发知识库tLoadingException
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.

This part randomly swaps from [field userInfo] from the entity class [class com.myproject.jpa.UserId] to [field userIdList] from the entity class [class com.myproject.jpa.Cell]. So both classes probably have the same issue.

Here are my classes:

@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;

    ...  
}

*The first error was solved, but the [field userIdList] from the entity class [class com.myproject.jpa.Cell]. remains. Ive added the code of my cell table:

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;
    }


Error message describes the problem pretty clearly - UserInfo has a composite key, therefore you need to specify several @JoinColumns, one column for each field of the composite key:

@JoinColumns({
    @JoinColumn(...),
    @JoinColumn(...)
})
@ManyToOne(optional = false)
private UserInfo userInfo;


From the error message I would try to add the following to your class UserId:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜