我试图让 EclipseLink (2.4.1) 通过 MongoDB 在建立关系时按预期工作。但是...
我必须到实体:
@Entity
@NoSql(dataType="account", dataFormat=DataFormatType.MAPPED) // dataType -> collectionName, MAPPED -> because object are transformed into a MAP in MongoDB
@Table(uniqueConstraints = @UniqueConstraint(columnNames = "email"))
public class Account extends JPAMongoBaseEntity {
@Id
@Field(name="_id")
@GeneratedValue
private String id;
@Override
public String getId() { return id;};
public void setId(String id) { this.id = id;};
// Must be unique (id fonc)
@NotNull
@Size(min = 1, max = 256)
@Email
private String email;
...
和:
@Entity
@NoSql(dataType="invoice", dataFormat=DataFormatType.MAPPED) // dataType -> collectionName, MAPPED -> because object are transformed into a MAP in MongoDB
@Table(uniqueConstraints = @UniqueConstraint(columnNames = "label"))
public class Invoice extends JPAMongoBaseEntity {
@Id
@Field(name="_id")
@GeneratedValue
private String id;
... // Relations
@ManyToOne
private Account account;
我尝试获取所有具有 account.id = 参数的发票。 我执行此请求:
TypedQuery<Invoice> q = em.createQuery("Select i from Invoice i join i.account a where a.id=:accountId", Invoice.class);
q.setParameter("accountId", accountId);
List<Invoice> res = q.getResultList();
结果总是相同的:
Internal Exception: java.lang.ClassCastException: org.eclipse.persistence.eis.mappings.EISOneToOneMapping cannot be cast to org.eclipse.persistence.mappings.OneToOneMapping
Query: ReadAllQuery(referenceClass=Invoice jpql="Select i from Invoice i join i.account a where a.id=:accountId")
我尝试了很多东西(@JoinField、@OneToOne,...),但我总是遇到这个异常。
任何帮助将不胜感激。
最佳答案
根据 http://wiki.eclipse.org/EclipseLink/Examples/JPA/NoSQL#Step_6_:_Querying 不支持连接。
您可以尝试使用只读基本映射来映射外键并直接在查询中访问它。或者按照此处所述为 fk 字段添加查询键 http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/Query_Keys
最诚挚的问候, 克里斯
关于mongodb - EclipseLink MongoDB @ManyToOne classCastException 非常简单的示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14857451/