hibernated icon indicating copy to clipboard operation
hibernated copied to clipboard

problem getting @ManyToOne working

Open SingingBush opened this issue 9 years ago • 4 comments

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();

SingingBush avatar Dec 29 '15 17:12 SingingBush

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();

SingingBush avatar Sep 16 '16 19:09 SingingBush

Strange. It's not expected to work this way

buggins avatar Sep 22 '16 14:09 buggins

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);

SingingBush avatar Sep 22 '16 16:09 SingingBush

I think there should be WHERE group.id=:group_id

buggins avatar Sep 23 '16 03:09 buggins