Java tutorial
/* **************************************************************************** * * * (c) Copyright 2006 ABM-utvikling * * * * This program is free software; you can redistribute it and/or modify it * * under the terms of the GNU General Public License as published by the * * Free Software Foundation; either version 2 of the License, or (at your * * option) any later version. * * * * This program is distributed in the hope that it will be useful, but * * WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General * * Public License for more details. http://www.gnu.org/licenses/gpl.html * * * **************************************************************************** */ package no.abmu.questionnarie.domain; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hibernate.annotations.*; import javax.persistence.Id; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Transient; import javax.persistence.ManyToMany; import javax.persistence.CascadeType; import javax.persistence.OneToMany; import javax.persistence.ManyToOne; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.DiscriminatorValue; import javax.persistence.DiscriminatorType; import javax.persistence.DiscriminatorColumn; import javax.persistence.Table; import javax.persistence.JoinTable; import javax.persistence.JoinColumn; import javax.persistence.Column; import java.util.HashSet; import java.util.Set; import no.abmu.common.domain.*; import no.abmu.common.domain.Entity; /** * @author Erik Romson, erik@zenior.no * @author $Author:$ * @version $Rev:$ * @date $Date:$ * @copyright ABM-Utvikling * @hibernate.class table="FINANCE_POST" * @hibernate.discriminator column="type" * @hibernate.cache usage="nonstrict-read-write" * @navassoc * - 1 no.abmu.finances.domain.PostPeriod * @navassoc * - 1 no.abmu.finances.domain.PostValidator * @navassoc * - 1 no.abmu.finances.domain.PostType */ @javax.persistence.Entity //@Table(schema="QUESTIONNARIE") @Table(name = "QUESTIONNARIE_POST") //@Cache(usage = CacheConcurrencyStrategy.READ_ONLY) @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name = "posttype", discriminatorType = DiscriminatorType.STRING) @DiscriminatorValue("Post") abstract public class Post implements no.abmu.common.domain.Entity { private static final Log logger = (Log) LogFactory.getLog(Post.class); private Long id; private String code; private String name; private Set<Account> accounts = new HashSet<Account>(); private Set<PostValidator> validators = new HashSet<PostValidator>(); private PostPeriod period; private PostType postType; protected Post() { } protected Post(String code, PostPeriod period, PostType postType) { this.code = code; this.period = period; this.postType = postType; } /** * @return * @hibernate.id generator-class="increment" */ @Id @GeneratedValue(strategy = GenerationType.AUTO) public Long getId() { return id; } /** * @hibernate.property not-null="true" * unique="true" */ @Index(name = "post_code_idx") @Column(unique = true) public String getCode() { return code; } /** * @hibernate.property */ public String getName() { return name; } /** * @hibernate.set lazy="true" * inverse="true" * cascade="save-update" * table="FINANCE_ACCOUNT_POST" * @hibernate.key column="FK_POST_ID" * @hibernate.many-to-many class="no.abmu.finances.domain.Account" * column="FK_ACCOUNT_ID" * @hibernate.cache usage="nonstrict-read-write" * @hibernate.collection-key column="FK_POST_ID" * @hibernate.collection-many-to-many class="no.abmu.finances.domain.Account" * column="FK_ACCOUNT_ID" */ // @ManyToMany(cascade = {CascadeType.ALL}) // @ForeignKey(name = "FK_ACCOUNT_ID", inverseName = "FK_ACCOUNT_ID") // @JoinTable( // name = "QUESTIONNARIE_ //// schema = "QUESTIONNARIE" // ) @ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, mappedBy = "posts") // @Cache(usage = CacheConcurrencyStrategy.READ_ONLY) public Set<Account> getAccounts() { return accounts; } public void addAccount(Account account) { accounts.add(account); } /** * @hibernate.set lazy="false" * cascade="all" * table="FINANCE_POST_VALIDATOR" * @hibernate.key column="FK_POST_ID" * @hibernate.key-column name="FK_POST_ID" * not-null="false" * index="Post_ValdiatorId_idx" * @hibernate.one-to-many class="no.abmu.finances.domain.PostValidator" * @hibernate.cache usage="nonstrict-read-write" * @hibernate.collection-key column="FK_POST_ID" * @hibernate.collection-key-column name="FK_POST_ID" * not-null="false" * index="Post_ValdiatorId_idx" * @hibernate.collection-one-to-many class="no.abmu.finances.domain.PostValidator" */ @OneToMany(cascade = { CascadeType.ALL }) @JoinColumn(name = "post_fk") // @Cache(usage = CacheConcurrencyStrategy.READ_ONLY) // @ForeignKey(name = "FK_POST_PVALIDATOR_ID", inverseName = "ID") // @JoinTable( // schema = "QUESTIONNARIE" // ) public Set<PostValidator> getValidators() { return validators; } public void addValidator(PostValidator validator) { validators.add(validator); } /** * @hibernate.many-to-one column="FK_POSTPERIOD_ID" * class="no.abmu.finances.domain.PostPeriod" * cascade="save-update" * @hibernate.column name="FK_POSTPERIOD_ID" * index="rdoum_FK_POSTPERIOD_ID_idx" */ @ManyToOne(cascade = { CascadeType.ALL }) @ForeignKey(name = "FK_POSTPERIOD_ID") // @Cache(usage = CacheConcurrencyStrategy.READ_ONLY) public PostPeriod getPeriod() { return period; } /** * @hibernate.many-to-one column="FK_POSTTYPE_ID" * class="no.abmu.finances.domain.PostType" * cascade="save-update" * @hibernate.column name="FK_POSTTYPE_ID" * index="rdoum_FK_POSTPERIOD_ID_idx" */ @ManyToOne(cascade = { CascadeType.ALL }) @ForeignKey(name = "FK_POSTTYPE_ID", inverseName = "FK_POSTTYPE_ID") // @Cache(usage = CacheConcurrencyStrategy.READ_ONLY) public PostType getPostType() { return postType; } private void setId(Long id) { this.id = id; } private void setCode(String code) { this.code = code; } public void setName(String name) { this.name = name; } private void setAccounts(Set accounts) { this.accounts = accounts; } private void setValidators(Set validators) { this.validators = validators; } public void setPeriod(PostPeriod period) { this.period = period; } public void setPostType(PostType postType) { this.postType = postType; } public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } final Post that = (Post) o; if (code != that.code) { return false; } if (id != null ? !id.equals(that.id) : that.id != null) { return false; } if (!name.equals(that.name)) { return false; } return true; } public int hashCode() { int result; result = (code != null ? code.hashCode() : 0); result = 29 * result + (name != null ? name.hashCode() : 0); return result; } /** * @hidden */ static public class Factory { public static String INT_POST_TYPE = IntPost.class.getName(); public static String BOOL_POST_TYPE = BoolPost.class.getName(); public static String STRING_POST_TYPE = StringPost.class.getName(); public static String DATE_POST_TYPE = DatePost.class.getName(); public static Post newInstance(String type, String name, String code) { String className = type; try { Post post = (Post) Class.forName(className).newInstance(); post.setName(name); post.setCode(code); return post; } catch (InstantiationException e) { String message = "Exception when creating class " + className; logger.error(message, e.getCause()); throw new RuntimeException(message, e.getCause()); } catch (IllegalAccessException e) { String message = "The class " + className + " did not have a public empty contructor"; logger.error(message, e.getCause()); throw new RuntimeException(message, e.getCause()); } catch (ClassNotFoundException e) { String message = "The class " + className + " given in for creating a Post does not exist, " + "is it implemented?"; logger.error(message, e); throw new RuntimeException(message, e); } } } }