-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: createCriteria returns entities with null fields
PostPosted: Fri Nov 28, 2014 9:31 pm 
Newbie

Joined: Tue Nov 04, 2014 3:36 am
Posts: 4
So, I have these two different entities

User
Code:
    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = User.class)
    @JsonInclude(Include.NON_NULL)
    @Entity
    @Table(name = "user", uniqueConstraints =
    { @UniqueConstraint(columnNames = "email"),
        @UniqueConstraint(columnNames = "nick") })
    public class User implements java.io.Serializable, RecognizedServerEntities
    {
       private static final long serialVersionUID = 1961053649796995346L;
       @Id
       @GeneratedValue(strategy = IDENTITY)
       @Column(name = "id", unique = true, nullable = false)
       private Integer id;
       
       @OneToMany(fetch = FetchType.LAZY, mappedBy = "user", orphanRemoval = false)
       private Set<Thread> threads = new HashSet<Thread>(0);
       
       @OneToMany(fetch = FetchType.LAZY, mappedBy = "user", orphanRemoval = false)
       private Set<Message> messages = new HashSet<Message>(0);

        //...other irrelevant fieds and the usual getters and setters
    }   


Thread
Code:
    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = Thread.class)
    @JsonInclude(Include.NON_NULL)
    @Entity
    @Table(name = "thread")
    public class Thread implements java.io.Serializable, RecognizedServerEntities
    {
       private static final long serialVersionUID = 6447442467668497491L;
       @Id
       @GeneratedValue(strategy = IDENTITY)
       @Column(name = "id", unique = true, nullable = false)
       private Integer id;
       
       @ManyToOne(fetch = FetchType.LAZY)
       @JoinColumn(name = "author", nullable = false)
       private User user;
     }


When I try to use the Criteria interface to retrieve a list of threads belonging to an user, it works great. However, all the threads returned have the user field setted to null.
Debugging the code, the value of user field is
Code:
User_$$_jvste9c_1  (id=84)
.
Don't know if it is related, but I think that it can't correctly invoke toString() on the user instance because it displays com.sun.jdi.InvocationException occurred invoking method.; however, I've overrided toString method in some way, but not hashCode() and equals() because I'm note sure on how to do this with pojo entities.
Is there any way to have the user field initialized?
This is the code used to retrieve the threads
Code:
    session.createCriteria(Thread.class)
                 .add(Restrictions.like("user", anUser))
                 .setMaxResults(50).list();


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.