simple entity example

Take a look how easy entities can be created, it is just a simple example but that was my intention.

package pl.mypck.db;

import ...;

@Entity
@NamedQueries({
    @NamedQuery(name = "Book.fetchAuthor", query = "SELECT b FROM Book b WHERE b.author = :author"),
    @NamedQuery(name = "Book.fetchAllBooks", query = "SELECT b FROM Book b")
})
public class Book implements Serializable {
    private static final long serialVersionUID = 1L;
    private Long id;
    private String author;
    private List<Page> pages = new ArrayList();

    public void setId(Long id){this.id = id;}
    public void setAuthor(String a){this.author = a;}
    public void setPages(List<Page> ps){this.pages = ps;}
    public void addPages(Page p){this.pages.add(p);}
    @Id
    @SequenceGenerator(name="BOOK_SEQ", allocationSize=0, sequenceName="BOOK_SEQ")
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="BOOK_SEQ")
    @Column(name="id_book")
    public Long getId() {
        return id;
    }
    public String getAuthor(){return this.author;}
    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    /* when bidirectional mapping */
    //with bidirectional mapping I should add mappedBy="book" to @OneToMany annotation
    public List<Page> getPages(){
        return this.pages;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Book)) {
            return false;
        }
        Book other = (Book) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return " pl.mypck.db.Book[id=" + id + "]";
    }
}
//Page class
package pl.mypck.db;

import ...;

@Entity
public class Page implements Serializable {
    private static final long serialVersionUID = 1L;
    private Long id;
    //private Book book; /* when bidirectional mapping */
    private String content;

    public void setId(Long id){this.id = id;}
    //public void setBook(Book b){this.book = b;}/* when bidirectional mapping */
    public void setContent(String c){this.content = c;}

    @Id
    @SequenceGenerator(name="PAGE_SEQ", allocationSize=0, sequenceName="PAGE_SEQ")
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="PAGE_SEQ")
    public Long getId() {return id;}
    //@ManyToOne(cascade={CascadeType.ALL})/* when bidirectional mapping */
    //@JoinColumn(name="id_book")/* when bidirectional mapping */
    //public Tabela getBook() {return this.book;}/* when bidirectional mapping */
    public String getContent(){return this.content;}

    /*
     *
     */
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Page)) {
            return false;
        }
        Page other = (Page) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "pl.mypck.db.Page[id=" + id + "]";
    }
}

so now we have two classes (entities) that we can use as normal objects and will be saved in some DB. here only Book knows about its pages (aka indirect mapping).

Page p = new Page();
p.setContent("text on first page");
Book b = new Book();
b.addPages(p);

it is working, fetching and inserting or updating, deleting but what Hibernate created in DB system is stunning.

Dodaj komentarz