[HHH-4848]

added an example that derive from entities not defined with component key


git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18731 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Ståle W. Pedersen 2010-02-08 18:44:26 +00:00
parent 3d911bd658
commit 817fe52a55
5 changed files with 570 additions and 0 deletions

View File

@ -0,0 +1,217 @@
package org.hibernate.test.annotations.derivedidentities.complex;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;
@SuppressWarnings("serial")
@NamedQueries({
@NamedQuery(name=Customer.QUERY_ALL,
query="select a from Customer a"),
@NamedQuery(name=Customer.QUERY_COUNT,
query="select COUNT(a) from Customer a"),
@NamedQuery(name=Customer.QUERY_BY_CREDIT,
query="SELECT c.id FROM Customer c WHERE c.creditLimit > :limit")
})
@Entity
@Table(name="O_CUSTOMER")
public class Customer implements Serializable {
public static final String QUERY_ALL = "Customer.selectAll";
public static final String QUERY_COUNT = "Customer.count";
public static final String QUERY_BY_CREDIT = "Customer.selectByCreditLimit";
public static final String BAD_CREDIT = "BC";
@Id
@Column(name="C_ID")
private int id;
@Column(name="C_FIRST")
private String firstName;
@Column(name="C_LAST")
private String lastName;
@Column(name="C_CONTACT")
private String contact;
@Column(name="C_CREDIT")
private String credit;
@Column(name="C_CREDIT_LIMIT")
private BigDecimal creditLimit;
@Column(name="C_SINCE")
@Temporal(TemporalType.DATE)
private Calendar since;
@Column(name="C_BALANCE")
private BigDecimal balance;
@Column(name="C_YTD_PAYMENT")
private BigDecimal ytdPayment;
@OneToMany(targetEntity=CustomerInventory.class, mappedBy="customer", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
private List<CustomerInventory> customerInventories;
@Version
@Column(name = "C_VERSION")
private int version;
protected Customer() {
}
public Customer(String first, String last,
String contact, String credit, BigDecimal creditLimit,
BigDecimal balance, BigDecimal YtdPayment) {
this.firstName = first;
this.lastName = last;
this.contact = contact;
this.since = Calendar.getInstance();
this.credit = credit;
this.creditLimit = creditLimit;
this.balance = balance;
this.ytdPayment = YtdPayment;
}
public Integer getId() {
return id;
}
public void setId(Integer customerId) {
this.id = customerId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
public String getCredit() {
return credit;
}
public void setCredit(String credit) {
this.credit = credit;
}
public BigDecimal getCreditLimit() {
return creditLimit;
}
public void setCreditLimit(BigDecimal creditLimit) {
this.creditLimit = creditLimit;
}
public Calendar getSince() {
return since;
}
public void setSince(Calendar since) {
this.since = since;
}
public BigDecimal getBalance() {
return balance;
}
public void setBalance(BigDecimal balance) {
this.balance = balance;
}
public void changeBalance(BigDecimal change) {
setBalance(balance.add(change).setScale(2, BigDecimal.ROUND_DOWN));
}
public BigDecimal getYtdPayment() {
return ytdPayment;
}
public void setYtdPayment(BigDecimal ytdPayment) {
this.ytdPayment = ytdPayment;
}
public List<CustomerInventory> getInventories() {
if (customerInventories == null){
customerInventories = new ArrayList<CustomerInventory>();
}
return customerInventories;
}
public CustomerInventory addInventory(Item item, int quantity,
BigDecimal totalValue) {
CustomerInventory inventory = new CustomerInventory(this, item,
quantity, totalValue);
getInventories().add(inventory);
return inventory;
}
public int getVersion() {
return version;
}
public boolean hasSufficientCredit(BigDecimal amount) {
return !BAD_CREDIT.equals(getCredit())
&& creditLimit != null
&& creditLimit.compareTo(amount) >= 0;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
return id == ((Customer) o).id;
}
@Override
public int hashCode() {
return new Integer(id).hashCode();
}
@Override
public String toString() {
return this.getFirstName() + " " + this.getLastName();
}
}

View File

@ -0,0 +1,118 @@
package org.hibernate.test.annotations.derivedidentities.complex;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Comparator;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.Version;
@NamedQueries({
@NamedQuery(name="CustomerInventory.selectAll",
query="select a from CustomerInventory a")
})
@SuppressWarnings("serial")
@Entity
@Table(name="O_CUSTINVENTORY")
@IdClass(CustomerInventoryPK.class)
public class CustomerInventory implements Serializable, Comparator<CustomerInventory> {
@Id
@TableGenerator(name="inventory",
table="U_SEQUENCES",
pkColumnName="S_ID",
valueColumnName="S_NEXTNUM",
pkColumnValue="inventory",
allocationSize=1000)
@GeneratedValue(strategy=GenerationType.TABLE,generator="inventory")
@Column(name="CI_ID")
private Integer id;
@Id
@ManyToOne(cascade=CascadeType.MERGE)
@JoinColumn(name="CI_CUSTOMERID")
private Customer customer;
@ManyToOne(cascade=CascadeType.MERGE)
@JoinColumn(name = "CI_ITEMID")
private Item vehicle;
@Column(name="CI_VALUE")
private BigDecimal totalCost;
@Column(name="CI_QUANTITY")
private int quantity;
@Version
@Column(name = "CI_VERSION")
private int version;
protected CustomerInventory() {
}
CustomerInventory(Customer customer, Item vehicle, int quantity , BigDecimal totalValue)
{
this.customer = customer;
this.vehicle = vehicle;
this.quantity = quantity;
this.totalCost = totalValue;
}
public Item getVehicle() {
return vehicle;
}
public BigDecimal getTotalCost() {
return totalCost;
}
public int getQuantity() {
return quantity;
}
public Integer getId() {
return id;
}
public Customer getCustomer() {
return customer;
}
public int getVersion() {
return version;
}
public int compare(CustomerInventory cdb1, CustomerInventory cdb2) {
return cdb1.id.compareTo(cdb2.id);
}
@Override
public boolean equals (Object obj) {
if (obj == this)
return true;
if (obj == null || !(obj instanceof CustomerInventory))
return false;
if (this.id == ((CustomerInventory)obj).id)
return true;
if (this.id != null && ((CustomerInventory)obj).id == null)
return false;
if (this.id == null && ((CustomerInventory)obj).id != null)
return false;
return this.id.equals(((CustomerInventory)obj).id);
}
}

View File

@ -0,0 +1,45 @@
package org.hibernate.test.annotations.derivedidentities.complex;
import java.io.Serializable;
public class CustomerInventoryPK implements Serializable {
private Integer id;
private Customer customer;
public CustomerInventoryPK() {
}
public CustomerInventoryPK(Integer id, Customer customer) {
this.id = id;
this.customer = customer;
}
public boolean equals(Object other) {
if (other == this) {
return true;
}
if (other == null || getClass() != other.getClass()) {
return false;
}
CustomerInventoryPK cip = (CustomerInventoryPK) other;
return (getCustomer().getId() == cip.getCustomer().getId() && (id == cip.id ||
( id != null && id.equals(cip.id))));
}
public int hashCode() {
return (id == null ? 0 : id.hashCode()) ^ getCustomer().getId();
}
public Integer getId() {
return id;
}
public Customer getCustomer() {
return customer;
}
}

View File

@ -0,0 +1,70 @@
package org.hibernate.test.annotations.derivedidentities.complex;
import java.math.BigDecimal;
import java.util.List;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.test.annotations.TestCase;
import org.hibernate.junit.FailureExpected;
/**
* A test.
*
* @author <a href="mailto:stale.pedersen@jboss.org">Stale W. Pedersen</a>
* @version $Revision: 1.1 $
*/
public class IdClassGeneratedValueManyToOneTest extends TestCase
{
@FailureExpected(jiraKey="HHH-4848")
public void testComplexIdClass()
{
Logger.getLogger("org.hibernate").setLevel(Level.TRACE);
Session s = openSession();
Transaction tx = s.beginTransaction();
Customer c1 = new Customer("foo", "bar", "contact1", "100", new BigDecimal(1000),new BigDecimal(1000), new BigDecimal(1000));
s.persist(c1);
Item boat = new Item();
boat.setId("1");
boat.setName("cruiser");
boat.setPrice(new BigDecimal(500));
boat.setDescription("a boat");
boat.setCategory(42);
s.persist(boat);
s.flush();
s.clear();
c1.addInventory(boat, 10, new BigDecimal(5000));
s.merge(c1);
s.flush();
s.clear();
Customer c2 = (Customer) s.createQuery( "select c from Customer c" ).uniqueResult();
List<CustomerInventory> inventory = c2.getInventories();
assertEquals(1, inventory.size());
assertEquals(10, inventory.get(0).getQuantity());
tx.rollback();
s.close();
assertTrue(true);
}
protected Class[] getAnnotatedClasses()
{
return new Class[] {
Customer.class,
CustomerInventory.class,
CustomerInventoryPK.class,
Item.class
};
}
}

View File

@ -0,0 +1,120 @@
package org.hibernate.test.annotations.derivedidentities.complex;
import java.io.Serializable;
import java.math.BigDecimal;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Version;
@NamedQueries( {
@NamedQuery(name = "Item.findByCategory",
query = "SELECT i FROM Item i WHERE i.category=:category ORDER BY i.id")})
@SuppressWarnings("serial")
@Entity
@Table(name = "O_ITEM")
public class Item implements Serializable
{
public static final String QUERY_BY_CATEGORY = "Item.findByCategory";
@Id
@Column(name = "I_ID")
private String id;
@Column(name = "I_NAME")
private String name;
@Column(name = "I_PRICE")
private BigDecimal price;
@Column(name = "I_DESC")
private String description;
@Column(name = "I_DISCOUNT")
private BigDecimal discount;
@Column(name = "I_CATEGORY")
private int category;
@Version
@Column(name = "I_VERSION")
int version;
public String getId() {
return id;
}
public void setId(String i) {
id = i;
}
public int getCategory() {
return category;
}
public void setCategory(int category) {
this.category = category;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public BigDecimal getDiscount() {
return discount;
}
public void setDiscount(BigDecimal discount) {
if (discount.doubleValue() < 0 || discount.doubleValue() > 100.0)
throw new IllegalArgumentException(this + " discount " + discount
+ " is invalid. Must be between 0.0 and 100.0");
this.discount = discount;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public int getVersion() {
return version;
}
@Override
public boolean equals(Object other) {
if (other == null || other.getClass() != this.getClass()) {
return false;
}
if (other == this) {
return true;
}
return id.equals(((Item) other).id);
}
@Override
public int hashCode() {
return id.hashCode();
}
}