Note that there are some explanatory texts on larger screens.

plurals
  1. POEnsuring UML constraints in Java?
    text
    copied!<p>Say that the design of my application is such that there are two classes: <code>Person</code> and <code>Company</code>. Also suppose I have defined a UML diagram that defines a one-to-many relationship between <code>Person</code> and <code>Company</code>: every <code>Person</code> object must belong to exactly one <code>Company</code>, and a <code>Company</code> can contain many <code>Person</code>s.</p> <p>What are some best practices for ensuring that this simple constraint always holds, i.e., that there is never a point in time where a <code>Person</code> object is contained in more than one <code>Company</code>? I've thought of three approaches:</p> <ol> <li><p>Have perfect, bug-free code that would <em>never</em> violate any of my constaints. However, being a realist, I know that this is difficult or impossible, especially as the number of constraints grows.</p></li> <li><p>Implement an <code>ensureCorrectness()</code> method that manually checks each and every constraint. In this example, it would loop through all <code>Person</code>s, and make sure that exactly one <code>Company</code> contained that <code>Person</code>. If not, it would print an error. I would call this method every so often.</p></li> <li><p>Use, e.g., Hibernate to define a database schema and store the data into a database. In the schema, I can define a constraint to match the constrains in my UML diagram. Then, whenever I persists data into the database, Hibernate will check all the constraints and print an error if something's gone wrong.</p></li> </ol> <p>But I can't help wondering if there is another, cleaner way to do this in Java, that doesn't rely on (1) perfection, (2) manual validation, or (3) a relational database. Is there a library or framework that would allow me to specify annotations in the code similar to:</p> <p><code> @OneToMany(Person, Company) </code></p> <p>Or perhaps more specifically:</p> <p><code> @OneToMany(Person, Company.persons) </code></p> <p>assuming the <code>Company</code> class had a <code>List&lt;Person&gt; persons</code> member variable.</p> <p>And if I wanted to ensure that a person's social security number is unique across all <code>Person</code>s, I could say:</p> <p><code> @Unique(Person.SSN) </code></p> <p><strong>Does anything like this exist for Java?</strong></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload