Creating your domain model

Creating your domain model

At this point, you should have read Chris' original articles (RAD That Ain't Bad: Domain-Driven Development with Trails, Further Down the Trail), have your environment set-up and be initially familiar with Trails. Start by figuring out what your business objects are, what are the relationships between them and then implement simple entities for them with minimal annotations. Keep your application in a running state as much as possible and work in small increases. Once you've verified your entities are recognized and you can add, update and delete them in your web application, start implementing the relationships between them using the Hibernate annotations.

Implementing Trails "POJOs"

Your domain objects are just POJOs, Plain Old Java Objects, they really are - only with a few minor exceptions :-)First of all, each need to marked with @Entity so Hibernate knows this class represent a persistable domain object, and that it should do something about it. Trails doesn't use Hibernate's EntityManager that's is a J2EE container feature. Instead, using the HibernateAnnotationProcessor Trails produces hibernate.cfg.xml file, and xml description of your entities that Hibernate then reads. If you use Maven, you can configure Apt processor to run as part of your build in your pom.xml (automatically configured for you if you started with an archetype):

<plugin>
	<groupId>org.apache.myfaces.tobago</groupId>
	<artifactId>maven-apt-plugin</artifactId>
	<executions>
		<execution>
			<phase>generate-resources</phase>
			<goals>
				<goal>execute</goal>
			</goals>
		</execution>
	</executions>
	<configuration>
		<factory>org.trails.hibernate.HibernateAnnotationProcessorFactory</factory>
		<force>true</force>
		<nocompile>true</nocompile>
		<generated>target/generated-sources/java</generated>
		<A>
			configFile=\${basedir}/src/main/resources/hibernate.cfg.xml,
			destFile=\${basedir}/target/generated-sources/resources/hibernate.cfg.xml
		</A>
		<target>1.5</target>
	</configuration>
</plugin>

In addition to @Entity, each object needs to have a guaranteed unique identifier. Typically, your entity contains:

@Id @GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() {return id;}

@Entity and @Id are the only mandatory additions to a POJO, but it's a good practice to always implement (override) toString(), equals() and hashCode() operations. Trails uses toString, for example in lists; equals and hashCode are needed to reliably compare object "sameness" based on the object values rather than checking if two object references are pointing to the same instance.

Annotating entities for Trails 

Trails works its magic on assumptions it makes on your code. Sometimes it's right, sometimes not. In cases, the expected behavior is something else you wanted, you can provide more "hints" with annotations for Trails to know what to do.

More stuff needed here, should we just "loan" parts of Chris' articles here? 

Labels

 
(None)