欢迎光临
我们一直在努力

Jboss Ejb3.0 Entity Bean-JSP教程,Java技巧及代码

建站超值云服务器,限时71元/月

 
 

order.java

package org.jboss.tutorial.entity.bean;

 

import javax.persistence.cascadetype;

import javax.persistence.entity;

import javax.persistence.fetchtype;

import javax.persistence.generatortype;

import javax.persistence.id;

import javax.persistence.onetomany;

import javax.persistence.table;

import javax.persistence.id;

import javax.persistence.cascadetype;

import javax.persistence.fetchtype;

import java.util.arraylist;

import java.util.collection;

 

@entity

@table(name = "purchase_order")

//if the table name isnt specified it defaults to the bean name

//of the class. for instance, the lineitem ejb would be mapped to

//the lineitem table

public class order implements java.io.serializable

{

   private int id;

   private double total;

   private collection<lineitem> lineitems;

 

   @id(generate = generatortype.auto)

   public int getid()

   {

      return id;

   }

 

   public void setid(int id)

   {

      this.id = id;

   }

 

   public double gettotal()

   {

      return total;

   }

 

   public void settotal(double total)

   {

      this.total = total;

   }

 

   public void addpurchase(string product, int quantity, double price)

   {

      if (lineitems == null) lineitems = new arraylist<lineitem>();

      lineitem item = new lineitem();

      item.setorder(this);

      item.setproduct(product);

      item.setquantity(quantity);

      item.setsubtotal(quantity * price);

      lineitems.add(item);

      total += quantity * price;

   }

 

   //cascadetype.all specifies that when an order is created,

   //any lineitems held in the lineitems collection will be created

   //as well (cascadetype.persist). if the order is delete from

   //persistence storage, all related lineitems will be

   //deleted (cascadetype.remove). if an order instance is

   //reattached to persistence storage, any changes to the

   //lineitems collection will be merged with persistence

   //storage (cascadetype.merge).

   //fetchtype.eager specifies that when the order is loaded

   //whether or not to prefetch the relationship as well.

   //if you want the lineitems to be loaded on demand, then specify fetchtype.lazy.

   //   the mappedby attribute specifies that this is a bi-directional

   //relationship that is managed by the order property on the lineitem entity bean.

   @onetomany(cascade = cascadetype.all, fetch = fetchtype.eager, mappedby="order")

   public collection<lineitem> getlineitems()

   {

      return lineitems;

   }

 

   public void setlineitems(collection<lineitem> lineitems)

   {

      this.lineitems = lineitems;

   }

}

 

加了不少英文注释,希望能看得懂。

@table(name = "purchase_order")

指名数据库(jboss的数据库为hsqldb)中对应得表的名字,默认为class的名字,比如下面的lineitem就没有@table。(order.java , lineitem.java都为o/r映射,值得互相对照)

@id(generate = generatortype.auto)

递增,同数据库里的相同。必须在getter方法上标注。

@onetomany(cascade = cascadetype.all, fetch = fetchtype.eager, mappedby="order")

关系:order和lineitem为1对多的关系。在这里cascadetype.all是指明当建立一个order被实例化后,都应该同时同时建立lineitem。注释里的就是根据两者建立时间的不同,定义了不同的cascadetype。

 

 

lineitem.java

package org.jboss.tutorial.entity.bean;

 

import javax.persistence.entity;

import javax.persistence.generatortype;

import javax.persistence.id;

import javax.persistence.joincolumn;

import javax.persistence.manytoone;

import javax.persistence.entity;

 

@entity

public class lineitem implements java.io.serializable

{

   private int id;

   private double subtotal;

   private int quantity;

   private string product;

   private order order;

 

   @id(generate = generatortype.auto)

   public int getid()

   {

      return id;

   }

 

   public void setid(int id)

   {

      this.id = id;

   }

 

   public double getsubtotal()

   {

      return subtotal;

   }

 

   public void setsubtotal(double subtotal)

   {

      this.subtotal = subtotal;

   }

 

   public int getquantity()

   {

      return quantity;

   }

 

   public void setquantity(int quantity)

   {

      this.quantity = quantity;

   }

 

   public string getproduct()

   {

      return product;

   }

 

   public void setproduct(string product)

   {

      this.product = product;

   }

 

   //the @joincolumn specifies the foreign key column within the lineitem table.

   @manytoone

   @joincolumn(name = "order_id")

   public order getorder()

   {

      return order;

   }

 

   public void setorder(order order)

   {

      this.order = order;

   }

}

 

 

 

shoppingcart.java

package org.jboss.tutorial.entity.bean;

 

import javax.ejb.remote;

import javax.ejb.remove;

 

@remote

public interface shoppingcart

{

   void buy(string product, int quantity, double price);

 

   order getorder();

 

   @remove void checkout();

}

 

 

 

shoppingcartbean.java

package org.jboss.tutorial.entity.bean;

 

import javax.persistence.entitymanager;

import javax.ejb.inject;

import javax.ejb.remove;

import javax.ejb.stateful;

 

 

@stateful

public class shoppingcartbean implements shoppingcart

{

   @inject

   private entitymanager manager;       //the entitymanager is used to do querying,

//creating, find by primary key, and removal of entity beans.

   private order order;

 

   public void buy(string product, int quantity, double price)

   {

      if (order == null) order = new order();

      order.addpurchase(product, quantity, price);

   }

 

   public order getorder()

   {

      return order;

   }

 

   @remove

   public void checkout()

   {

      manager.persist(order);

   }

}

 

entitymanager的作用可大了,可以用于查询,创建,删除实体bean,这里插入个entitymanager,主要是在最后用户离开时,在数据库里保存数据,相当于保存个object。运行完client.java后就可以在hsqldb上看到有purchase_order 和 lineitem 的两张表。hypersonicdatabase ,找到startdatabasemanager然后下面有个invoke,点击就可以访问hsqldb了。

 

 

client.java

package org.jboss.tutorial.entity.client;

 

 

import org.jboss.tutorial.entity.bean.lineitem;

import org.jboss.tutorial.entity.bean.order;

import org.jboss.tutorial.entity.bean.shoppingcart;

 

import javax.naming.initialcontext;

 

public class client

{

   public static void main(string[] args) throws exception

   {

      initialcontext ctx = new initialcontext();

      shoppingcart cart = (shoppingcart) ctx.lookup(shoppingcart.class.getname());

 

      system.out.println("buying 2 memory sticks");

      cart.buy("memory stick", 2, 500.00);

      system.out.println("buying a laptop");

      cart.buy("laptop", 1, 2000.00);

 

      system.out.println("print cart:");

      order order = cart.getorder();

      system.out.println("total: $" + order.gettotal());

      for (lineitem item : order.getlineitems())

      {

         system.out.println(item.getquantity() + "     " + item.getproduct() + "     " + item.getsubtotal());

      }

 

      system.out.println("checkout");

      cart.checkout();

   }

}

 

 

这里附上log4j.properties 在jboss-ejb-3.0_preview_5.zip 里面没有这个老是显示缺少appender。有了这个将在该目录下生成个record.log日志文件。

 

log4j.properties

log4j.appender.r=org.apache.log4j.rollingfileappender

log4j.appender.r.file=record.log

log4j.appender.r.layout=org.apache.log4j.patternlayout

log4j.appender.r.layout.conversionpattern=%p  %d{hh:mm:ss} %t %c{1} -%m%n

log4j.appender.r.maxbackupindex=1

log4j.appender.r.maxfilesize=100kb

log4j.appender.stdout.layout=org.apache.log4j.patternlayout

log4j.appender.stdout.layout.conversionpattern=%5p [%t] (%f:%l) -%m%n

log4j.appender.stdout=org.apache.log4j.consoleappender

log4j.rootlogger=stdout,r

 

 

 

运行:参考installing.html

windows下

打开命令提示符cmd,到  jboss_home/bin

 run.bat –c all

用ant

先build后run 就行了。

 

 

 

 

 

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » Jboss Ejb3.0 Entity Bean-JSP教程,Java技巧及代码
分享到: 更多 (0)