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 19:

At line 168 changed 1 line.
Der einige 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.
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.
At line 180 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 184 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 186 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 188 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 190 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 210 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 212 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 271 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 288 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 290 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 296 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 304 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 307 changed 1 line.
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.''
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 323 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 325 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 327 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 360 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 364 added 3 lines.
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 364 changed 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.
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 373 removed 1 line.
<value>org/appfuse/model/UserCookie.hbm.xml</value>
At line 378 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 388 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 391 changed 1 line.
Save all your edited files and try running __ant test-dao -Dtestcase=PersonDao__ one more time.
Jetzt speichert man alle editierten Dateien und versucht __ant test-dao -Dtestcase=PersonDao__ noch einmal auszuführen.
At line 399 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.