I have two entities: Entry and Comment. One entry has n comments. I want to add extra lazy loading in my Entry for Comments. My entities looks like:
@Entity
@Table(name="entries")
public class Entry {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
private int id;
@OneToMany(mappedBy = "entry", cascade = CascadeType.ALL, orphanRemoval = true)
@LazyCollection(LazyCollectionOption.EXTRA)
private List<Comment> comments = new ArrayList<>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public List<Comment> getComments() {
return comments;
}
public boolean containsComment(int commentId) {
return comments.contains(commentId);
}
public Comment getComment(int commentId) {
return comments.get(commentId);
}
}
@Entity
@Table(name="comments")
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
public int id;
@ManyToOne
private Entry entry;
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public Entry getEntry() {
return entry;
}
public void setEntry(Entry entry) {
this.entry = entry;
}
}
I wrote a test to test the association mapping:
@Test
@Transactional
public void testContainsComment() {
/* I inserted already an entry and a comment for the entry into the database */
Entry entry = entryDao.getById(1);
boolean containsComment = entry.containsComment(1); // -> Here I get the following exception: org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [public int com.mypackage.Comment.id] by reflection for persistent property [com.mypackage.Comment#id] : 1
Assert.assertTrue(containsComment);
}
Why I get the following exception?
org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [public int com.mypackage.Comment.id] by reflection for persistent property [com.mypackage.Comment#id] : 1
What did I missed?
Additional Info I should mention that I can access the comments collection with the getComments() method. The size() on the comments collection works too. The single problem is the contains() method. Note that I pass the commentId as parameter to the containsComment()-method. Can / Should I pass something else?
The funny thing is that hibernate logs the right query for the contains method.
Hibernate: select 1 from comments where entry_id =? and id =?