我使用Eclipse 2.5.2在Oracle 11g数据库中开发了简单的JPA2.1应用程序。以下是我的情景 - 有两个实体 @ManyToMany 关系
Club.java
@Entity(name = "CLUB")
public class Club {
@Id
private int c_id;
private String c_name;
@ManyToMany(cascade={CascadeType.PERSIST, CascadeType.MERGE},
fetch=FetchType.EAGER)
@JoinTable(name = "CLUB_PEOPLE",
joinColumns=@JoinColumn(name="c_id", referencedColumnName="c_id"),
inverseJoinColumns=@JoinColumn(name="p_id"))
private Set<People> people=new HashSet<People>();
public int getC_id() {
return c_id;
}
public void setC_id(int c_id) {
this.c_id = c_id;
}
public String getC_name() {
return c_name;
}
public void setC_name(String c_name) {
this.c_name = c_name;
}
public Set<People> getPeople() {
return people;
}
public void setPeople(Set<People> people) {
this.people = people;
}
public void addPeople(People p){
getPeople().add(p);
}
}
的 People.Java
@Entity(name="people")
public class People{
@Id
private int p_id;
private String name ;
@ManyToMany(mappedBy="people")
private Set<Club> club;
public int getP_id() {
return p_id;
}
public void setP_id(int p_id) {
this.p_id = p_id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Client.java
public class Client1 {
public static void main(String[] args) {
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("JPA-Query");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Club> cq = cb.createQuery(Club.class).distinct(true);
Root<Club> root = cq.from(Club.class);
final List<Predicate> requiredCriteria = new ArrayList<Predicate>();
final Join people = root.join("people", JoinType.LEFT);
requiredCriteria.add(cb.or(cb.isNull(people)));
requiredCriteria.add(cb.isNull(people.get("name")));
requiredCriteria.add(cb.equal(people.get("name"), "people1"));
TypedQuery<Club> q = em.createQuery(
cq.where(cb.or(requiredCriteria
.toArray(new Predicate[requiredCriteria.size()])
))
);
List<Club> allemps = q.getResultList();
em.close();
emf.close();
}
}
表 -
CREATE TABLE Club(
c_id number NOT NULL,
c_name VARCHAR2(20)
)
ALTER TABLE Club ADD CONSTRAINT CLUGGRPPK PRIMARY KEY (c_id);
CREATE TABLE People(
p_id number NOT NULL,
name VARCHAR2(20)
)
ALTER TABLE People ADD CONSTRAINT PEOPLEGRPPK PRIMARY KEY (p_id);
CREATE TABLE CLUB_PEOPLE
(
C_ID number,
P_ID number,
CONSTRAINT CLUB_PEOPLEPK PRIMARY KEY (C_ID, P_ID),
CONSTRAINT GROUP_FK FOREIGN KEY("C_ID")
REFERENCES "CLUB"("C_ID") ON DELETE CASCADE,
CONSTRAINT PEOPLE_FK FOREIGN KEY("P_ID")
REFERENCES "PEOPLE"("P_ID") ON DELETE CASCADE
)
当我运行Client.java时,它会抛出异常
Exception Description: Object comparisons can only be used with OneToOneMappings. Other mapping comparisons must be done through query keys or direct attribute level comparisons.
Mapping: [org.eclipse.persistence.mappings.ManyToManyMapping[people]]
Expression: [
Query Key people
Base ManyToMany.Club]
Query: ReadAllQuery(referenceClass=Club )
javax.persistence.PersistenceException: Exception [EclipseLink-6076] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.QueryException
Exception Description: Object comparisons can only be used with OneToOneMappings. Other mapping comparisons must be done through query keys or direct attribute level comparisons.
Mapping: [org.eclipse.persistence.mappings.ManyToManyMapping[people]]
Expression: [
Query Key people
Base ManyToMany.Club]
Query: ReadAllQuery(referenceClass=Club )
at org.eclipse.persistence.internal.jpa.QueryImpl.getResultList(QueryImpl.java:480)
at ManyToMany.Client.main(Client.java:75)
Caused by: Exception [EclipseLink-6076] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.QueryException
Exception Description: Object comparisons can only be used with OneToOneMappings. Other mapping comparisons must be done through query keys or direct attribute level comparisons.
Mapping: [org.eclipse.persistence.mappings.ManyToManyMapping[people]]
Expression: [
Query Key people
Base ManyToMany.Club]
Query: ReadAllQuery(referenceClass=Club )
at org.eclipse.persistence.exceptions.QueryException.unsupportedMappingForObjectComparison(QueryException.java:1170)
请帮我解决上述问题。提前致谢。