Wednesday, April 22, 2009

How to Use Hibernate Annotation

1. applicationContext.xml
    <bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.aais.model.Transaction</value>
<value>com.aais.model.Account</value>
<value>com.aais.model.Security</value>
<value>com.aais.model.TradeType</value>
<value>com.aais.model.JournalEntry</value>
<value>com.aais.model.SecurityType</value>
</list>
</property>
...


2. SecurityType.java
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.NamedQueries;
import org.hibernate.annotations.NamedQuery;

/**
* SecurityType entity.
*/
@SuppressWarnings("serial")
@Entity
@Table(name = "security_type")
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
@NamedQueries({
@NamedQuery(name = "SecurityType.FindAll", query = "from SecurityType order by securityTypeId asc") ,
@NamedQuery(name = "SecurityType.FindById", query = "from SecurityType where securityTypeId=:securityTypeId")
})
public class SecurityType implements java.io.Serializable {

// Fields

private Integer securityTypeId;
private String name;

// Constructors

/** default constructor */
public SecurityType() {
}

// Property accessors
@Id
@Column(name = "security_type_id", unique = true, nullable = false)
public Integer getSecurityTypeId() {
return this.securityTypeId;
}

public void setSecurityTypeId(Integer securityTypeId) {
this.securityTypeId = securityTypeId;
}

@Column(name = "name", length = 128)
public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((securityTypeId == null) ? 0 : securityTypeId.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
SecurityType other = (SecurityType) obj;
if (securityTypeId == null) {
if (other.securityTypeId != null)
return false;
} else if (!securityTypeId.equals(other.securityTypeId))
return false;
return true;
}

}


3. Security.java
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/**
* Security entity.
*/
@SuppressWarnings("serial")
@Entity
@Table(name = "security")
public class Security implements java.io.Serializable {

// Fields

private Integer securityId;
private SecurityType securityType;
private String ticker;

// Constructors

/** default constructor */
public Security() {
}

// Property accessors
@Id
@Column(name = "security_id", unique = true, nullable = false)
public Integer getSecurityId() {
return this.securityId;
}

public void setSecurityId(Integer securityId) {
this.securityId = securityId;
}

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "security_type_id")
public SecurityType getSecurityType() {
return this.securityType;
}

public void setSecurityType(SecurityType securityType) {
this.securityType = securityType;
}

@Column(name = "ticker", length = 128)
public String getTicker() {
return this.ticker;
}

public void setTicker(String ticker) {
this.ticker = ticker;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((securityId == null) ? 0 : securityId.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Security other = (Security) obj;
if (securityId == null) {
if (other.securityId != null)
return false;
} else if (!securityId.equals(other.securityId))
return false;
return true;
}

}


4. JournalEntry.java
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/**
* JournalEntry entity.
*/
@SuppressWarnings("serial")
@Entity
@Table(name = "journal_entry")
public class JournalEntry implements java.io.Serializable {

// Fields

private Integer journalEntryId;
private Transaction transaction;
private Account account;
private Double debit;
private Double credit;

// Constructors

/** default constructor */
public JournalEntry() {
}

// Property accessors
@Id
@Column(name = "journal_entry_id", unique = true, nullable = false)
public Integer getJournalEntryId() {
return this.journalEntryId;
}

public void setJournalEntryId(Integer journalEntryId) {
this.journalEntryId = journalEntryId;
}

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "transaction_id")
public Transaction getTransaction() {
return this.transaction;
}

public void setTransaction(Transaction transaction) {
this.transaction = transaction;
}

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "account_id")
public Account getAccount() {
return this.account;
}

public void setAccount(Account account) {
this.account = account;
}

@Column(name = "debit", precision = 18, scale = 4)
public Double getDebit() {
return this.debit;
}

public void setDebit(Double debit) {
this.debit = debit;
}

@Column(name = "credit", precision = 18, scale = 4)
public Double getCredit() {
return this.credit;
}

public void setCredit(Double credit) {
this.credit = credit;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((journalEntryId == null) ? 0 : journalEntryId.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
JournalEntry other = (JournalEntry) obj;
if (journalEntryId == null) {
if (other.journalEntryId != null)
return false;
} else if (!journalEntryId.equals(other.journalEntryId))
return false;
return true;
}

}


5. Transaction.java
import java.util.HashSet;
import java.util.Set;
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.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

/**
* Transaction entity.
*/
@SuppressWarnings("serial")
@Entity
@Table(name = "transaction")
public class Transaction implements java.io.Serializable {

// Fields

private Integer transactionId;
private Security security;
private TradeType tradeType;
private Double price;
private Integer quantity;
private Set<JournalEntry> journalEntries = new HashSet<JournalEntry>(0);

// Constructors

/** default constructor */
public Transaction() {
}

// Property accessors
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "transaction_id", unique = true, nullable = false)
public Integer getTransactionId() {
return this.transactionId;
}

public void setTransactionId(Integer transactionId) {
this.transactionId = transactionId;
}

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "security_id")
public Security getSecurity() {
return this.security;
}

public void setSecurity(Security security) {
this.security = security;
}

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "trade_type_id")
public TradeType getTradeType() {
return this.tradeType;
}

public void setTradeType(TradeType tradeType) {
this.tradeType = tradeType;
}

@Column(name = "price", precision = 8, scale = 4)
public Double getPrice() {
return this.price;
}

public void setPrice(Double price) {
this.price = price;
}

@Column(name = "quantity")
public Integer getQuantity() {
return this.quantity;
}

public void setQuantity(Integer quantity) {
this.quantity = quantity;
}

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "transaction")
@OrderBy("journalEntryId asc")
public Set<JournalEntry> getJournalEntries() {
return this.journalEntries;
}

public void setJournalEntries(Set<JournalEntry> journalEntries) {
this.journalEntries = journalEntries;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((transactionId == null) ? 0 : transactionId.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Transaction other = (Transaction) obj;
if (transactionId == null) {
if (other.transactionId != null)
return false;
} else if (!transactionId.equals(other.transactionId))
return false;
return true;
}

}

No comments:

Post a Comment