2

I trying to make EclipseLink (2.4.1) over MongoDB works as expected when having relations. But ...

I've got to Entity:

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

and :

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

I try to get all Invoice having account.id = a parameter. I do this request :

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();

And the result is always the same :

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")

I've tried many thing (@JoinField, @OneToOne, ...) but I always run into that exception.

Any help would be greatly apprieciated.

jmcollin92
  • 2,896
  • 6
  • 27
  • 49

1 Answers1

2

According to http://wiki.eclipse.org/EclipseLink/Examples/JPA/NoSQL#Step_6_:_Querying joins are not supported.

You might try mapping the foreign key with a read-only basic mapping and accessing it in the query directly. Or adding a query key for the fk field as described here http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/Query_Keys

Best Regards, Chris

Chris
  • 20,138
  • 2
  • 29
  • 43
  • Thank's for your answer. I have seen article but the example given in Step 6 shows an example of join : `Query query = em.createQuery("Select o from Order o join o.orderLines l where l.description = :desc"); query.setParameter("desc", "shipping"); List orders = query.getResultList();`. This example just don't work with EclipseLink and MongoDB (but the doc seems to says its possible). Do you know if it will be suppported in future version ? – jmcollin92 Feb 15 '13 at 23:53
  • Joins are not supported by MongoDB, so not possible. The join syntax used for the embedded ElementCollection is supported, but not a real join, in this example the orderLines are embedded, so accessible. (joins to ManyToOne, OneToOne, OneToMany, ManyToMany are not supported, joins to ElementCollection are ok) – James Jun 18 '13 at 13:19