hibernated
hibernated copied to clipboard
problem getting @ManyToOne working
the following seems to break:
@Entity
@Table("books")
public class Book {
public:
@Id
@Generated
Uint id;
@Column("title", 250) @NotNull
string title;
@ManyToOne @JoinColumn("author_id") @NotNull
Person author;
}
@Entity
@Table("people")
public class Person {
public:
@Id
@Generated
Uint id;
@Column("name", 50) @NotNull
string name;
}
when I try to get books by user the program bombs out:
Query q = session
.createQuery("FROM Book WHERE author=:person")
.setParameter("person", somePersonObject);
return q.list!Book();
I thought I had updated this issue. I have got ManyToOne working, just not as expected. It wasn't clear from the documentation that I had to do:
Query q = session
.createQuery("FROM Book WHERE author=:personId")
.setParameter("personId", somePersonObject.id);
return q.list!Book();
Strange. It's not expected to work this way
I added a test for it in hdtest/source/htestmain.d in this commit to ensure it keeps working as-is:
writeln("Test retrieving users by group... (ManyToOne relationship)");
auto qUsersByGroup = sess.createQuery("FROM User WHERE group=:group_id").setParameter("group_id", grp2.id);
User[] usersByGroup = qUsersByGroup.list!User();
assert(usersByGroup.length == 2);
I think there should be WHERE group.id=:group_id