simple entity mapping example

because Entities are just simple POJOs there is nothing really to talk about.
The good idea is to implement from Serializable interface and override int hashCode(), boolean equals(Object object) and String toString() methods. With IDE like netbeans it is all auto generated for you so you don’t need to worry about.

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 one 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 &amp;&amp; other.id != null) || (this.id != null &amp;&amp; !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 &amp;&amp; other.id != null) || (this.id != null &amp;&amp; !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

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

A book object has its pages and 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);

very simple very nice, I like it:)
it is working, fetching and inserting or updating, deleting but what Hibernate created in DB system sometimes scares me out.

Możliwość komentowania jest wyłączona.