dcsimg
EJB: which object has to be persist?
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   stefano_T
Posted On:   Sunday, June 7, 2009 07:44 AM

Hi all. I've a conceptual problem about the using of EJB. this is the entity Orders public class Orders implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.AUTO, generator = "ordersgenerator") @Column(name = "idorder") private Integer idorder; @Column(name = "datetime") @Temporal(TemporalType.TIMESTAMP) private Date datetime; @Column(name = "total") private Double total; @JoinColumn(name = "idcustomer", referencedColumnName = "idcustomer") @ManyToOne(fetch = FetchType.EAGER) pr   More>>

Hi all.
I've a conceptual problem about the using of EJB.
this is the entity

Orders

			

public class Orders implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "ordersgenerator")
@Column(name = "idorder")
private Integer idorder;
@Column(name = "datetime")
@Temporal(TemporalType.TIMESTAMP)
private Date datetime;
@Column(name = "total")
private Double total;
@JoinColumn(name = "idcustomer", referencedColumnName = "idcustomer")
@ManyToOne(fetch = FetchType.EAGER)
private Customer idcustomer;
@OneToMany(mappedBy = "idorder", fetch = FetchType.EAGER)
private Collection boughtCollection;



Bought
			

public class Bought implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "boughtgenerator")
@Column(name = "idbought")
private Integer idbought;
@Column(name = "quantity")
private Integer quantity;
@JoinColumn(name = "iditem", referencedColumnName = "iditem")
@ManyToOne(fetch = FetchType.EAGER)
private Item iditem;
@JoinColumn(name = "idorder", referencedColumnName = "idorder")
@ManyToOne
private Orders idorder;
....



Item
			

@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "itemgenerator")
@Column(name = "iditem")
private Integer iditem;
@Column(name = "name")
private String name;
@Column(name = "description")
private String description;
@Column(name = "price")
private Double price;
@Column(name = "image")
private String image;
@Column(name = "stock")
private Integer stock;
@JoinColumn(name = "idcategory", referencedColumnName = "idcategory")
@ManyToOne(fetch = FetchType.EAGER)
private Category idcategory;
@OneToMany(mappedBy = "iditem")
private Collection boughtCollection;




Customer
			

public class Customer implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "customergenerator")
@Column(name = "idcustomer")
private Integer idcustomer;
@Column(name = "firstname")
private String firstname;
@Column(name = "lastname")
private String lastname;
@Column(name = "address")
private String address;
@Column(name = "username")
private String username;
@Column(name = "password")
private String password;
@Column(name = "money")
private Double money;
@OneToMany(mappedBy = "idcustomer")
private Collection ordersCollection;



if is not understandably is a shop, when a user complete the shop i need to store the order, the order contains the bought item list and the user that is the proprietary of this order.
the bought item contains a reference to item and quantity.

so, how can i do the persist?

i've to construct the order class putting all the information inside this class (user, item, etc) and persist only the order?
if i do in this way and if i decrement the stock of the item, the stock is updatet in the db?

because at the moment i've done this (in a statful ejb):

Customer
			
public boolean completeShop() {
boolean ret = true;
//creater the order
Orders order = new Orders();
//put customer and date
order.setIdcustomer(customer);
order.setDatetime(new Date());
Vector v = new Vector(items.keySet());
ArrayList boughts = new ArrayList ();
double money = getCurrentMoneyRemains();
double total=0.0;
for (Enumeration e = v.elements(); e.hasMoreElements();) {
Item item = (Item) e.nextElement();
int quan = (Integer) items.get(item);
//check the availabel quantity
if (item.getStock() >= quan) {
//decrement the stock value
item.setStock(item.getStock() - quan);
//merge the item
em.merge(item);
Bought bought = new Bought();
bought.setIditem(item);
bought.setQuantity(quan);
bought.setIdorder(order);
//create and merge the bought item
em.persist(bought);
boughts.add(bought);
total+=item.getPrice()*quan;

} else {
ret = false;
System.out.println("out of order");
ctx.setRollbackOnly();
}
}
if (ret) {
//update and merge the customer
customer.setMoney(money);
items.clear();
em.merge(customer);
//put the last parameters in orders and persist it.
order.setBoughtCollection(boughts);
order.setTotal(total);
em.persist(order);
}
return ret;
}



this is a right solution or isnot the way to use the persist?    <<Less
About | Sitemap | Contact