So, I have these two different entities
UserCode:
@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
}
ThreadCode:
@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();