java - hibernate select data from two tables -


i have 2 tables: authors , books

author:

 @entity  @table (name="authors")  public class author implements  java.io.serializable {  private integer id; private string name; private string lastname; /*book list*/ private set<book> books= new hashset<book>(0);  public author() { }  public author(string name, string lastname) {     this.name = name;     this.lastname = lastname; }  public author(string name, string lastname, set<book> books) {     this.name = name;     this.lastname = lastname;     this.books = books; } @id @generatedvalue(strategy = generationtype.identity) @column(name = "author_id", unique = true, nullable = false) public integer getid() {     return id; } public void setid(integer id) {     this.id = id; }  @column(name = "author_name", unique = true, nullable = false, length = 10) public string getname() {     return name; } public void setname(string name) {     this.name = name; }  @column(name = "author_lastname", unique = true, nullable = false, length = 10) public string getlastname() {     return lastname; }  public void setlastname(string lastname) {     this.lastname = lastname; }   @onetomany(fetch = fetchtype.lazy, mappedby = "author") public set<book> getbooks() {     return books; }  public void setbooks(set<book> books) {     this.books = books; } } 

book:

import javax.persistence.*;  @entity @table(name = "books") public class book implements  java.io.serializable { private integer id; private string name; private author author; public book() { } public book(string name) {     this.name = name; }  public book(string name, author author) {     this.name = name;     this.author = author; } @id @column(name = "book_id") @generatedvalue(strategy = generationtype.identity) public integer getid() {     return id; }  public void setid(integer id) {     this.id = id; }  @column(name = "book_name") public string getname() {     return name; }  public void setname(string name) {     this.name = name; }  @manytoone(fetch = fetchtype.lazy) @joincolumn(name = "author_id",nullable = false) public author getauthor() {     return author; } public void setauthor(author author) {     this.author = author; } } 

in book's table have field id author. how can books 1 author? how can solve it? must use hql or other methods? beginner in this.

i wont code here logic.. first thing need build relationship between author , books using annotations @onetomany or @manytoone depending on structure.

next use author class object retrive list of books.


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -