Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Problems on Join
PostPosted: Mon Nov 07, 2016 4:49 am 
Newbie

Joined: Mon Nov 07, 2016 4:36 am
Posts: 3
Hello,
I'm using Hibernate OGM with MongoDB.

i have a problem with a class that should have a "join" on two other classes. Basically. If i use the "Utenti" class created in this way:

Code:
@Entity
@Indexed
@Table(name="users")
public class Utenti implements Serializable {

   public Utenti() {
   }
   /**
    *
    */
   private static final long serialVersionUID = 5495044706694401513L;

   @Id
   @GeneratedValue(generator = "uuid")
   @GenericGenerator(name = "uuid", strategy = "uuid2")
   @Type(type = "objectid")
   public String id;

   @Column(name="salt")
   private String salt;

   @Column(name="username")
   private String username;


And use the session factory there are no problem. All works flawlessy. While on my "Report" class that has two link to "Utenti" class i have a problem when i try to use a "where" clause to filter the ID. The class is this one:

Code:
@Entity
@Indexed
@Table(name="reports")
public class Reports implements Serializable {

   public Reports() {
   }

   /**
    *
    */
   private static final long serialVersionUID = 5688689875181900535L;

   @Id
   @GeneratedValue(generator = "uuid")
   @GenericGenerator(name = "uuid", strategy = "uuid2")
   @Type(type = "objectid")
   private String id;

   @Column(name="description")
   private String description;

   @JoinColumn(name="verifiedBy")
   @OneToOne
   //@Column(name="verifiedBy")
   private Utenti verifiedBy;

   @JoinColumn(name="user")
   @OneToOne
   //@Column(name="user")
   private Utenti user;

   @JoinColumn(name="category")
   @OneToOne
   //@Column(name="category")
   private Categories category;

   @Column(name="reportDate")
   private String reportDate;

   @Column(name="reportTime")
   private String reportTime;

   @Column(name="thumbnail")
   private String thumbnail;

   @Column(name="image")
   private String image;

   @Column(name="lat")
   private Double lat;

   @Column(name="lon")
   private Double lon;

   @Column(name="elevation")
   private Integer elevation;

   @Column(name="__v")
   private Integer v;

/* getters an setters */


When i try to filter the reports using this query:

Code:
list = session.createQuery("from Reports where user=:IDUtente").setParameter("IDUtente", new String(userId)).list();


I receive this error:

org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [public java.lang.String it.papersoft.hibernate.Utenti.id] by reflection for persistent property [it.papersoft.hibernate.Utenti#id] : 577d485b55fade0e001759e6
at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:43)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:223)
...
caused by: java.lang.IllegalArgumentException: Can not set java.lang.String field it.papersoft.hibernate.Utenti.id to java.lang.String

I've tried different way to use the ID and also the class (as you can see the @Column annotation). So.. my question is. What is the correct way to embed an external class with OGM?

Thank you


Top
 Profile  
 
 Post subject: Re: Problems on Join
PostPosted: Mon Nov 07, 2016 5:59 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 273
Hi,
I can see some problems:

1) Mapping using a generated object ObjectId should be done this way (see documentation https://docs.jboss.org/hibernate/ogm/5. ... n-strategy):
Code:
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Type(type = "objectid")
   public String id;


2) Hibernate OGM does not (yet) support join queries. In this situation a native query or a full-text query might help you.

Hope this help,
Davide


Top
 Profile  
 
 Post subject: Re: Problems on Join
PostPosted: Mon Nov 07, 2016 1:03 pm 
Newbie

Joined: Mon Nov 07, 2016 4:36 am
Posts: 3
even using this query:

Code:
         list = session.createQuery("select r.user from Reports r JOIN r.user where r.user=:IDUtente").setParameter("IDUtente", new String(userId)).list();


And changing the id as you suggested makes no differences, I have this error now:

org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [public java.lang.String it.papersoft.hibernate.Utenti.id] by reflection for persistent property [it.papersoft.hibernate.Utenti#id] : 577d485b55fade0e001759e6


Top
 Profile  
 
 Post subject: Re: Problems on Join
PostPosted: Wed Nov 09, 2016 7:11 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 273
I'm not sure JPA supports that syntax, I would expect the query written as:
Quote:
list = session.createQuery("select r.user from Reports r JOIN r.user u where u.id=:IDUtente").setParameter("IDUtente", userId).list();


Problem is, Hibernate OGM at the moment does not support JOIN queries with MongoDB

Sorry about that.


Top
 Profile  
 
 Post subject: Re: Problems on Join
PostPosted: Wed Nov 09, 2016 7:15 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 273
This chapter of the documentation contains more details about what it is supported and what is not:

https://docs.jboss.org/hibernate/stable ... #ogm-query

Quote:
In particular and of notice, what is not supported is:

cross entity joins
JP-QL functions in particular aggregation functions like count
JP-QL update and delete queries


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.