- Make sure collections on transient nodes are set to transient when making persitable

to avoid collection objects being loaded from database.
- Allow getKey() to be called on transient nodes iff primary key is set
- Do set DbMapping in setPrototype() method
- Unified Node constructors and initializers a bit
This commit is contained in:
hns 2005-06-15 14:40:57 +00:00
parent 879015efd5
commit 0ecfcd0fba

View file

@ -90,9 +90,7 @@ public final class Node implements INode, Serializable {
this.id = id; this.id = id;
this.name = ((name == null) || "".equals(name)) ? id : name; this.name = ((name == null) || "".equals(name)) ? id : name;
if (prototype != null) {
setPrototype(prototype); setPrototype(prototype);
}
created = lastmodified = System.currentTimeMillis(); created = lastmodified = System.currentTimeMillis();
markAs(CLEAN); markAs(CLEAN);
@ -121,17 +119,24 @@ public final class Node implements INode, Serializable {
this.id = primaryKey.getID(); this.id = primaryKey.getID();
this.name = propname; this.name = propname;
this.anonymous = false; this.anonymous = false;
setPrototype(prototype); setPrototype(prototype);
// set the collection's state according to the home node's state
if (home.state == NEW || home.state == TRANSIENT) {
this.state = TRANSIENT;
} else {
this.state = VIRTUAL; this.state = VIRTUAL;
} }
}
/** /**
* Creates a new Node with the given name. This is used for ordinary transient nodes. * Creates a new Node with the given name. This is used for ordinary transient nodes.
*/ */
public Node(String name, String prototype, WrappedNodeManager nmgr) { public Node(String name, String prototype, WrappedNodeManager nmgr) {
this.nmgr = nmgr; this.nmgr = nmgr;
this.prototype = prototype;
dbmap = nmgr.getDbMapping(prototype); setPrototype(prototype);
// the id is only generated when the node is actually checked into db, // the id is only generated when the node is actually checked into db,
// or when it's explicitly requested. // or when it's explicitly requested.
@ -144,17 +149,12 @@ public final class Node implements INode, Serializable {
/** /**
* Initializer used for nodes being stored in a relational database table. * Initializer used for nodes being stored in a relational database table.
*/ */
public void init(DbMapping dbm, String id, String name, String protoName, public void init(DbMapping dbm, String id, String name, String prototype,
Hashtable propMap, WrappedNodeManager nmgr) { Hashtable propMap, WrappedNodeManager nmgr) {
this.nmgr = nmgr; this.nmgr = nmgr;
// see what prototype/DbMapping this object should use
this.dbmap = dbm; this.dbmap = dbm;
// set the prototype name this.prototype = prototype;
this.prototype = protoName;
this.id = id; this.id = id;
this.name = name; this.name = name;
// If name was not set from resultset, create a synthetical name now. // If name was not set from resultset, create a synthetical name now.
if ((name == null) || (name.length() == 0)) { if ((name == null) || (name.length() == 0)) {
@ -550,6 +550,10 @@ public final class Node implements INode, Serializable {
*/ */
public void setPrototype(String proto) { public void setPrototype(String proto) {
this.prototype = proto; this.prototype = proto;
// set DbMapping matching the prototype name
if (nmgr != null) {
dbmap = nmgr.getDbMapping(proto);
}
} }
/** /**
@ -585,7 +589,7 @@ public final class Node implements INode, Serializable {
* @return ... * @return ...
*/ */
public Key getKey() { public Key getKey() {
if (state == TRANSIENT) { if (primaryKey == null && state == TRANSIENT) {
throw new RuntimeException("getKey called on transient Node: " + this); throw new RuntimeException("getKey called on transient Node: " + this);
} }
@ -2519,6 +2523,8 @@ public final class Node implements INode, Serializable {
if (dbmap != null) { if (dbmap != null) {
Relation rel = dbmap.getExactPropertyRelation(next.getName()); Relation rel = dbmap.getExactPropertyRelation(next.getName());
if (rel != null && rel.isVirtual() && !rel.needsPersistence()) { if (rel != null && rel.isVirtual() && !rel.needsPersistence()) {
// temporarilly set state to TRANSIENT to avoid loading anything from db
n.setState(TRANSIENT);
n.makeChildrenPersistable(); n.makeChildrenPersistable();
// make this a virtual node. what we do is basically to // make this a virtual node. what we do is basically to
// replay the things done in the constructor for virtual nodes. // replay the things done in the constructor for virtual nodes.