I have the following classes mapped as one to many: Reader and book when one reader can hold more then one book:
Book:
@Entity
@Table(name = "book")
public class Book implements Serializable{
    private static final long serialVersionUID = 1L;
    private Long id;
    private String author;
    private String title;
    public Book(){}
    public Book(String author,String title){
        this.author = author;
        this.title = title;
    }
    @Id
    @GeneratedValue
    @Column(name = "BOOK_ID")
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    @Column(name = "author")
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    @Column(name="title")
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    //equals and hashcode ommited.
}
Reader
@Entity
@Table(name = "reader")
public class Reader implements Serializable{
    private static final long serialVersionUID = 1L;
    private Long id;
    private String firstName;
    private String lastName;
    Set<Book> set = new HashSet<Book>();
    @Transient
    public void loanBook(Book book){
        set.add(book);
    }
    @Id
    @GeneratedValue
    @Column(name = "READER_ID")
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    @Column(name="firstName")
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    @Column(name="lastName")
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    @OneToMany(cascade=CascadeType.ALL,fetch = FetchType.LAZY)
     @JoinColumn(name="READER_ID")
    public Set<Book> getSet() {
        return set;
    }
    public void setSet(Set<Book> set) {
        this.set = set;
    }
    public Reader(){}
    public Reader(String firstName, String lastName){
        this.firstName = firstName;
        this.lastName = lastName;
    }
}
Now I would like to fetch reader class using some book id for example:
Reader reader = service.getReaderbyBook(Long.valueOf(10));
My function looks like:
    @Override
    public Reader getReaderbyBook(Long id) {
        Session session = null;
        Reader reader = null;
         session =sessionFactory.openSession();
           org.hibernate.Transaction tr = session.getTransaction();
           tr.begin();
           String hqlSelect = "Select .... where book.id:=id";
           Query query = session.createQuery(hqlSelect);
           query.setLong("id", id);
           reader =  (Reader) query.uniqueResult();
           tr.commit();
           session.flush();
              session.close();
              return reader;
开发者_如何学Python    }
}
How my hql select should look like if I just want to fetch a single reader associated with some book?
from Reader r join r.set book where book.id = :id .  
I think you should create a table to map readers to the books. It make things easier.
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论