1 package org.splat.kernel;
3 * Abstract root class of persistent objects supporting relations to other persistent objects.<br/>
4 * Relations are instances of concrete subclasses of Relation that are assigned to Entity objects at run time.<br/>
6 * Entity objects also support dynamic attributes provided by the Any class.
9 * @author Daniel Brunier-Coulin
10 * @copyright OPEN CASCADE 2012
13 import java.util.HashSet;
14 import java.util.Iterator;
15 import java.util.List;
17 import java.util.Vector;
19 import org.hibernate.Session;
22 public abstract class Entity extends Any {
24 private Set<Relation> relations;
26 // ==============================================================================================================================
28 // ==============================================================================================================================
30 // Database fetch constructor
33 // Initialization constructor
34 protected Entity (ObjectProperties prop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException {
35 // ----------------------------------------
37 relations = new HashSet<Relation>();
40 // ==============================================================================================================================
41 // Public member functions
42 // ==============================================================================================================================
44 public Relation getFirstRelation (Class<? extends Relation> type) {
45 // -----------------------------------------------------------------
46 for (Iterator<Relation> i=relations.iterator(); i.hasNext();) {
47 Relation link = i.next();
48 if (link.getClass().equals(type)) return link;
53 public List<Relation> getRelations (Class<? extends Relation> type) {
54 // -------------------------------------------------------------------
55 List<Relation> result = new Vector<Relation>();
57 for (Iterator<Relation> i=relations.iterator(); i.hasNext();) {
58 Relation link = i.next();
59 if (link.getClass().equals(type)) result.add(link);
64 // ==============================================================================================================================
66 // ==============================================================================================================================
68 protected Set<Relation> getAllRelations () {
69 // ------------------------------------------
73 protected Relation addRelation (Relation link) {
74 // ----------------------------------------------
75 Session session = Database.getSession();
81 if (link.isBidirectional()) {
82 Entity to = (Entity)link.getTo(); // Bidirectional relation are necessarily between entities
84 link = link.getReverse();
86 // if (to.relations == null) to.relations = new HashSet<Relation>();
87 to.relations.add(link);
93 protected void removeRelation (Class<? extends Relation> type, Persistent to) {
94 // -----------------------------------------------------------------------------
95 for (Iterator<Relation> i=relations.iterator(); i.hasNext();) {
96 Relation link = i.next();
97 if (!link.getClass().equals(type)) continue;
98 if (!link.getTo().equals(to)) continue;
100 if (this.isSaved()) Database.getSession().update(this);