Introducing Spring Framework

Introducing Spring Framework Introducing Spring Framework

25.02.2015 Views

Chapter 9 ■ Adding Persistence to Your Spring Application Listing 9-10 shows a new method called initialize, where you read the resource files, the schema.sql and data.sql files, and you inject the query through the queryAll property. Also, you are exposing a better way to build the Document object because you are adding the type; just remember that the Document contains this relationship. Look at Figure 9-1 and Listing 9-8 where you have this relationship between the documents and types, and the users and documents. But wait! How do you tell Spring to call the initialize method? Let’s see how by reviewing the XML file in Listing 9-11. Listing 9-11. mydocuments-jdbc-context.xml select d.documentId, d.name, d.location, d.description as doc_desc, d.typeId, d.created, d.modified, t.name as type_name, t.description as type_desc, t.extension from documents d join types t on d.typeId = t.typeId 125

Chapter 9 ■ Adding Persistence to Your Spring Application If you run Listing 9-7 with gradle :ch09:test then the test will pass, because you have added the initialize method and you can count on the data being there. Embedding a Database In the XML configuration (see Listing 9-11) you can see the DocumentRepository class and the initialize method; there should be a better way to initialize your database, right? Well, the Spring Framework offers different ways to use the JDBC; for test purposes it provides a way to use the HSQLDB in an embedded way through an xmlns:jdbc namespace. Let’s modify the XML configuration to use these new features from the xmlns:jdbc namespace (see Listing 9-12). Listing 9-12. mydocuments-jdbc-embedded-context.xml

Chapter 9 ■ Adding Persistence to Your <strong>Spring</strong> Application<br />

Listing 9-10 shows a new method called initialize, where you read the resource files, the schema.sql and<br />

data.sql files, and you inject the query through the queryAll property. Also, you are exposing a better way to build<br />

the Document object because you are adding the type; just remember that the Document contains this relationship.<br />

Look at Figure 9-1 and Listing 9-8 where you have this relationship between the documents and types, and the users<br />

and documents.<br />

But wait! How do you tell <strong>Spring</strong> to call the initialize method? Let’s see how by reviewing the XML file in<br />

Listing 9-11.<br />

Listing 9-11. mydocuments-jdbc-context.xml<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

select d.documentId, d.name, d.location, d.description as doc_desc, d.typeId,<br />

d.created, d.modified,<br />

t.name as type_name, t.description as type_desc, t.extension from documents d<br />

join types t<br />

on d.typeId = t.typeId<br />

<br />

<br />

<br />

<br />

125

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!