SVN layout migration for core/branches/Branch_3_2

git-svn-id: https://svn.jboss.org/repos/hibernate/core/branches/Branch_3_2@11760 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Steve Ebersole 2007-06-29 19:51:10 +00:00
parent 378d7aa872
commit 6b1bb7ca35
11 changed files with 846 additions and 0 deletions

View File

@ -0,0 +1,37 @@
//$Id$
package org.hibernate.auction;
import java.util.Date;
/**
* @author Gavin King
*/
public class AuctionInfo {
private long id;
private String description;
private Date ends;
private Float maxAmount;
public String getDescription() {
return description;
}
public Date getEnds() {
return ends;
}
public long getId() {
return id;
}
public Float getMaxAmount() {
return maxAmount;
}
public AuctionInfo(long id, String description, Date ends, Float maxAmount) {
this.id = id;
this.description = description;
this.ends = ends;
this.maxAmount = maxAmount;
}
}

View File

@ -0,0 +1,42 @@
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
package="org.hibernate.auction">
<import class="AuctionInfo"/>
<class name="AuctionItem">
<comment>An item that is being auctioned.</comment>
<id name="id">
<generator class="native"/>
</id>
<natural-id>
<many-to-one name="seller"/>
<property name="shortDescription"
length="200"/>
</natural-id>
<property name="description"
length="1000"/>
<property name="ends"/>
<property name="condition" column="`CONDITION`"/>
<many-to-one name="successfulBid"
outer-join="false"/>
<bag name="bids"
inverse="true"
cascade="all">
<key column="item"/>
<one-to-many class="Bid"/>
</bag>
</class>
</hibernate-mapping>

View File

@ -0,0 +1,78 @@
//$Id$
package org.hibernate.auction;
import java.util.Date;
import java.util.List;
/**
* @author Gavin King
*/
public class AuctionItem extends Persistent {
private String description;
private String shortDescription;
private List bids;
private Bid successfulBid;
private User seller;
private Date ends;
private int condition;
public List getBids() {
return bids;
}
public String getDescription() {
return description;
}
public User getSeller() {
return seller;
}
public Bid getSuccessfulBid() {
return successfulBid;
}
public void setBids(List bids) {
this.bids = bids;
}
public void setDescription(String string) {
description = string;
}
public void setSeller(User user) {
seller = user;
}
public void setSuccessfulBid(Bid bid) {
successfulBid = bid;
}
public Date getEnds() {
return ends;
}
public void setEnds(Date date) {
ends = date;
}
public int getCondition() {
return condition;
}
public void setCondition(int i) {
condition = i;
}
public String toString() {
return shortDescription + " (" + description + ": " + condition + "/10)";
}
public String getShortDescription() {
return shortDescription;
}
public void setShortDescription(String shortDescription) {
this.shortDescription = shortDescription;
}
}

View File

@ -0,0 +1,39 @@
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
package="org.hibernate.auction">
<class name="Bid"
discriminator-value="N">
<comment>A bid or "buy now" for an item.</comment>
<id name="id">
<generator class="native"/>
</id>
<discriminator type="char">
<column name="isBuyNow">
<comment>Y if a "buy now", N if a regular bid.</comment>
</column>
</discriminator>
<natural-id>
<many-to-one name="item"/>
<property name="amount"/>
</natural-id>
<property name="datetime"
not-null="true"
column="`datetime`"/>
<many-to-one name="bidder"
not-null="true"/>
<subclass name="BuyNow"
discriminator-value="Y"/>
</class>
</hibernate-mapping>

View File

@ -0,0 +1,55 @@
//$Id$
package org.hibernate.auction;
import java.util.Date;
/**
* @author Gavin King
*/
public class Bid extends Persistent {
private AuctionItem item;
private float amount;
private Date datetime;
private User bidder;
public AuctionItem getItem() {
return item;
}
public void setItem(AuctionItem item) {
this.item = item;
}
public float getAmount() {
return amount;
}
public Date getDatetime() {
return datetime;
}
public void setAmount(float f) {
amount = f;
}
public void setDatetime(Date date) {
datetime = date;
}
public User getBidder() {
return bidder;
}
public void setBidder(User user) {
bidder = user;
}
public String toString() {
return bidder.getUserName() + " $" + amount;
}
public boolean isBuyNow() {
return false;
}
}

View File

@ -0,0 +1,14 @@
//$Id$
package org.hibernate.auction;
/**
* @author Gavin King
*/
public class BuyNow extends Bid {
public boolean isBuyNow() {
return true;
}
public String toString() {
return super.toString() + " (buy now)";
}
}

View File

@ -0,0 +1,390 @@
//$Id$
package org.hibernate.auction;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import org.hibernate.FetchMode;
import org.hibernate.FlushMode;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.MatchMode;
/**
* Demonstrate some useful features of Hibernate.
*
* @author Gavin King
*/
public class Main {
private SessionFactory factory;
/**
* Demonstrates HQL projection/aggregation
*/
public void viewAllAuctionsFast() throws Exception {
System.out.println("Viewing all auction item info");
Session s = factory.openSession();
Transaction tx=null;
try {
tx = s.beginTransaction();
List auctions = s.createQuery(
"select new AuctionInfo( item.id, item.description, item.ends, max(bid.amount) ) "
+ "from AuctionItem item "
+ "left join item.bids bid "
+ "group by item.id, item.description, item.ends "
+ "order by item.ends desc"
)
.setMaxResults(100)
.list();
Iterator iter = auctions.iterator();
while ( iter.hasNext() ) {
AuctionInfo ai = (AuctionInfo) iter.next();
System.out.println(
"Auction: " + ai.getId() + " - " + ai.getDescription() +
", ends: " + ai.getEnds() +
", highest bid: " + ai.getMaxAmount()
);
}
System.out.println();
tx.commit();
}
catch (Exception e) {
if (tx!=null) tx.rollback();
throw e;
}
finally {
s.close();
}
}
/**
* Demonstrates HQL with runtime fetch strategy
*/
public void viewAllAuctionsSlow() throws Exception {
System.out.println("Viewing all auction item objects");
Session s = factory.openSession();
Transaction tx=null;
try {
s.setFlushMode(FlushMode.NEVER); //entirely optional!!
tx = s.beginTransaction();
List auctions = s.createQuery(
"from AuctionItem item "
+ "left join fetch item.bids bid left join fetch bid.bidder "
+ "order by item.ends desc"
)
.setMaxResults(100)
.list();
Iterator iter = new HashSet(auctions).iterator();
while ( iter.hasNext() ) {
AuctionItem auction = (AuctionItem) iter.next();
System.out.println(
"Auction: " + auction.getId() + " - " + auction.getDescription() +
", ends: " + auction.getEnds() +
", bids: " + auction.getBids()
);
}
System.out.println();
tx.commit();
}
catch (Exception e) {
if (tx!=null) tx.rollback();
throw e;
}
finally {
s.close();
}
}
/**
* Demonstrates transitive persistence with detached object support
*/
public void bidOnAuction(User bidder, AuctionItem item, float amount) throws Exception {
System.out.println("Creating a new bid for auction item: " + item.getId() + " by user: " + bidder.getId() );
Session s = factory.openSession();
Transaction tx=null;
try {
tx = s.beginTransaction();
s.lock(item, LockMode.NONE);
s.lock(bidder, LockMode.NONE);
Bid bid = new Bid();
bid.setBidder(bidder);
bid.setDatetime( new Date() );
bid.setAmount(amount);
bid.setItem(item);
bidder.getBids().add(bid);
item.getBids().add(bid);
tx.commit();
}
catch (Exception e) {
if (tx!=null) tx.rollback();
throw e;
}
finally {
s.close();
}
}
/**
* Demonstrates detached object support
*/
public void changeUserDetails(User user) throws Exception {
System.out.println("Changing user details for: " + user.getId() );
Session s = factory.openSession();
Transaction tx=null;
try {
tx = s.beginTransaction();
s.merge(user);
tx.commit();
}
catch (Exception e) {
if (tx!=null) tx.rollback();
throw e;
}
finally {
s.close();
}
}
/**
* Demonstrates automatic dirty checking
*/
public void changeItemDescription(Long itemId, String description) throws Exception {
System.out.println("Changing auction item description for: " + itemId );
Session s = factory.openSession();
Transaction tx=null;
try {
tx = s.beginTransaction();
AuctionItem item = (AuctionItem) s.get(AuctionItem.class, itemId);
if (item==null) throw new IllegalArgumentException("No item for the given id: " + itemId);
item.setDescription(description);
tx.commit();
}
catch (Exception e) {
if (tx!=null) tx.rollback();
throw e;
}
finally {
s.close();
}
}
/**
* Demonstrates query by criteria with runtime fetch strategy
*/
public void viewUserAuctions(Long sellerId) throws Exception {
System.out.println("Viewing user and auctions: " + sellerId);
Session s = factory.openSession();
Transaction tx=null;
try {
tx = s.beginTransaction();
List list = s.createCriteria(User.class)
.add( Expression.eq("id", sellerId) )
.setFetchMode("auctions", FetchMode.JOIN)
.list();
if (list.size()==0) throw new IllegalArgumentException("No user for the given id: " + sellerId);
User user = (User) list.get(0);
System.out.println(
"User: " + user.getId() + " - " + user.getName() +
", email: " + user.getEmail() +
", auctions: " + user.getAuctions()
);
tx.commit();
}
catch (Exception e) {
if (tx!=null) tx.rollback();
throw e;
}
finally {
s.close();
}
}
/**
* Demonstrates query by example
*/
public void viewAuctionsByDescription(String description, int condition) throws Exception {
String msg = "Viewing auctions containing: " + description;
if (condition>0) msg += " with condition: " + condition + "/10";
AuctionItem item = new AuctionItem();
item.setDescription(description);
item.setCondition(condition);
Session s = factory.openSession();
Transaction tx=null;
try {
tx = s.beginTransaction();
Iterator iter = s.createCriteria(AuctionItem.class)
.add( Example.create(item)
.enableLike(MatchMode.ANYWHERE)
.ignoreCase()
.excludeZeroes()
)
.list()
.iterator();
System.out.println(msg);
while ( iter.hasNext() ) {
item = (AuctionItem) iter.next();
System.out.println("Item: " + item.getId() + " - " + item.getDescription() );
}
System.out.println();
tx.commit();
}
catch (Exception e) {
if (tx!=null) tx.rollback();
throw e;
}
finally {
s.close();
}
}
/**
* Demonstrates transitive persistence
*/
public void createTestAuctions() throws Exception {
System.out.println("Setting up some test data");
Session s = factory.openSession();
Transaction tx = s.beginTransaction();
User seller = new User();
seller.setUserName("xam");
seller.setName( new Name("Max", new Character('R'), "Andersen") );
seller.setEmail("max@hibernate.org");
seller.setPassword("******");
seller.setAuctions( new ArrayList() );
s.save(seller);
User bidder1 = new User();
bidder1.setUserName("1E1");
bidder1.setName( new Name( "Gavin", new Character('A'), "King") );
bidder1.setEmail("gavin@hibernate.org");
bidder1.setPassword("******");
bidder1.setBids( new ArrayList() );
s.save(bidder1);
User bidder2 = new User();
bidder2.setUserName("steve");
bidder2.setName( new Name("Steve", null, "Ebersole") );
bidder2.setEmail("steve@hibernate.org");
bidder2.setPassword("******");
bidder2.setBids( new ArrayList() );
s.save(bidder2);
for ( int i=0; i<3; i++ ) {
AuctionItem item = new AuctionItem();
item.setShortDescription("Auction " + i);
item.setDescription("the auction item number " + i);
item.setEnds( new Date() );
item.setBids( new ArrayList() );
item.setSeller(seller);
item.setCondition(i*3 + 2);
for ( int j=0; j<i; j++ ) {
Bid bid = new Bid();
bid.setBidder(bidder1);
bid.setAmount(j);
bid.setDatetime( new Date() );
bid.setItem(item);
item.getBids().add(bid);
bidder1.getBids().add(bid);
Bid bid2 = new Bid();
bid2.setBidder(bidder2);
bid2.setAmount( j + 0.5f);
bid2.setDatetime( new Date() );
bid2.setItem(item);
item.getBids().add(bid2);
bidder2.getBids().add(bid2);
}
seller.getAuctions().add(item);
mainItem = item;
}
mainBidder = bidder2;
mainSeller = seller;
BuyNow buyNow = new BuyNow();
buyNow.setAmount(1.2f);
buyNow.setDatetime( new Date() );
buyNow.setBidder(mainBidder);
buyNow.setItem(mainItem);
mainBidder.getBids().add(buyNow);
mainItem.getBids().add(buyNow);
tx.commit();
s.close();
}
static AuctionItem mainItem;
static User mainBidder;
static User mainSeller;
public static void main(String[] args) throws Exception {
final Main test = new Main();
Configuration cfg = new Configuration()
.addClass(AuctionItem.class)
.addClass(Bid.class)
.addClass(User.class)
.setProperty(Environment.HBM2DDL_AUTO, "create");
//cfg.setProperty("hibernate.show_sql", "true");
test.factory = cfg.buildSessionFactory();
test.createTestAuctions();
test.viewAllAuctionsSlow();
test.viewAllAuctionsFast();
test.bidOnAuction(mainBidder, mainItem, 5.5f);
test.viewAllAuctionsFast();
test.viewUserAuctions( mainSeller.getId() );
mainSeller.setEmail("max@jboss.org");
test.changeUserDetails(mainSeller);
test.changeItemDescription(mainItem.getId(), "new description");
test.viewUserAuctions( mainSeller.getId() );
test.viewAuctionsByDescription("It", 0);
test.viewAuctionsByDescription("DESC", 3);
test.viewAuctionsByDescription("DESC", 8);
test.factory.close();
}
}

View File

@ -0,0 +1,51 @@
//$Id$
package org.hibernate.auction;
/**
* @author Gavin King
*/
public class Name {
private String firstName;
private String lastName;
private Character initial;
private Name() {}
public Name(String first, Character middle, String last) {
firstName = first;
initial = middle;
lastName = last;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public Character getInitial() {
return initial;
}
public void setInitial(Character initial) {
this.initial = initial;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String toString() {
StringBuffer buf = new StringBuffer()
.append(firstName)
.append(' ');
if (initial!=null) buf.append(initial)
.append(' ');
return buf.append(lastName)
.toString();
}
}

View File

@ -0,0 +1,17 @@
//$Id$
package org.hibernate.auction;
/**
* @author Gavin King
*/
public class Persistent {
private Long id;
public Long getId() {
return id;
}
public void setId(Long long1) {
id = long1;
}
}

View File

@ -0,0 +1,54 @@
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
package="org.hibernate.auction">
<class name="User" table="AuctionUser" lazy="true">
<comment>Users may bid for or sell auction items.</comment>
<id name="id">
<generator class="native"/>
</id>
<natural-id mutable="true">
<property name="userName"
length="10"/>
</natural-id>
<property name="password"
not-null="true"
length="15"
column="`password`"/>
<property name="email"/>
<component name="name">
<property name="firstName"
length="50"
not-null="true"/>
<property name="initial"
column="`initial`"/>
<property name="lastName"
length="50"
not-null="true"/>
</component>
<bag name="bids"
inverse="true"
cascade="save-update,lock">
<key column="bidder"/>
<one-to-many class="Bid"/>
</bag>
<bag name="auctions"
inverse="true"
cascade="save-update,lock">
<key column="seller"/>
<one-to-many class="AuctionItem"/>
</bag>
</class>
</hibernate-mapping>

View File

@ -0,0 +1,69 @@
//$Id$
package org.hibernate.auction;
import java.util.List;
/**
* @author Gavin King
*/
public class User extends Persistent {
private String userName;
private String password;
private String email;
private Name name;
private List bids;
private List auctions;
public String getEmail() {
return email;
}
public String getPassword() {
return password;
}
public String getUserName() {
return userName;
}
public void setEmail(String string) {
email = string;
}
public void setPassword(String string) {
password = string;
}
public void setUserName(String string) {
userName = string;
}
public List getAuctions() {
return auctions;
}
public List getBids() {
return bids;
}
public void setAuctions(List list) {
auctions = list;
}
public void setBids(List list) {
bids = list;
}
public String toString() {
return userName;
}
public Name getName() {
return name;
}
public void setName(Name name) {
this.name = name;
}
}