EJB メディエーター照会引数を微調整する方法を、次の例で示します。
select e.empid, e.name from Emp as e where e.salary > 100
<?xml version="1.0" encoding="ASCII"?> <datagraph:DataGraphSchema xmlns:datagraph="datagraph.ecore"> <root> <Emp empid="1003" name="Eric" /> <Emp empid="1004" name="Dave" /> </root> </datagraph:DataGraphSchema>
select e.empid, e.name from Emp as e where e.salary > ?1
select e.empid as employeeId, e.bonus+e.salary as totalPay, e.dept.mgr.name as managerNam, e.computePension( ) as pension from Emp as e where e.salary > 100
データ・オブジェクトの属性名は CMP フィールド名から派 生していますが、照会内の AS キーワードを使用してオーバーライドすることができます。 式を指定するとき、式に名前を与えるために常時 AS キーワードが使用されます。
select e.* from Emp as e
select e.name, e.salary from Emp as e
select d.* from Dept d order by d.name select e.* from in(d.emps) e order by e.empid descこれによって、Dept オブジェクトは名前で配列され、 Emp オブジェクトはそれぞれの Dept 内で empid によって降順に配列されます。
select d.deptno, d.name, d.budget from Dept d where d.deptno < 10 select e.empid, e.name, e.salary from in(d.emps) e where e.salary > 10この場合、Dept は DataGraph 内のルート・ノードで、以下に示すように Dept から Emp への多値参照があります。
<?xml version="1.0" encoding="ASCII" ?> <datagraph:DataGraphSchema xmlns:datagraph="datagraph.ecore"> <root> <Dept deptno="1" name="WAS_Sales" budget="500.0" emps="//@root/@Emp.1 //@root/@Emp.0" /> <Dept deptno="2" name="WBI_Sales" budget="450.0" emps="//@root/@Emp.3 //@root/@Emp.2" /> <Emp empid="1001" name="Rob" salary="100.0" EmpDept="//@root/@Dept.0" /> <Emp empid="1002" name="Jason" salary="100.0" EmpDept="//@root/@Dept.0" /> <Emp empid="1003" name="Eric" salary="200.0" EmpDept="//@root/@Dept.1" /> <Emp empid="1004" name="Dave" salary="500.0" EmpDept="//@root/@Dept.1" /> </root> </datagraph:DataGraphSchema>
select d.* from Dept as d where d.deptno between ?1 and ?3 select e.* from in(d.emps) e where e.salary < ?2
select d.deptno, d.name from Dept as d select p.projid from in(d.projects) p select t.taskid, t.cost from in (p.tasks) tXML フォーマットの結果のデータ・グラフを以下に表示します。
<?xml version="1.0" encoding="ASCII" ?> <datagraph:DataGraphSchema xmlns:datagraph="datagraph.ecore"> <root> <Dept deptno="1" name="WAS_Sales" projects="//@root/@Project.0" /> <Dept deptno="2" name="WBI_Sales" projects="//@root/@Project.1" /> <Project projid="1" ProjectDept="//@root/@Dept.0" tasks="//@root/@Task.0 //@root/@Task.2 //@root/@Task.1" /> <Project projid="2" ProjectDept="//@root/@Dept.1" tasks="//@root/@Task.3" /> <Task taskid="1" cost="50.0" TaskProject="//@root/@Project.0" /> <Task taskid="2" cost="60.0" TaskProject="//@root/@Project.0" /> <Task taskid="3" cost="900.0" TaskProject="//@root/@Project.0" /> <Task taskid="7" cost="20.0" TaskProject="//@root/@Project.1" /> </root> </datagraph:DataGraphSchema>
select d.deptno, d.name from Dept d select e.empid, e.name from in(d.emps) e select p.projid from in(d.projects) p select t.taskid, t.cost from in(p.tasks) where t.cost > 10戻された DataGraph は、以下のようになりえます。
<?xml version="1.0" encoding="ASCII" ?> <datagraph:DataGraphSchema xmlns:datagraph="datagraph.ecore"> <root> <Dept deptno="1" name="WAS_Sales" projects="//@root/@Project.0" emps="//@root/@Emp.1 //@root/@Emp.0" /> <Dept deptno="2" name="WBI_Sales" projects="//@root/@Project.1" emps="//@root/@Emp.3 //@root/@Emp.2" /> <Project projid="1" ProjectDept = "//@root/@Dept.0" tasks="//@root/@Task.0 //@root/@Task.2 //@root/@Task.1" /> <Project projid="2" ProjectDept="//@root/@Dept.1" tasks="//@root/@Task.3" /> <Task taskid="1" cost="50.0" TaskProject="//@root/@Project.0" /> <Task taskid="2" cost="60.0" TaskProject="//@root/@Project.0" /> <Task taskid="3" cost="900.0" TaskProject="//@root/@Project.0" /> <Task taskid="7" cost="20.0" TaskProject="//@root/@Project.1" /> <Emp empid="1001" name="Rob" EmpDept="//@root/@Dept.0" /> <Emp empid="1002" name="Jason" EmpDept="//@root/@Dept.0" /> <Emp empid="1003" name="Eric" EmpDept="//@root/@Dept.1" /> <Emp empid="1004" name="Dave" EmpDept="//@root/@Dept.1" /> </root> </datagraph:DataGraphSchema>
select e.empid, e.name from Emp e select d.deptno, d.name from in(e.dept) dXML フォーマットの DataGraph は以下のようになります。
<?xml version="1.0" encoding="ASCII" ?> <datagraph:DataGraphSchema xmlns:datagraph="datagraph.ecore"> <root> <Emp empid="1001" name="Rob" dept="//@root/@Dept.0" /> <Emp empid="1002" name="Jason" dept="//@root/@Dept.0" /> <Emp empid="1003" name="Eric" dept="//@root/@Dept.1" /> <Emp empid="1004" name="Dave" dept="//@root/@Dept.1" /> <Dept deptno="1" name="WAS_Sales" DeptEmp="//@root/@Emp.1 //@root/@Emp.0" /> <Dept deptno="2" name="WBI_Sales" DeptEmp="//@root/@Emp.3 //@root/@Emp.2" /> </root> </datagraph:DataGraphSchema>
select e.empid as EmplId , e.name as EmpName , e.dept.deptno as DeptNo , e.dept.name as DeptName from Emp as e
select e.empid, e.name from Emp as e select t.taskid, t.description from in(e.tasks) as t select p.projid, p.cost from in(t.proj) as p
select d.deptno, d.name from Dept d where d.name like ‘%Dev%’ select e.empid, e.name from in (d.emps) as e select m.empid, m.name from in(d.manager) as m2 番目および 3 番目の select 文節は、いずれも Emp DataObject のインスタンスを戻します。 d.emps 関係および d.manager 関係から同じ Emp インスタンスが検索される可能性もあります。 EJB メディエーターは 1 つの Emp インスタンスを作成しますが、両方の関係を作成します。
select d.deptno, d.name from Dept d where d.name like ‘%Dev%’ select e.empid, e.name from in (d.emps) as e xrel d.manager
select d.deptno, d.name from Dept d where d.name like ‘%Dev%’ select e.empid, e.name from Emp e where e.name like ‘Dev%’ xrel d.emps, d.manager
select d.deptno from Dept d where d.deptno = 42 select e.empid from in(d.emps) e select p.projid from in(d.projs) p select t.* from in(p.tasks) t xrel e.tasks
部門 42 の従業員にタスクが割り当てられると、そのリンクがデータ・グラフに表示されます。 部門 42 に属さない従業員にタスクが割り当てられると、そのデータ・オブジェクトは照会によって フィルタリングされて除外されるため、そのリンクはデータ・グラフには表示されません。 1 つの XREL キーワードの後に、1 つ以上の EJB 関係を続けることができます。 双方向関係は、いずれの役割名も参照することができます。 関係のソースおよびターゲットの両方とも、1 つ以上の照会によって検索される必要があります。
select d.deptno, d.name from Dept d where d.name like ‘%Dev%’ select t.taskid, t.startDate from Task t where t.startDate > ‘2005’
select d.deptno, d.name from Dept d where d.name like ‘%Dev%’ select e.empid, e.name from Emp e where e.dept.name like ‘%Dev%’
select d.deptno, d.name from Dept d where d.emps is empty select e.empid, e.name from Emp e where e.dept is null xrel e.dept
select d.deptno, d.name from ((Dept) ?1) as d select e.empid, e.name from in(d.emps) as e where e.salary > 10
// this method runs in an EJB context and within a transaction scope public DataGraph myServiceMethod() { InitialContext ic = new InitialContext(); DeptLocalHome deptHome = ic.lookup("java:comp/env/ejb/Dept"); Integer deptKey = new Integer(10); DeptEJB dept = deptHome.findByPrimaryKey( deptKey)); Iterator i = dept.getEmps().iterator(); while (i.hasNext()) { EmpEJB e = (EmpEJB)i.next(); e.setSalary( e.getSalary() * 1.10); // give everyone a 10% raise } // create the query collection parameter Collection c = new LinkedList(); c.add(dept); Object[] parms = new Object[] { c}; // put ejb collection in parm array. // collection containing the dept EJB is passed to EJB Mediator String[] query = new String[] { "select d.deptno, d.name from ((Dept)?1 ) as d", "select e.empid, e.name, e.salary " + " from in (d.employees) as e", "select p.projno, p.name from in (d.projects) as p" }; Mediator m = EJBMediatorFactory.getInstance().createMediator( query, parms); DataGraph dg = m.getGraph(); return dg; // the DataGraph contains the updated and as yet uncommitted // salary information. Dept and Emp data // is fetched through EJB instances active in the EJBContainer. // Project data is retrieved from database using // container managed relationships. }