Objective: To learn about commonly used relational entity operations through instances, and use familiar order management for instances. Main Content: N create a database table, including the order table and order list. N. Create a persistent unit and Object Class N. Create a session bean that manages the object class, and add orders, delete orders, and view all orders. N write a client program for testing. Note: You can refer to this content when completing the third big job, but do not follow it completely. 1. Create a database table order management includes the order table and order list (under normal circumstances, it should also contain the item table for ease of explanation). The table definition statement is as follows: create Table ordertable (orderid char (10) Not null, orderdate date, orderstate char (1), userid char (10), primary key (orderid )); create Table orderdetail (orderid char (10), goodid char (10), quantity int, primary key (orderid, goodid), foreign key (orderid) References ordertable (orderid )); 2. The process of creating a persistent unit and object class operations is the same as that described earlier. The code for the entity class generated based on the table structure above is as follows. 2.1 object class ordertable: Package order; import Java. io. serializable; import Java. util. collection; import Java. util. date; import javax. persistence. cascadetype; import javax. persistence. column; import javax. persistence. entity; import javax. persistence. fetchtype; import javax. persistence. ID; import javax. persistence. namedqueries; import javax. persistence. namedquery; import javax. persistence. onetovel; import javax. pers Istence. table; import javax. persistence. temporal; import javax. persistence. temporaltype; @ entity @ table (name = "ordertable") @ namedqueries ({@ namedquery (name = "ordertable. findbyorderid ", query =" select O from ordertable O where o. orderid =: orderid "), @ namedquery (name =" ordertable. findbyorderdate ", query =" select O from ordertable O where o. orderdate =: orderdate "), @ namedquery (name =" ordertable. Findbyorderstate ", query =" select O from ordertable O where o. orderstate =: orderstate "), @ namedquery (name =" ordertable. findbyuserid ", query =" select O from ordertable O where o. userid =: userid ")}) public class ordertable implements serializable {@ ID @ column (name =" orderid ", nullable = false) Private string orderid; @ column (name = "orderdate") @ temporal (temporaltype. date) private date order Date; @ column (name = "orderstate") Private Character orderstate; @ column (name = "userid") Private string userid; @ onetoetype (cascade = cascadetype. all, mappedby = "ordertable", fetch = fetchtype. eager) private collection <orderdetail> orderdetailcollection;/** creates a new instance of ordertable */Public ordertable () {} public ordertable (string orderid) {This. orderid = orderid;} Public String get Orderid () {return this. orderid;} public void setorderid (string orderid) {This. orderid = orderid;} public date getorderdate () {return this. orderdate;} public void setorderdate (date orderdate) {This. orderdate = orderdate;} public character getorderstate () {return this. orderstate;} public void setorderstate (character orderstate) {This. orderstate = orderstate;} Public String getuserid (){ Return this. userid;} public void setuserid (string userid) {This. userid = userid;} public collection <orderdetail> getorderdetailcollection () {return this. orderdetailcollection;} public void setorderdetailcollection (collection <orderdetail> orderdetailcollection) {This. orderdetailcollection = orderdetailcollection;} @ override public int hashcode () {int hash = 0; hash + = (this. orderid! = NULL? This. orderid. hashcode (): 0); Return hash;} @ override public Boolean equals (Object object) {// todo: warning-This method won't work in the case the ID fields are not set if (! (Object instanceof ordertable) {return false;} ordertable Other = (ordertable) object; If (this. orderid! = Other. orderid & (this. orderid = NULL |! This. orderid. equals (Other. orderid) return false; return true;} @ override Public String tostring () {return "order. ordertable [orderid = "+ orderid +"] ";}} 2.2 entity class orderdetail: Package order; import Java. io. serializable; import javax. persistence. column; import javax. persistence. embeddedid; import javax. persistence. entity; import javax. persistence. joincolumn; import javax. persistence. manytoone; import ja VAX. persistence. namedqueries; import javax. persistence. namedquery; import javax. persistence. table; @ entity @ table (name = "orderdetail") @ namedqueries ({@ namedquery (name = "orderdetail. findbyorderid ", query =" select O from orderdetail O where o. orderdetailpk. orderid =: orderid "), @ namedquery (name =" orderdetail. findbygoodid ", query =" select O from orderdetail O where o. orderdetailpk. goodid =: goodid "), @ Namedquery (name =" orderdetail. findbyquantity ", query =" select O from orderdetail O where o. quantity =: quantity ")}) public class orderdetail implements serializable {@ embeddedid protected orderdetailpk; @ column (name =" quantity ") Private integer quantity; @ joincolumn (name = "orderid", referencedcolumnname = "orderid", insertable = false, updatable = false) @ manytoone private Ordertable; Public orderdetail () {} public orderdetail (orderdetailpk) {This. orderdetailpk = orderdetailpk;} public orderdetail (string orderid, string goodsid) {This. orderdetailpk = new orderdetailpk (goodsid, orderid);} public orderdetailpk getorderdetailpk () {return this. orderdetailpk;} public void setorderdetailpk (orderdetailpk) {This. orderdetailpk = Orderdetailpk;} public integer getquantity () {return this. quantity;} public void setquantity (integer quantity) {This. quantity = quantity;} public ordertable getordertable () {return this. ordertable;} public void setordertable (ordertable) {This. ordertable = ordertable;} @ override public int hashcode () {int hash = 0; hash + = (this. orderdetailpk! = NULL? This. orderdetailpk. hashcode (): 0); Return hash;} @ override public Boolean equals (Object object) {// todo: warning-This method won't work in the case the ID fields are not set if (! (Object instanceof orderdetail) {return false;} orderdetail Other = (orderdetail) object; If (this. orderdetailpk! = Other. orderdetailpk & (this. orderdetailpk = NULL |! This. orderdetailpk. equals (Other. orderdetailpk) return false; return true ;}@ override Public String tostring () {return "order. orderdetail [orderdetailpk = "+ orderdetailpk +"] ";}} 2.3 order details primary key class: Package order; import Java. io. serializable; import javax. persistence. column; import javax. persistence. embeddable; @ embeddablepublic class orderdetailpk implements serializable {@ column (name = "orderid", null Able = false) Private string orderid; @ column (name = "goodid", nullable = false) Private string goodid;/** creates a new instance of orderdetailpk */Public orderdetailpk () {} public orderdetailpk (string goodid, string orderid) {This. goodid = goodid; this. orderid = orderid;} Public String getorderid () {return this. orderid;} public void setorderid (string orderid) {This. orderid = orderid;} p Ublic string getgoodid () {return this. goodid;} public void setgoodid (string goodid) {This. goodid = goodid;} @ override public int hashcode () {int hash = 0; hash + = (this. goodid! = NULL? This. goodid. hashcode (): 0); hash + = (this. orderid! = NULL? This. orderid. hashcode (): 0); Return hash;} @ override public Boolean equals (Object object) {// todo: warning-This method won't work in the case the ID fields are not set if (! (Object instanceof orderdetailpk) {return false;} orderdetailpk Other = (orderdetailpk) object; If (this. goodid! = Other. goodid & (this. goodid = NULL |! This. goodid. Equals (other. goodid) return false; If (this. orderid! = Other. orderid & (this. orderid = NULL |! This. orderid. equals (Other. orderid) return false; return true;} @ override Public String tostring () {return "order. orderdetailpk [goodid = "+ goodid +", orderid = "+ orderid +"] ";}} 3. Create a session bean to manage entities and add the following business methods to the Session Bean: N add order method N delete order method N query all orders method N add ORDER item 3.1 add business method: Add order public void addorder (ordertable order) {ordertable neworder = em. merge (order); em. persist (neworder);} because the order object passed by the parameter is in the separate state You must first convert the instance to a managed instance through the merge operation, and then use the persist Method for persistence. Note: When the order is persisted, the Order details associated with the order are also persisted. This is done by specifying the "cascade" attribute during definition management. The Code is as follows: @ onetoetype (cascade = cascadetype. all, mappedby = "ordertable", fetch = fetchtype. eager) private collection <orderdetail> orderdetailcollection; the value of the cascade attribute is cascadetype. all means that operations on the order class are cascade to the associated order details, including the following order deletion. 3.2 add business method: delete order public void removeorder (string orderid) {ordertable order = em. find (ordertable. class, orderid); em. remove (order);} to delete an order, you need to find the order and then delete it. 3.3 Add business method: Query all orders in public list <ordertable> getallorders () {return em. createquery ("select O from ordertable o "). getresultlist ();} because the order includes order details, all order details involved in the order should be queried when querying all orders, this function is completed by the fetch attribute when the link is defined. The code is in red as follows: @ onetoetype (cascade = cascadetype. all, mappedby = "ordertable", fetch = fetchtype. eager) private collection <orderdetail> orderdetailcollection; if you do not need to load the object, you can use fetchtype. lazy. In the manytoone relationship, the default value of the fetch attribute is eager. In the onetoone relationship, the default value of the fetch attribute is eager. In the onetoworkflow relationship, the default value of the fetch attribute is lazy. In the manytoch relationship, the default value of the fetch attribute is lazy. 3.4 add business method: Add ORDER item public void additem (string orderid, string goodsid, int quantity) {ordertable order = em. find (ordertable. class, orderid); orderdetail item = new orderdetail (orderid, goodsid); item. setquantity (Quantity); If (Order. getorderdetailcollection () = NULL) Order. setorderdetailcollection (new vector <orderdetail> (); Order. getorderdetailcollection (). add (item); item. setordertable (order);} 4. Write the client program Test the following three functions: N add Order N delete order n view order 4.1 Add order there are two ways to add order: N create order object, create order item object, add ORDER item to order, and then call Session Bean to complete adding. N create an order object, call Session Bean to complete adding, and then call the Add ORDER item method of Session Bean to add specific order items. The following code uses remote as the injected EJB object. 4.1.1 first method ordertable order = new ordertable (); string orderid = "order2222"; Order. setorderid (orderid); Order. setuserid ("lixucheng"); Order. setorderstate ('0'); Order. setorderdate (new date (); vector v = new vector (); orderdetail od1 = new orderdetail (orderid, "goods111"); orderdetail od2 = new orderdetail (orderid, "goods777"); orderdetail od3 = new orderdetail (orderid, "goods333"); orderdetail od4 = N EW orderdetail (orderid, "goods999"); orderdetail od5 = new orderdetail (orderid, "goods555"); od1.setordertable (order); od2.setordertable (order); od3.setordertable (order ); od4.setordertable (order); od5.setordertable (order); V. add (od1); V. add (od2); V. add (od3); V. add (od4); V. add (od5); Order. setorderdetailcollection (V); remote. addorder (order); 4.1.2 Method 2: ordertable order = new ordertable (); Order. setord Erid ("order1111"); Order. setuserid ("lixucheng"); Order. setorderstate ('0'); Order. setorderdate (new date (); remote. addorder (order); remote. additem ("order1111", "goods111", 10); remote. additem ("order1111", "goods222", 20); remote. additem ("order1111", "goods333", 30); remote. additem ("order1111", "goods444", 40); remote. additem ("order1111", "goods555", 50); 4.2 Delete an order based on the order number. Remote. removeorder ("order0000"); 4.3 query all orders and details list <ordertable> List = remote. getallorders (); this method is called to obtain details of all orders and orders. The following code shows the order information in the servlet: iterator <ordertable> I = List. iterator (); While (I. hasnext () {ordertable temporder = I. next (); out. println ("Order Number:" + temporder. getorderid () + "Order Date:" + temporder. getorderdate () + "details:"); iterator <orderdetail> detail = temporder. getorderdetailcollection (). iterator (); While (detail. hasnext () {orderdetail tempdetail = detail. next (); out. println ("[" + tempdetail. getorderdetailpk (). getgoodid () + "," + tempdetail. getquantity () + "]");} Out. println ("<br> ");
}
For more information, see Java ee 5 practical tutorial-Based on WebLogic and eclipse