0

I am making a small library project in Java EE. I have created 3 tables and class with authors, genres and books. Now I am trying to connect it using hibernate, but i haven't ide how confire annotations ... Please help me :)

bookTable:

| id_book | author_id | title | genre_id | description | photo |

genreTable:

| genre_id | genre |

authorTable:

| author_id | author|

It is easy structure:

bookTable.author_id - authorTable.author_id = ManyToMany

bookTable.genre_id - genreTable.genre_id = OneToOne

Below there are my pojo class:

Book

@Entity
@Table(name = "books")
public class Book implements Serializable{

    private static final long serialVersionUID = -5057364006691079475L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "user_id")
    private Integer user_id;
    private Author author;
    private String description;
    private BookGenre genre;
    private String title;


    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Integer getUser_id() {
        return user_id;
    }

    public void setUser_id(Integer user_id) {
        this.user_id = user_id;
    }

    public Author getAuthor() {
        return author;
    }

    public void setAuthor(Author author) {
        this.author = author;
    }

    public BookGenre getGenre() {
        return genre;
    }

    public void setGenre(BookGenre genre) {
        this.genre = genre;
    }

}

Author

@Entity
@Table(name = "author")
public class Author implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "author_id")
    private Integer author_id;

    @Column(name = "author")
    private String author;  

    public Integer getAuthor_id() {
        return author_id;
    }

    public void setAuthor_id(Integer author_id) {
        this.author_id = author_id;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }
}

Genre

@Entity
@Table(name = "genre")
public class BookGenre implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "genre_id")
    private Integer genreId;

    @Column(name = "genre")
    private String genre;


    public Integer getGenreId() {
        return genreId;
    }
    public void setGenreId(Integer genreId) {
        this.genreId = genreId;
    }
    public String getGenre() {
        return genre;
    }
    public void setGenre(String genre) {
        this.genre = genre;
    }

}

2 Answers 2

1

Should it be a uni-directional or bi-directional association? Have a look at this example:

https://howtoprogramwithjava.com/hibernate-manytomany-unidirectional-bidirectional/

It even uses your entity names :)

Sign up to request clarification or add additional context in comments.

Comments

0

That are entitys and no pojos, but they look good so far. For them you normally don't need to take care. Best way, you autogenerate them after you connected your project with the database. Hibernate will take care for everything. What is more interesting is how your DAO looks, bcz. that is the layer communication with your database. The entity is only the representation of the database table on Java side. I guess you already connected your project with the database? Please provide your Database Access Object (DAO) for further help. If you havent that so far here you can get help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.