
package org.cipres.treebase.domain.taxon;

import javax.persistence.AttributeOverride;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Index;

import org.cipres.treebase.domain.AbstractPersistedObject;
import org.cipres.treebase.domain.TBPersistable;

/**
 * A TaxonVariant is usually corresponding to one uBio namebank id object.
 * 
 * Created on Feb 14, 2006
 * 
 * @author Jin Ruan
 * 
 */

@Entity
@Table(name = "TAXONVARIANT")
@AttributeOverride(name = "id", column = @Column(name = "TAXONVARIANT_ID"))
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, region = "taxonCache")
@BatchSize(size = 20)
public class TaxonVariant extends AbstractPersistedObject {

	private static final long serialVersionUID = 1L;

	private Long mNamebankID;
	private String mName;
	private String mFullName;
	private String mLexicalQualifier;
	private Integer mTB1LegacyId; // mjd 20090415

	private Taxon mTaxon;

	/**
	 * Constructor.
	 */
	public TaxonVariant() {
		super();
	}

	/**
	 * Constructor.
	 * 
	 * @param nameBankID
	 * @param nameString
	 * @param fullNameString
	 * @param lexicalQualifier
	 */
	public TaxonVariant(
		Long nameBankID,
		String nameString,
		String fullNameString,
		String lexicalQualifier) {

		super();

		setNamebankID(nameBankID);
		setName(nameString);
		setFullName(fullNameString);
		setLexicalQualifier(lexicalQualifier);

	}

	/**
	 * uBio name variant short style.
	 * 
	 * @return the nameString
	 */
	@Column(name = "Name", nullable = true, length = TBPersistable.COLUMN_LENGTH_STRING)
	public String getName() {
		return mName;
	}

	/**
	 * @param pNameString the nameString to set
	 */
	private void setName(String pNameString) {
		mName = pNameString;
	}

	/**
	 * uBio name bank id.
	 * 
	 * @return
	 */
	@Column(name = "NamebankID", nullable = true)
	@Index(name = "TVARI_NBID_IDX") // index name length: 18 char
	public Long getNamebankID() {
		return mNamebankID;
	}

	/**
	 * @param pNameBankID
	 */
	private void setNamebankID(Long pNameBankID) {
		mNamebankID = pNameBankID;
	}

	/**
	 * uBio name variant long style.
	 * 
	 * @return
	 */
	@Column(name = "FullName", nullable = true, length = TBPersistable.COLUMN_LENGTH_STRING)
	@Index(name = "TVARI_FNAME_IDX")
	public String getFullName() {
		return mFullName;
	}

	/**
	 * @param pFullNameString
	 */
	private void setFullName(String pFullNameString) {
		mFullName = pFullNameString;
	}

	/**
	 * uBio qualifier (e.g. canonical form)
	 * 
	 * @return
	 */
	@Column(name = "LexicalQualifier", nullable = true, length = TBPersistable.COLUMN_LENGTH_50)
	public String getLexicalQualifier() {
		return mLexicalQualifier;
	}

	/**
	 * @param pLexicalQualifier
	 */
	private void setLexicalQualifier(String pLexicalQualifier) {
		mLexicalQualifier = pLexicalQualifier;
	}

	/**
	 * @return
	 */
	@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST})
	@JoinColumn(name = "TAXON_ID", nullable = false)
	@Index(name = "TVARI_TAXON_IDX")
	public Taxon getTaxon() {
		return mTaxon;
	}

	/**
	 * @param pTaxon the taxon to set
	 */
	public void setTaxon(Taxon pTaxon) {
		mTaxon = pTaxon;
	}

	@Column(name = "TB1LegacyId", nullable = true)
	public Integer getTB1LegacyId() {
		return mTB1LegacyId;
	}

	public void setTB1LegacyId(Integer legacyId) {
		mTB1LegacyId = legacyId;
	}
	
	@Transient
	public String getLabel() {
		return getFullName();
	}
}

@@ -518,24 +518,110 @@
 yStrategy;%0D%0A
+import org.hibernate.annotations.Fetch;%0D%0Aimport org.hibernate.annotations.FetchMode;%0D%0A
 import org.h
@@ -3606,32 +3606,57 @@
 RI_TAXON_IDX%22)%0D%0A
+%09@Fetch(FetchMode.JOIN)%0D%0A
 %09public Taxon ge

Set FetchMode to JOIN to prevent lazy initialization exceptions.