gpt4 book ai didi

mongodb - EclipseLink MongoDB @ManyToOne classCastException 非常简单的例子

转载 作者:可可西里 更新时间:2023-11-01 10:33:37 25 4
gpt4 key购买 nike

我试图使 EclipseLink (2.4.1) over 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com