|
|||
|
|||
|
Linux Backup Umstieg Tipps nVidia & 2.6.8er Kernel Cocoon OJB JDO CForms Passwort vergessen? Noch keinen Account? SSL aktivieren |
Cocoon, OJB and JDO with CformsContent1:n Relationship
See also: http://joose.iki.fi/ojb/ Database Design
/*
* Table Parent
*/
CREATE TABLE parent (
id INT8 DEFAULT nextval('parent_id_seq'::text) NOT NULL,
name VARCHAR(255) NOT NULL,
CONSTRAINT parent_id_pkey PRIMARY KEY(id),
CONSTRAINT parent_name_ukey UNIQUE(name)
);
CREATE SEQUENCE parent_id_seq start 1 increment 1 maxvalue 9223372036854775807 minvalue 1 cache 1;
/*
* Table childs
*/
CREATE TABLE childs (
id INT8 DEFAULT nextval('childs_id_seq'::text) NOT NULL,
parent_id INT8 NOT NULL,
name VARCHAR(255) NOT NULL,
CONSTRAINT childs_id_pkey PRIMARY KEY(id),
CONSTRAINT childs_parent_id_fkey FOREIGN KEY(parent_id) REFERENCES parent (id)
);
CREATE SEQUENCE childs_id_seq start 1 increment 1 maxvalue 9223372036854775807 minvalue 1 cache 1;
Java Classes
/**
* Parent.java
*/
package papillon;
import java.util.ArrayList;
import java.util.Collection;
import java.io.Serializable;
public class Parent implements Serializable {
private int id;
private Collection childs = new ArrayList ();
private String name;
public int getId () {
return id;
}
public void setId (int id) {
this.id = id;
}
public Collection getChilds () {
return childs;
}
public void setChilds (Collection childs ) {
this.childs = childs;
}
public void addChild (Child child) {
child.setParent (this);
this.childs.add (child);
}
public String getName () {
return name;
}
public void setName (String name) {
this.name = name;
}
}
/**
* Child.java
*/
package papillon;
import java.io.Serializable;
public class Child implements Serializable {
private int id;
private Parent parent;
private String name;
public int getId () {
return id;
}
public void setId (int id) {
this.id = id;
}
public Parent getParent () {
return parent;
}
public void setParent (Parent parent) {
this.parent = parent;
}
public String getName () {
return name;
}
public void setName (String name) {
this.name = name;
}
}
package.jdo
<class name="Parent" identity-type="datastore">
<field name="id" persistence-modifier="persistent">
<extension vendor-name="ojb" key="column" value="id"/>
</field>
<field name="name" persistence-modifier="persistent">
<extension vendor-name="ojb" key="column" value="name"/>
</field>
<field name="childs" embedded="true">
<collection embedded-element="true" />
</field>
</class>
<class name="Child" identity-type="datastore">
<field name="id" persistence-modifier="persistent">
<extension vendor-name="ojb" key="column" value="id"/>
</field>
<field name="parent" persistence-modifier="persistent">
<extension vendor-name="ojb" key="column" value="parent_id"/>
</field>
<field name="name" persistence-modifier="persistent">
<extension vendor-name="ojb" key="column" value="name"/>
</field>
</class>
repository.xml
<class-descriptor class="papillon.Parent" table="parent">
<field-descriptor name="id" primarykey="true" nullable="false" default-fetch="true" autoincrement="true" column="id" sequence-name="parent_id_seq" jdbc-type="INTEGER"/>
<field-descriptor name="name" default-fetch="true" column="name" jdbc-type="VARCHAR"/>
<collection-descriptor name="childs" element-class-ref="papillon.Child" auto-retrieve="true" auto-delete="true" auto-update="true">
<inverse-foreignkey field-ref="parent_id"/>
</collection-descriptor>
</class-descriptor>
<class-descriptor class="papillon.Child" table="childs">
<field-descriptor name="id" primarykey="true" nullable="false" default-fetch="true" column="id" jdbc-type="INTEGER" autoincrement="true" sequence-name="childs_id_seq" />
<field-descriptor name="parent_id" nullable="false" default-fetch="true" column="parent_id" jdbc-type="INTEGER" access="anonymous"/>
<field-descriptor name="name" default-fetch="true" column="name" jdbc-type="VARCHAR"/>
<reference-descriptor name="parent" class-ref="papillon.Parent">
<foreignkey field-ref="parent_id" />
</reference-descriptor>
</class-descriptor>
Flow
function starttest () {
var factory = cocoon.getComponent(Packages.org.apache.cocoon.ojb.jdo.components.JdoPMF.ROLE);
var dao = new Packages.papillon.SimpleDAO(factory, "cocoon");
var bean = new Packages.papillon.Parent ();
var child;
var id;
var iterator;
// 1.
// let's make a new bean with 1 child
bean.setName ('Dad 1');
child = new Packages.papillon.Child ();
child.setName ('Child 1');
bean.addChild (child);
// 2.
// let's save it
dao.insert (bean);
// 3.
// let's retrieve it from database
id = bean.getId();
bean = new Packages.papillon.Parent ();
bean.setId(id);
bean = dao.retrieve(bean);
iterator=bean.getChilds().iterator();
while (iterator.hasNext()) {
child = iterator.next();
child.setName ('Child XXX');
}
// 4.
// let's modify the bean
bean.setName ('Dad 2');
child = new Packages.papillon.Child ();
child.setName ('Child 2');
bean.addChild (child);
// let's save it to the database
dao.update (bean);
// and remove it from database
dao.remove (bean);
cocoon.releaseComponent(factory);
cocoon.redirectTo ("ok.html");
}
|
|
|||
|