users@glassfish.java.net

Re: RE: Many to Many bidirectional relationship

From: <glassfish_at_javadesktop.org>
Date: Thu, 08 Mar 2007 16:10:04 PST

[b]Does the TestTable class have any other
> relationships?
Yes, has a ManyToOne with an entity called Page.

Here is the code for the TestTable class:[/b]

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import sem90.ptm2.fhmp.page.Page;
import sem90.ptm2.fhmp.story.Story;

/**
 * Entity class Table
 *
 */
@Entity
@Table(name="TESTTABLE")
public class TestTable implements Serializable {
    
    
    private Long id;
    private String tableName;
    private int tableType;
    private String positionBelow;
    private Page p;
    private Collection<Story> stories = new ArrayList<Story>();
    private String htmlContent;
    
    public TestTable() {
    }
    /** Creates a new instance of Table */
    public TestTable(String tableName,int tableType, Page page, String positionBelow, String htmlContent) {
        this.setTableName(tableName);
        this.setTableType(tableType);
        this.p =page;
        this.setPositionBelow(positionBelow);
        this.setHtmlContents(htmlContent);
    }
    
    public TestTable(String tableName,int tableType, Page page, String positionBelow, String htmlContent, Collection<Story> stories) {
        this.setTableName(tableName);
        this.setTableType(tableType);
        this.p =page;
        this.setPositionBelow(positionBelow);
        this.setHtmlContents(htmlContent);
        this.stories = stories;
    }
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="TABLE_ID")
    public Long getId() {
        return this.id;
    }
    
    /**
     * Sets the id of this Table to the specified value.
     * @param id the new id
     */
    public void setId(Long id) {
        this.id = id;
    }
    
    /**
     * Returns a hash code value for the object. This implementation computes
     * a hash code value based on the id fields in this object.
     * @return a hash code value for this object.
     *
     */
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (this.getId() != null ? this.getId().hashCode() : 0);
        return hash;
    }
    
    /**
     * Determines whether another object is equal to this Table. The result is
     * <code>true</code> if and only if the argument is not null and is a Table object that
     * has the same id field values as this object.
     * @param object the reference object with which to compare
     * @return <code>true</code> if this object is the same as the argument;
     * <code>false</code> otherwise.
     */
    @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 TestTable)) {
            return false;
        }
        TestTable other = (TestTable)object;
        if (this.getId() != other.getId() && (this.getId() == null || !this.getId().equals(other.getId()))) return false;
        return true;
    }
    
    /**
     * Returns a string representation of the object. This implementation constructs
     * that representation based on the id fields.
     * @return a string representation of the object.
     */
    @Override
    public String toString() {
        return "sem90.ptm2.fhmp.table.Table[id=" + getId() + "]";
    }
    
    @Column(name="TABLENAME")
    public String getTableName() {
        return tableName;
    }
    
    public void setTableName(String tableName) {
        this.tableName = tableName;
    }
    
    @Column(name="TABLETYPE")
    public int getTableType() {
        return tableType;
    }
    
    public void setTableType(int tableType) {
        this.tableType = tableType;
    }
    
    @Column(name="POSITIONBELOW")
    public String getPositionBelow() {
        return positionBelow;
    }
    
    public void setPositionBelow(String positionBelow) {
        this.positionBelow = positionBelow;
    }
    
    
    @ManyToOne
    @JoinColumn(name="PAGE_ID")
    public Page getP() {
        return p;
    }
    
    public void setP(Page page){
        this.p = page;
    }
    
    
    @ManyToMany(mappedBy="testTables",cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    public Collection<Story> getStories(){
        return stories;
    }
    
    public void setStories(Collection<Story> stories){
        this.stories = stories;
    }
    
    @Transient
    public String getHtmlContents() {
        return htmlContent;
    }
    
    public void setHtmlContents(String htmlContent){
        this.htmlContent = htmlContent;
    }
}
[Message sent by forum member 'ptm' (ptm)]

http://forums.java.net/jive/thread.jspa?messageID=207011