2010-09-14 92 views
1

嗨我有一個問題,從志願者獲取技能。由於某種原因,我不使用這種方法ManyToMany JPA取得

public Volunteer getVolunteer(int id){ 

Volunteer vo; 

Query q; 

q = em.createNamedQuery("Volunteer.findById").setParameter("id", id); 
vo = (Volunteer) q.getSingleResult(); 

for(Skill s: vo.getSkills()){ 
    System.out.println(s); 
} 

return vo; 

} 

列表爲空當,因此取似乎並沒有工作獲取列表。

進出口使用JPA的EclipseLink和Glassfish

任何幫助表示讚賞!

技能實體:

@Entity 
@Table(name="skill") 
@NamedQueries({ 
    @NamedQuery(name = "Skill.findAll", query = "SELECT s FROM Skill s"), 
    @NamedQuery(name = "Skill.findById", query = "SELECT s FROM Skill s WHERE s.id = :id"), 
    @NamedQuery(name = "Skill.findBySkillDescription", query = "SELECT s FROM Skill s WHERE s.skillDescription = :skillDescription")}) 
public class Skill implements Serializable { 
@Override 
public String toString() { 
    return "Skill [id=" + id + ", skillDescription=" + skillDescription 
    + "]"; 
} 

private static final long serialVersionUID = 1L; 

@Id 
@GeneratedValue(strategy=GenerationType.IDENTITY) 
@Column(unique=true, nullable=false) 
private int id; 

@Column(name="skill_description", length=130) 
private String skillDescription; 

//bi-directional many-to-many association to Volunteer 
    @ManyToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER) 
@JoinTable(
    name="skill_volunteer" 
    , joinColumns={ 
    @JoinColumn(name="skill_id", nullable=false) 
    } 
    , inverseJoinColumns={ 
    @JoinColumn(name="volunteer_id", nullable=false) 
    } 
) 
private List<Volunteer> volunteers; 

    public Skill() { 
    } 

public int getId() { 
    return this.id; 
} 

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

public String getSkillDescription() { 
    return this.skillDescription; 
} 

public void setSkillDescription(String skillDescription) { 
    this.skillDescription = skillDescription; 
} 

public List<Volunteer> getVolunteers() { 
    return this.volunteers; 
} 

public void setVolunteers(List<Volunteer> volunteers) { 
    this.volunteers = volunteers; 
} 

} 

而且志願實體:所以取似乎並沒有工作

@Entity 
@Table(name="volunteer") 
@NamedQueries({ 
    @NamedQuery(name = "Volunteer.findAll", query = "SELECT v FROM Volunteer v"), 
    @NamedQuery(name = "Volunteer.findById", query = "SELECT v FROM Volunteer v WHERE v.id = :id"), 
    @NamedQuery(name = "Volunteer.findByPhone", query = "SELECT v FROM Volunteer v WHERE v.phone = :phone"), 
    @NamedQuery(name = "Volunteer.findByEmail", query = "SELECT v FROM Volunteer v WHERE v.email = :email"), 
    @NamedQuery(name = "Volunteer.findByFirstName", query = "SELECT v FROM Volunteer v WHERE v.firstName = :firstName"), 
    @NamedQuery(name = "Volunteer.findByLastName", query = "SELECT v FROM Volunteer v WHERE v.lastName = :lastName")}) 
public class Volunteer implements Serializable { 
@Override 
public String toString() { 
    return "Volunteer [id=" + id + ", email=" + email + ", firstName=" 
    + firstName + ", lastName=" + lastName + ", phone=" + phone 
    + "]"; 
} 

private static final long serialVersionUID = 1L; 

@Id 
@GeneratedValue(strategy=GenerationType.IDENTITY) 
@Column(unique=true, nullable=false) 
private int id; 

@Column(length=255) 
private String email; 

@Column(name="first_name", length=255) 
private String firstName; 

@Column(name="last_name", length=255) 
private String lastName; 

@Column(length=255) 
private String phone; 

//bi-directional many-to-many association to Event 
@ManyToMany(mappedBy="volunteers") 
private List<Event> events; 

//bi-directional many-to-many association to Skill 
@ManyToMany(mappedBy="volunteers", cascade=CascadeType.ALL, fetch=FetchType.EAGER) 
private List<Skill> skills; 

    public Volunteer() { 
    } 

public int getId() { 
    return this.id; 
} 

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

public String getEmail() { 
    return this.email; 
} 

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

public String getFirstName() { 
    return this.firstName; 
} 

public void setFirstName(String firstName) { 
    this.firstName = firstName; 
} 

public String getLastName() { 
    return this.lastName; 
} 

public void setLastName(String lastName) { 
    this.lastName = lastName; 
} 

public String getPhone() { 
    return this.phone; 
} 

public void setPhone(String phone) { 
    this.phone = phone; 
} 

public List<Event> getEvents() { 
    return this.events; 
} 

public void setEvents(List<Event> events) { 
    this.events = events; 
} 

public List<Skill> getSkills() { 
    return this.skills; 
} 

public void setSkills(List<Skill> skills) { 
    this.skills = skills; 
} 

} 

回答

0

列表是空的。

獲得一個空列表不允許得出結論,「取」不工作,以及是否使用LAZY或EAGER獲取類型,你應該得到的技能記錄對於給定的志願者,如果有任何。

而且由於映射看起來是正確的,我會通過查看生成的SQL啓動並運行它針對數據庫,以確認

  1. 生成的SQL是預期的結果
  2. 的數據是正確的。

要做到這一點,設置在persistence.xml以下屬性:

<property name="eclipselink.logging.level" value="FINEST" /> 
    <property name="eclipselink.logging.level.sql" value="FINEST" /> 

然後找到該findByIdgetSkills的SQL查詢和運行它們的數據庫。

然後請更新問題與獲得的結果爲一個給定的ID(生成的SQL和確認數據是好的)。

0

只是嘗試使用em.flush(),它是爲我工作;

Query q = em.createNamedQuery("Volunteer.findById").setParameter("id", id); 
vo = (Volunteer) q.getSingleResult(); 
em.flush(); 
for(Skill s: vo.getSkills()){ 
    System.out.println(s); 
} 
0

最常見的問題有雙向關係是當用戶不維護雙方 - JPA實體是普通Java對象,併爲你喜歡的EJB 2.0使用JPA不能修復關係。當您將志願者添加到技能時,您還必須將技能添加到志願者,以便緩存與您在數據庫中強制進行的更改保持同步。