2011-03-22 29 views
1

我目前正試圖顯示通過JPA 2.0(eclipselink)請求的信息。我使用glassfish 3.0.1和NetBeans 6.9.1作爲IDE。我的持久性實體是在netbeans選項「從數據庫創建實體類」下創建的。我的shcema是用workbench mysql設計的,而我的服務器當然是mysql。我真的不知道是什麼造成了這個@JoinColumns錯誤。JPA Java - @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]交換從實體類[類com.myproject.jpa.UserId][字段userIdList]從實體類[類com.myproject.jpa。細胞]。所以這兩個類可能都有相同的問題。

這裏是我的課:

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

    ... 
} 

*的第一個錯誤是解決了,但是從實體類[類com.myproject.jpa.Cell]的[字段userIdList。仍然存在。伊夫加入我的單元格表的代碼:

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

回答

0

從錯誤消息,我會嘗試以下添加到您的類UserId

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

錯誤消息描述的問題很清楚 - UserInfo有複合鍵,因此您需要指定幾個@JoinColumn s,複合鍵的每個字段的一列:

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

這適用於userIn我不得不停止服務器,乾淨的構建,然後測試它看到它的工作...有點討厭。該問題仍然存在於實體類[class com.myproject.jpa.Cell]中的** [field userIdList] ** – guiomie 2011-03-22 14:40:30