Raible's Wiki

Raible Designs
Wiki Home
News
Recent Changes

AppFuse

Homepage
  - Korean
  - Chinese
  - Italian
  - Japanese

QuickStart Guide
  - Chinese
  - French
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish
  - Japanese

User Guide
  - Korean
  - Chinese

Tutorials
  - Chinese
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish

FAQ
  - Korean

Latest Downloads

Other Applications

Struts Resume
Security Example
Struts Menu

Set your name in
UserPreferences


Referenced by
Articles
Articles_de
CreateManager_de
LeftMenu




JSPWiki v2.2.33

[RSS]


Hide Menu

CreateDAO_de


Difference between version 35 and version 7:

At line 4 changed 1 line.
Dieses Tutorial erläutert, wie man eine neue Tabelle in der Datenbank anlegt, und wie man Java Code erzeugt, um auf diese Tabelle zuzugreifen.
Dieses Tutorial erläutert, wie man eine neue Tabelle in der Datenbank anlegt und wie man Java Code erzeugt, um auf diese Tabelle zuzugreifen.
At line 12 changed 1 line.
%%note __Bemerkung:__ Falls man MySQL verwendet und Transaktionen genutzt werden sollen (was normalerweise der Fall ist), muss man InnoDB Tabellen nutzen. Um dies zu tun, muss man folgende Zeilen zur MySQL Konfigurationsdatei hinzufügen: ({{/etc/my.cnf}} oder {{c:\Windows\my.ini}}). Die 2. Zeile (Für UTF-8 character set) benötigt man für MySQL Versionen ab 4.1.7+.
%%note __Hinweis:__ Falls man MySQL verwendet und Transaktionen genutzt werden sollen (was normalerweise der Fall ist), muss man InnoDB Tabellen nutzen. Um dies zu tun, muss man folgende Zeilen zur MySQL Konfigurationsdatei hinzufügen: ({{/etc/my.cnf}} oder {{c:\Windows\my.ini}}). Die 2. Zeile (Für UTF-8 character set) benötigt man für MySQL Versionen ab 4.1.7+.
At line 31 changed 1 line.
Let's get started on creating a new Object, DAO and Test in AppFuse's project structure.
Beginnen wir mit dem Erstellen eines neuen Objekts, der DAO Klasse und der Tests in Appfuse Projekt Struktur.
At line 33 changed 7 lines.
!Table of Contents
* [1] Create a new Object and add [XDoclet|http://xdoclet.sf.net] tags
* [2] Create a new database table from the object using Ant
* [3] Create a new DaoTest to run JUnit tests on the DAO
* [4] Create a new DAO to perform CRUD on the object
* [5] Configure Spring for the Person object and PersonDao
* [6] Run the DaoTest
!Inhaltsverzeichnis
* [1] Ein neues Objekt erzeugen und [XDoclet|http://xdoclet.sf.net] tags hinzufügen
* [2] Aus dem Objekt mit Hilfe von Ant eine neue Datenbanktabelle erzeugen
* [3] Eine neuen DaoTest erzeugen, um die DAO Klasse mit Hilfe von JUnit zu testen
* [4] Eine neue DAO Klasse erzuegen, um CRUD Methoden auf dem Objekt auszuführen
* [5] Spring für das Person Objekt und die PersonDao konfigurieren
* [6] Den DaoTest ausführen
At line 41 changed 1 line.
!!Create a new Object and add XDoclet tags [#1]
!!Ein neues Objekt erzeugen und XDoclet tags hinzufügen [#1]
At line 43 changed 1 line.
The first thing we need to do is create an object to persist. Let's create a simple "Person" object (in the {{src/dao/**/model}} directory) that has an id, a firstName and a lastName (as properties).
Als Erstes benötigen wir ein Objekt. welches wir persistent speichern wollen. Dafür erzeugen wir ein einfaches "Person" Objekt (im {{src/dao/**/model}} Verzeichnis), welches eine ID, einen Vornamen und einen Nachnamen (als Properties) besitzt.
At line 45 changed 1 line.
%%note __NOTE:__ Copying the Java code in these tutorials [doesn't work in Firefox|http://raibledesigns.com/page/rd?anchor=java2html_plugin_for_jspwiki_causes]. A workaround is to CTRL+Click (Command+Click on OS X) the code block and then copy it.%%
%%note __Hinweis:__ Der Java Code in diesem Tutorial [kann man nicht mit mit dem Firefox|http://raibledesigns.com/page/rd?anchor=java2html_plugin_for_jspwiki_causes] Browser kopieren. Als Notlösung führt man CTRL+Click (Command+Click mit OS X) auf dem Code Block aus und kopiert ihn anschliessend.%%
At line 57 changed 1 line.
Generate your getters and setters using your favorite IDE:
Getters und Setters mit Hilfe der IDE erzeugen:
At line 59 changed 1 line.
Right-click -> Source -> Generate Getters and Setters
Rechts-Click -> Source -> Generate Getters and Setters
At line 64 changed 1 line.
This class should extend [BaseObject|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/model/BaseObject.java.html], which has 3 abstract methods: (equals(), hashCode() and toString()) that you will need to implement in the Person class. The first two are required by Hibernate. The easiest way to do this is using [Commonclipse|http://commonclipse.sf.net]. More information on using this tool can be found on [Lee Grey's site|http://www.leegrey.com/hmm/2004/09/29/1096491256000.html]. Another Eclipse Plugin you can use is [Commons4E|http://commons4e.berlios.de/]. I haven't used it, so I can't comment on its functionality.
Diese Klasse sollte vom [BaseObject|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/model/BaseObject.java.html] abgeleitet sein, welches folgende 3 abstrakten methoden enthält: (equals(), hashCode() und toString()). Diese benötigt man, um die Person Klasse zu implementieren. Die ersten beiden benötigt Hibernate. Diese Methoden können sehr einfach mit [Commonclipse|http://commonclipse.sf.net] erzeugt werden. Mehr Informationen über dieses Tool findet man auf [Lee Grey's Seite|http://www.leegrey.com/hmm/2004/09/29/1096491256000.html]. Ein anderes Eclipse PligIn, das man nutzen könnte, ist [Commons4E|http://commons4e.berlios.de/]. Ich habe es noch nicht verwendet, deshalb kann ich nichts zur genauen Funktionalität sagen.
At line 66 changed 1 line.
;:''If you're using [IntelliJ IDEA|http://www.jetbrains.com/idea], you can generate equals() and hashCode(), but not toString(). There is a [ToStringPlugin|http://www.intellij.org/twiki/bin/view/Main/ToStringPlugin] that works reasonably well.''
;:''Wenn man [IntelliJ IDEA|http://www.jetbrains.com/idea] verwendet, kann man die equals() und hashCode() erzeugen, aber nicht die toString() Methode. Es gibt aber ein [ToStringPlugin|http://www.intellij.org/twiki/bin/view/Main/ToStringPlugin], das anscheinend ganz gut funktioniert.''
At line 68 changed 1 line.
%%note __NOTE:__ If installing these plugins doesn't work for you, you can find all of these methods in the Person.java object that's used to test AppGen. Just look in ''extras/appgen/test/dao/org/appfuse/model/Person.java'' and copy and paste the methods from that class.%%
%%note __Hinweis:__ Falls die Installation dieser PlugIns nicht funktioniert, findet man alle diese Methoden in dem Person.java Objekct, welches zum Testen von AppGen verwendet wird. Einfach in der Datei ''extras/appgen/test/dao/org/appfuse/model/Person.java'' danach suchen, und per "Copy and Paste" in die gerade erstellte Datei kopieren.%%
At line 70 changed 1 line.
Now that we have this POJO created, we need to add XDoclet tags to generate the Hibernate mapping file. This mapping file is used by Hibernate to map objects → tables and properties (variables) → columns.
Nachdem wir jetzt das POJO erzeugt haben, müssen wir die XDoclet Tags hinzufügen, mit deren Hilfe das Hibernate Mapping Datei erzeugt wird. Diese Mapping Datei wird von Hibernate verwendet, um Objekte → Tabellen und Properties (Variablen) → Spalten zu verbinden.
At line 72 changed 1 line.
First of all, we add a [@hibernate.class|http://xdoclet.sourceforge.net/tags/hibernate-tags.html#@hibernate.class%20(0..1)] tag that tells Hibernate what table this object relates to:
Zuerst fügen wir einen [@hibernate.class|http://xdoclet.sourceforge.net/tags/hibernate-tags.html#@hibernate.class%20(0..1)] Tag ein, der Hibernate erklärt, welche Tabelle mit diesem Objekt verbunden ist:
At line 82 changed 1 line.
We also have to add a primary key mapping or XDoclet will puke when generating the mapping file. Note that all @hibernate.* tags should be placed in the __getters'__ Javadocs of your POJOs.
Desweiteren müssen wir einen Primär Schlüssel Mapping angeben, da dies zwingend zur Erzeugung der Mapping Datei von XDoclet benötigt wird. Man beachte, dass alle @hibernate.* tags in den JavaDoc Bereich der __Getter__ des POJOs gehören.
At line 97 changed 1 line.
;:%%(color: blue)''I'm using {{generator-class="increment"}} instead of {{generate-class="native"}} because I [found some issues|AppFuseOnDB2] when using "native" on other databases. If you only plan on using MySQL, I __recommend you use the "native" value__. This tutorial uses increment.''%%
;:%%(color: blue)''Ich verwende {{generator-class="increment"}} anstatt{{generate-class="native"}}, weil ich [einige Probleme|AppFuseOnDB2] entdeckt habe, wenn man "native" mit anderen Datenbanken nutzt. Falls man plant, nur MySQL zu nutzen, __empfehle ich die "native" Einstellung zu nutzen__. Dieses Tutorial ntzt "increment".''%%
At line 99 changed 2 lines.
!!Create a new database table from the object using Ant [#2]
At this point, you can create the person table by running __ant setup-db__. This task creates the {{Person.hbm.xml}} file and creates a database table called "person". From the ant console, you can see the table schema the Hibernate creates for you:
!!Aus dem Objekt mit Hilfe von Ant eine neue Datenbanktabelle erzeugen [#2]
An dieser Stelle kann man die person Tabelle erzeugen, in dem man __ant setup-db__ ausführt. Dieser task generiert die Datei {{Person.hbm.xml}} und erzeugt eine Datenbanktabelle mit dem Namen "person". In der ant console kann man das Schema finden, das Hibernate für einen erstellt:
At line 108 changed 1 line.
If you want to look at the {{Person.hbm.xml}} file that Hibernate generates for you, look in the {{build/dao/gen/**/model}} directory. Here's the contents of {{Person.hbm.xml}} (so far):
Falls man sich die {{Person.hbm.xml}} Datei, die Hibernate für einen erzeugt hat, ansehen will, findet man diese im Verzeichnis {{build/dao/gen/**/model}}. Hier der (bisherige) Inhalt der Datei {{Person.hbm.xml}}:
At line 139 added 1 line.
At line 148 changed 1 line.
Now we'll add additional [@hibernate.property|http://xdoclet.sourceforge.net/tags/hibernate-tags.html#@hibernate.property%20(0..1)] tags for our other columns (first_name, last_name):
Jetzt fügen wir zusätzliche [@hibernate.property|http://xdoclet.sourceforge.net/tags/hibernate-tags.html#@hibernate.property%20(0..1)] tags für die anderen Spalten (first_name, last_name) hinzu:
At line 167 changed 1 line.
In this example, the only reason for adding the ''column'' attribute is because the column name is different from our property name. If they're the same, you don't need to specify the ''column'' attribute. See the [@hibernate.property|http://xdoclet.sourceforge.net/tags/hibernate-tags.html#@hibernate.property%20(0..1)] reference for other attributes you can specify for this tag.
Der einzige Grund, warum in diesem Beispiel das ''column'' Attribut hinzugefügt wird, ist, dass der Spaltenname sich vom Attributnamen unterscheidet. Falls die beiden gleich sein sollten, muss man kein ''column'' Attribut angeben.
Für weitere Informationen kann man einen Blick in die [@hibernate.property|http://xdoclet.sourceforge.net/tags/hibernate-tags.html#@hibernate.property%20(0..1)] Referenz dieses Tags werfen.
At line 169 changed 1 line.
Run __ant setup-db__ again to get the additional columns added to your table.
Jetzt führt man wieder __ant setup-db__ aus, um die neuen Spalten der Tabelle hinzuzufügen.
At line 178 changed 1 line.
If you want to change the size of your columns, modify the ''length'' attribute in your @hibernate.property tag. If you want to make it a required field (NOT NULL), add not-null="true".
Falls man die Größe der Spalten verändern will, kann man das ''length'' Attribut im @hibernate.property tag ändern. Falls man dieses Feld als 'benötigt' (NOT NULL) markieren will, fügt man not-null="true" hinzu.
At line 180 changed 1 line.
!!Create a new DaoTest to run JUnit tests on your DAO [#3]
!!Eine neuen DaoTest erzeugen, um die DAO Klasse mit Hilfe von JUnit zu testen [#3]
At line 182 changed 1 line.
%%note <a name="appgen"></a>__NOTE:__ AppFuse versions 1.6.1+ contain include an [AppGen] tool that can be used to generate all the classes for the rest of these tutorials. However, it's best that you go through these tutorials before using this tool - then you'll know what code it's generating.%%
%%note <a name="appgen"></a>__Hinweis:__ AppFuse Versionen 1.6.1+ besitzen ein [AppGen] Tool, das man vwerwenden kann, um alle restlichen Klassen dieses Tutorials zu erzeugen. Wie auch immer, am besten ist es, wenn man dieses Tutorial durcharbeitet, bevor man das Tool nutzt - Dann weiß man, welchen Code das Tool erzeugt.%%
At line 184 changed 1 line.
Now we'll create a DaoTest to test that our DAO works. "Wait a minute," you say, "we haven't created a DAO!" You are correct. However, I've found that [Test-Driven Development|http://www.artima.com/intv/testdriven.html] breeds higher quality software. For years, I thought __write your test before your class__ was hogwash. It just seemed stupid. Then I tried it and I found that it works great. The only reason I do all this test-driven stuff now is because I've found it rapidly speeds up the process of software development.
Jetzt erzeugen wir einen DaoTest, um zu überprüfen, ob unsere DAO Klasse funktioniert. "Halt, halt," werden sie sagen, "wir haben noch gar keine DAO Klasse erzeugt!" Da haben sie natürlich Recht. Wie auch immer, ich bin der Meinung, dass [Test-Driven Eintwicklung|http://www.artima.com/intv/testdriven.html] qualitativ hochwertigere Software hervorbringt. Lange Zeit dachte ich, __schreib erst die Tests bevor du die Klasse schreibst__ ist Unsinn. Es kam mir einfach dumm vor. Dann versuchte ich es, und fand heraus, dass es wunderbar funktioniert. Der einzige Grund, warum ich jetzt all diese test-driven Dinge verwende, ist, weil ich herausgefunden habe, dass es immens den Prozess der Software Entwicklung beschleunigt.
At line 186 changed 1 line.
To start, create a {{PersonDaoTest.java}} class in the {{test/dao/**/dao}} directory. This class should extend [BaseDaoTestCase|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/dao/BaseDaoTestCase.java.html], a subclass of Spring's [AbstractDependencyInjectionSpringContextTests|http://www.springframework.org/docs/api/org/springframework/test/AbstractDependencyInjectionSpringContextTests.html] which already exists in this package. This parent class is used to load [Spring's|http://www.springframework.org] ApplicationContext (since Spring binds the layers together), and for (optionally) loading a .properties file (ResourceBundle) that has the same name as your {{*Test.class}}. In this example, if you put a {{PersonDaoTest.properties}} file in the same directory as {{PersonDaoTest.java}}, this file's properties will be available via an "rb" variable.
Für den Anfang erzeugt man eine {{PersonDaoTest.java}} Klasse im {{test/dao/**/dao}} Verzeichnis. Diese Klasse sollte von der Klasse [BaseDaoTestCase|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/dao/BaseDaoTestCase.java.html] abgeleitet werden (die bereits in diesem Package vorhanden ist), einer Unterklasse von Spring's [AbstractDependencyInjectionSpringContextTests|http://www.springframework.org/docs/api/org/springframework/test/AbstractDependencyInjectionSpringContextTests.html] Klasse. Diese Superklasse wird zum Laden von [Spring's|http://www.springframework.org] ApplicationContext (da Spring doe Schichten miteinander verbindet) genutzt, und (optional) zum Laden einer .properties Datei (ResourceBundle), welche den selben Namen wie ihre {{*Test.class}} besitzt. Falls sie in diesem Beispiel eine Datei namens {{PersonDaoTest.properties}} in dem gleichen Verzeichnis wie das der Datei erzeugen, ist der Inhalt dieser Datei in einer "rb" Variable verfügbar.
At line 188 changed 1 line.
;:%%(color: blue)''I usually copy (open &rarr; save as) an existing test (i.e. UserDaoTest.java) and find/replace [[Uu]ser with [[Pp]erson, or whatever the name of my object is.''%%
;:%%(color: blue)''Ich kopiere (open &rarr; save as) normalerweise einen bereits vorhandenen Test (z.B. UserDaoTest.java) und suche/ersetze [[Uu]ser mit [[Pp]erson, oder wie auch immer der Name meines Objektes lautet.''%%
At line 208 changed 1 line.
The code you see above is what we need for a basic Spring integration test that initializes and destroys our PersonDao. Spring will use autowiring byType to call the ''setPersonDao()'' method and set the "personDao" bean as a dependency of this class.
Der oben stehende Code stellt dar, was wir für einen einfachen Spring Integrationstest benötigen, um unsere PersonDao zu initialisieren und zu zerstören. Spring verwendet dazu "autowiring byType", um die Methode ''setPersonDao()'' aufzurufen und damit die Bean "personDao" als eine Abhängigkeit dieser Klasse setzt.
At line 210 changed 1 line.
Now we need to actually test that the CRUD (create, retrieve, update, delete) methods work in our DAO. To do this we created methods that begin with "test" (all lower case). As long as these methods are public, have a void return type and take no arguments, they will be called by our &lt;junit&gt; task in our Ant build.xml file. Here's some simple tests for testing CRUD. An important thing to remember is that each method (also known as a test), should be autonomous. Add the following methods to your {{PersonDaoTest.java}} file:
Nun müssen wir überprüfen, ob die CRUD (create, retrieve, update, delete) in unserer DAO Klasse funktionieren. Um dies zu tun erzeugen wir Methoden, die mit "test" (alles klein geschrieben) beginnen. Solange diese Methoden öffentlich sind, müssen sie als Rückgabewert "void" besitzten und keine Argumente besitzen. Sie werden von unserem &lt;junit&gt; task in unserer Ant build.xml Datei aufgerufen. Hier sind ein paar einfache Test um die CRUD Methoden zu überprüfen. Wichtig dabei ist es im Hinterkopf zu behalten, dass jede einzelne dieser Methoden (auch als "Test" bekannt) autonom sein sollte. Folgende Methoden fügt man seiner {{PersonDaoTest.java}} Datei hinzu:
At line 216 added 1 line.
At line 268 added 1 line.
At line 267 changed 1 line.
;:%%(color: blue)''In the testGetPerson method, we're creating a person and then calling a get. I usually enter a record in the database that I can always rely on. Since [DBUnit|http://www.dbunit.org] is used to populate the database with test data before our tests are run, you can simply add the new table/record to the metadata/sql/sample-data.xml file:''%%
;:%%(color: blue)''In der Methode testGetPerson erzeugen wir eine Person und rufen dann ein get auf. Ich füge normalerweise einen Datensatz in die Datenbank ein, den ich jederzeit nutzen kann. Seitdem [DBUnit|http://www.dbunit.org] genutzt wird, um die Datenbank mit Testwerten zu befüllen bevor die Tests ausgeführt werden, kann man einfach eine neue Tabelle / Datensatz in der Datei metadata/sql/sample-data.xml hibzufügen:''%%
At line 284 changed 1 line.
;:%%(color: blue)''This way, you can eliminate the "create new" functionality in the testGetPerson method. If you'd rather add this record directly into the database (via SQL or a GUI), you can rebuild your {{sample-data.xml}} file using __ant db-export__ and then __cp {{db-export.xml metadata/sql/sample-data.xml}}__.''%%
;:%%(color: blue)''Auf diesem Weg kann man die "create new" Funktionalität in der testGetPerson Methode elimieren. Falls man den Datensatz lieber direkt (mit Hilfe von SQL oder einer GUI) in die Datenbank einfügen will, kann man die {{sample-data.xml}} Datei mit Hilfe des __ant db-export__ Befehls und anschliessend __cp {{db-export.xml metadata/sql/sample-data.xml}}__ erzeugen.''%%
At line 286 changed 2 lines.
In the above example, you can see that we're calling person.set*(value) to populate our object before saving it. This is easy in this example, but it could get quite cumbersome if we're persisting an object with 10 required fields (not-null="true"). This is why I created the ResourceBundle in the BaseDaoTestCase. Simply create a {{PersonDaoTest.properties}} file in the same directory as {{PersonDaoTest.java}} and define your property values inside it:
;:%%(color: blue)''I tend to just hard-code test values into Java code - but the .properties file is an option that works great for large objects.''%%
In dem oben beschriebenen Beispiel kann man erkennen, daß wir person.set*(value) aufrufen, um unser Objekt zu befüllen befor wir es speichern. Dies ist in diesem Beispiel einfach, könnte aber sehr aufwendig werden, falls wir z.B. ein Objekt mit 10 benötigten (not-null="true") Attributen speichern wollten. Deshalb habe ich das ResourceBundle im BaseDaoTestCase erstellt. Man erzeugt einfach eine Datei namens {{PersonDaoTest.properties}} im sleben Verzeichnis wie die Datei {{PersonDaoTest.java}} und definiert die Attributwerte in dieser Datei:
;:%%(color: blue)''Ich tendiere dazu, Testwerte hart in den JavaCode einzubinden - aber die properties Datei ist eine andere Möglichkeit, die wunderbar für große Objekte funktioniert.''%%
At line 292 removed 1 line.
Then, rather than calling person.set* to populate your objects, you can use the BaseDaoTestCase.populate(java.lang.Object) method:
At line 297 added 3 lines.
Nun kann man, anstatt person.set* aufzurufen um das Objekt zu befüllen, die Methode BaseDaoTestCase.populate(java.lang.Object) nutzen:
At line 300 changed 1 line.
At this point, the PersonDaoTest class won't compile yet because there is no PersonDao.class in our classpath, we need to create it. PersonDAO.java is an interface, and PersonDAOHibernate.java is the Hibernate implementation of that interface. Let's go ahead and create those.
Zurzeit wird der PersonDaoTest noch nicht comilieren, da noch keine PersonDaoHibernate.class im classpath existiert. Diese müssen wir erzeugen. PersonDao.java ist ein Interface, und PersonDaoHibernate.java ist die Hibernate Implementierung dieses Interfaces. Dann wollen wir diese beiden mal erzeugen.
At line 302 changed 2 lines.
!!Create a new DAO to perform CRUD on the object [#4]
First off, create a PersonDao.java interface in the {{src/dao/**/dao}} directory and specify the basic CRUD methods for any implementation classes. ''I've eliminated the JavaDocs in the class below for display purposes.''
!!Eine neue DAO Klasse erzuegen, um CRUD Methoden auf dem Objekt auszuführen [#4]
Zuallererst erzeugt man ein PersonDao.java Interface im Verzeichnis {{src/dao/**/dao}} und spezifiziert die einfachen CRUD Methoden für beliebige implementierende Klassen. ''Ich habe die JavaDocs in der unten stehenden Klasse entfernt, um die Anzeige übersichtlicher zu halten.''
At line 319 changed 1 line.
Notice in the class above there are no exceptions on the method signatures. This is due to the power of [Spring|http://www.springframework.org] and how it wraps Exceptions with RuntimeExceptions. At this point, you should be able to compile all the source in {{src/dao}} and {{test/dao}} using __ant compile-dao__. However, if you try to run __ant test-dao -Dtestcase=PersonDao__, you will get an error: <span style="color: red">No bean named 'personDao' is defined</span>. This is an error message from Spring - indicating that we need to specify a bean named ''personDAO'' in {{applicationContext-hibernate.xml}}. Before we do that, we need to create the PersonDao implementation class.
Es sei darauf hingewiesen, dass in der obigen Klasse keine Exceptions in den Methoden Signaturen zu finden sind. Dies wird durch die Power von [Spring|http://www.springframework.org] möglich und wie es Exceptions mit RuntimeExceptions kapselt. Jetzt sollte es möglich sein, den Source Code in {{src/dao}} und {{test/dao}} zu comilieren, indem man __ant compile-dao__ aufruft. Wie auch immer, wenn man versucht __ant test-dao -Dtestcase=PersonDao__ laufen zu lassen, erhält man den Fehler: <span style="color: red">No bean named 'personDao' is defined</span>. Dies ist eine Fehlermeldung von Spring - Sie gibt an, dass wir eine Bean mit dem Namen ''personDAO'' in der Datei {{applicationContext-hibernate.xml}} angeben müssen. Bevor wir dies tun, müssen wir die die Klasse erzeugen, welche das Interface PersonDao implementiert.
At line 321 changed 1 line.
;:''The ant task for running dao tests is called __test-dao__. If you pass in a testcase parameter (using __-Dtestcase=name__), it will look for {{**/*${testcase}*}} - allowing us to pass in Person, PersonDao, or PersonDaoTest - all of which will execute the PersonDaoTest class.''
;:''Der ant taskfür die Ausführung der dao Tests lautet __test-dao__. Falls man einen Testcase Parameter mitangibt (in dem man __-Dtestcase=name__ verwendet), sucht ant nach der Datei {{**/*${testcase}*}} - Dies erlaubt es uns als Parameter Person, PersonDao, oder PersonDaoTest zu verwenden - alle werden veranlassen, dass die PersonDaoTest class ausgeführt wird.''
At line 323 changed 1 line.
Let's start by creating a PersonDaoHibernate class that implements the methods in PersonDao and uses Hibernate to get/save/delete the Person object. To do this, create a new class in {{src/dao/**/dao/hibernate}} and name it {{PersonDAOHibernate.java}}. It should extend [BaseDaoHibernate|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/dao/BaseDAOHibernate.java.html] and implement PersonDAO. ''Javadocs eliminated for brevity.''
Beginnen wir mit der Erstellung einer PersonDaoHibernate Klasse, die die Methoden von PersonDao implementiert und Hibernate nutzt, um das Person Objekt zu laden / zu speichern / und zu löschen. Um dies zu tun, erzeugt man eine neue Klasse im Verzeichnis {{src/dao/**/dao/hibernate}} und nennt diese {{PersonDaoHibernate.java}}. Sie sollte von der Klasse [BaseDaoHibernate|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/dao/BaseDAOHibernate.java.html] abgeleitet sein und das Interface PersonDao implementieren. ''Zur Kürzung wurden die Javadocs entfernt.''
At line 360 added 1 line.
At line 356 removed 1 line.
Now, if you try to run __ant test-dao -Dtestcase=PersonDao__, you will get the same error. We need to configure Spring so it knows that PersonDaoHibernate is the implementation of PersonDAO, and we also need to tell it about the Person object.
At line 358 changed 1 line.
!!Configure Spring for the Person object and PersonDao [#5]
Wenn man jetzt versucht __ant test-dao -Dtestcase=PersonDao__ auszuführen, erhält man den selben Fehler. Wir müssen noch Spring so konfigurieren, dass es weiß, dass PersonDaoHibernate die implementierende Klasse zum Interface PersonDao ist. Desweiteren müssen wir Spring mehr über das Objekt Person erzählen.
At line 360 removed 1 line.
First, we need to tell Spring where the Hibernate mapping file is located. To do this, open {{src/dao/**/dao/hibernate/applicationContext-hibernate.xml}} and add {{"Person.hbm.xml"}} to the following code block.
At line 367 added 4 lines.
!!Spring für das Person Objekt und die PersonDao konfigurieren [#5]
Zuerst müssen wir Spring erklären, wo die Hibernate Mapping Datei zu finden ist. Um dies zu tun, öffnet man {{src/dao/**/dao/hibernate/applicationContext-hibernate.xml}} und fügt {{"Person.hbm.xml"}} zum folgenden Codeblock hinzu.
At line 369 removed 1 line.
<value>org/appfuse/model/UserCookie.hbm.xml</value>
At line 374 changed 1 line.
Now we need to add some XML to this file to bind PersonDaoHibernate to PersonDao. To do this, add the following at the bottom of the file:
Jetzt müssen wir dieser Datei noch ein wenig XML hinzufügen, um PersonDaoHibernate an PersonDao zu binden. Um dies zu tun, fügt man folgende Zeilen am Ende der Datei ein:
At line 384 changed 1 line.
;:''You could also use __autowire="byName"__ to the &lt;bean&gt; and get rid of the "sessionFactory" property''. %%(color: blue)''Personally, I like having the dependencies of my objects documented (in XML).''%%
;:''Man könnte auch __autowire="byName"__ zu &lt;bean&gt; hinzufügen, und würde so um die "sessionFactory" property herumkommen''. %%(color: blue)''Persönlich mag ich es aber, wenn die Abhängligkeiten meiner Objekte dokumentiert ist (in XML).''%%
At line 386 changed 2 lines.
!!Run the DaoTest [#6]
Save all your edited files and try running __ant test-dao -Dtestcase=PersonDao__ one more time.
!!Den DaoTest ausführen [#6]
Jetzt speichert man alle editierten Dateien und versucht __ant test-dao -Dtestcase=PersonDao__ noch einmal auszuführen.
At line 395 changed 1 line.
''Next Up:'' __Part II:__ [Creating new Managers|CreateManager] - A HowTo for creating Business Facades, which are similar to [Session Facades|http://java.sun.com/blueprints/patterns/SessionFacade.html], but don't use EJBs. These facades are used to provide communication from the front-end to the DAO layer.
''Next Up:'' __Teil II:__ [Neue Manager erzeugen|CreateManager_de] - Eine Anleitung, wie man A Business Facades erzeugt, welche den [Session Facades|http://java.sun.com/blueprints/patterns/SessionFacade.html] ähneln, aber keine EJBs nutzen. Diese facades werden genutzt, um die Kommunikation zwischen dem Front End und der DAO Schicht zu ermöglichen.

Back to CreateDAO_de, or to the Page History.