📄 tutorial.pot
字号:
msgid "You can also design a collection of value types. This is conceptually very different from a collection of references to other entities, but looks almost the same in Java."msgstr ""#: tutorial.xml:1068(title) msgid "Collection of values"msgstr ""#: tutorial.xml:1070(para) msgid "We add a collection of value typed objects to the <literal>Person</literal> entity. We want to store email addresses, so the type we use is <literal>String</literal>, and the collection is again a <literal>Set</literal>:"msgstr ""#: tutorial.xml:1085(para) msgid "The mapping of this <literal>Set</literal>:"msgstr ""#: tutorial.xml:1094(para) msgid "The difference compared with the earlier mapping is the <literal>element</literal> part, which tells Hibernate that the collection does not contain references to another entity, but a collection of elements of type <literal>String</literal> (the lowercase name tells you it's a Hibernate mapping type/converter). Once again, the <literal>table</literal> attribute of the <literal>set</literal> element determines the table name for the collection. The <literal>key</literal> element defines the foreign-key column name in the collection table. The <literal>column</literal> attribute in the <literal>element</literal> element defines the column name where the <literal>String</literal> values will actually be stored."msgstr ""#: tutorial.xml:1104(para) msgid "Have a look at the updated schema:"msgstr ""#: tutorial.xml:1122(para) msgid "You can see that the primary key of the collection table is in fact a composite key, using both columns. This also implies that there can't be duplicate email addresses per person, which is exactly the semantics we need for a set in Java."msgstr ""#: tutorial.xml:1128(para) msgid "You can now try and add elements to this collection, just like we did before by linking persons and events. It's the same code in Java:"msgstr ""#: tutorial.xml:1146(para) msgid "This time we didnt' use a <emphasis>fetch</emphasis> query to initialize the collection. Hence, the call to its getter method will trigger an additional select to initialize it, so we can add an element to it. Monitor the SQL log and try to optimize this with an eager fetch."msgstr ""#: tutorial.xml:1156(title) msgid "Bi-directional associations"msgstr ""#: tutorial.xml:1158(para) msgid "Next we are going to map a bi-directional association - making the association between person and event work from both sides in Java. Of course, the database schema doesn't change, we still have many-to-many multiplicity. A relational database is more flexible than a network programming language, so it doesn't need anything like a navigation direction - data can be viewed and retrieved in any possible way."msgstr ""#: tutorial.xml:1166(para) msgid "First, add a collection of participants to the <literal>Event</literal> Event class:"msgstr ""#: tutorial.xml:1180(para) msgid "Now map this side of the association too, in <literal>Event.hbm.xml</literal>."msgstr ""#: tutorial.xml:1189(para) msgid "As you see, these are normal <literal>set</literal> mappings in both mapping documents. Notice that the column names in <literal>key</literal> and <literal>many-to-many</literal> are swapped in both mapping documents. The most important addition here is the <literal>inverse=\"true\"</literal> attribute in the <literal>set</literal> element of the <literal>Event</literal>'s collection mapping."msgstr ""#: tutorial.xml:1197(para) msgid "What this means is that Hibernate should take the other side - the <literal>Person</literal> class - when it needs to find out information about the link between the two. This will be a lot easier to understand once you see how the bi-directional link between our two entities is created ."msgstr ""#: tutorial.xml:1206(title) msgid "Working bi-directional links"msgstr ""#: tutorial.xml:1208(para) msgid "First, keep in mind that Hibernate does not affect normal Java semantics. How did we create a link between a <literal>Person</literal> and an <literal>Event</literal> in the unidirectional example? We added an instance of <literal>Event</literal> to the collection of event references, of an instance of <literal>Person</literal>. So, obviously, if we want to make this link working bi-directional, we have to do the same on the other side - adding a <literal>Person</literal> reference to the collection in an <literal>Event</literal>. This \"setting the link on both sides\" is absolutely necessary and you should never forget doing it."msgstr ""#: tutorial.xml:1218(para) msgid "Many developers program defensive and create link management methods to correctly set both sides, e.g. in <literal>Person</literal>:"msgstr ""#: tutorial.xml:1241(para) msgid "Notice that the get and set methods for the collection are now protected - this allows classes in the same package and subclasses to still access the methods, but prevents everybody else from messing with the collections directly (well, almost). You should probably do the same with the collection on the other side."msgstr ""#: tutorial.xml:1248(para) msgid "What about the <literal>inverse</literal> mapping attribute? For you, and for Java, a bi-directional link is simply a matter of setting the references on both sides correctly. Hibernate however doesn't have enough information to correctly arrange SQL <literal>INSERT</literal> and <literal>UPDATE</literal> statements (to avoid constraint violations), and needs some help to handle bi-directional associations properly. Making one side of the association <literal>inverse</literal> tells Hibernate to basically ignore it, to consider it a <emphasis>mirror</emphasis> of the other side. That's all that is necessary for Hibernate to work out all of the issues when transformation a directional navigation model to a SQL database schema. The rules you have to remember are straightforward: All bi-directional associations need one side as <literal>inverse</literal>. In a one-to-many association it has to be the many-side, in many-to-many association you can pick either side, there is no difference."msgstr ""#: tutorial.xml:1263(para) msgid "Let's turn this into a small web application."msgstr ""#: tutorial.xml:1270(title) msgid "Part 3 - The EventManager web application"msgstr ""#: tutorial.xml:1272(para) msgid "A Hibernate web application uses <literal>Session</literal> and <literal>Transaction</literal> almost like a standalone application. However, some common patterns are useful. We now write an <literal>EventManagerServlet</literal>. This servlet can list all events stored in the database, and it provides an HTML form to enter new events."msgstr ""#: tutorial.xml:1280(title) msgid "Writing the basic servlet"msgstr ""#: tutorial.xml:1282(para) msgid "Create a new class in your source directory, in the <literal>events</literal> package:"msgstr ""#: tutorial.xml:1296(para) msgid "The servlet handles HTTP <literal>GET</literal> requests only, hence, the method we implement is <literal>doGet()</literal>:"msgstr ""#: tutorial.xml:1326(para) msgid "The pattern we are applying here is called <emphasis>session-per-request</emphasis>. When a request hits the servlet, a new Hibernate <literal>Session</literal> is opened through the first call to <literal>getCurrentSession()</literal> on the <literal>SessionFactory</literal>. Then a database transaction is started-all data access as to occur inside a transaction, no matter if data is read or written (we don't use the auto-commit mode in applications)."msgstr ""#: tutorial.xml:1335(para) msgid "Do <emphasis>not</emphasis> use a new Hibernate <literal>Session</literal> for every database operation. Use one Hibernate <literal>Session</literal> that is scoped to the whole request. Use <literal>getCurrentSession()</literal>, so that it is automatically bound to the current Java thread."msgstr ""#: tutorial.xml:1342(para) msgid "Next, the possible actions of the request are processed and the response HTML is rendered. We'll get to that part soon."msgstr ""#: tutorial.xml:1347(para) msgid "Finally, the unit of work ends when processing and rendering is complete. If any problem occured during processing or rendering, an exception will be thrown and the database transaction rolled back. This completes the <literal>session-per-request</literal> pattern. Instead of the transaction demarcation code in every servlet you could also write a servlet filter. See the Hibernate website and Wiki for more information about this pattern, called <emphasis>Open Session in View</emphasis>-you'll need it as soon as you consider rendering your view in JSP, not in a servlet."msgstr ""#: tutorial.xml:1361(title) msgid "Processing and rendering"msgstr ""#: tutorial.xml:1363(para) msgid "Let's implement the processing of the request and rendering of the page."msgstr ""#: tutorial.xml:1394(para) msgid "Granted, this coding style with a mix of Java and HTML would not scale in a more complex application-keep in mind that we are only illustrating basic Hibernate concepts in this tutorial. The code prints an HTML header and a footer. Inside this page, an HTML form for event entry and a list of all events in the database are printed. The first method is trivial and only outputs HTML:"msgstr ""#: tutorial.xml:1412(para) msgid "The <literal>listEvents()</literal> method uses the Hibernate <literal>Session</literal> bound to the current thread to execute a query:"msgstr ""#: tutorial.xml:1440(para) msgid "Finally, the <literal>store</literal> action is dispatched to the <literal>createAndStoreEvent()</literal> method, which also uses the <literal>Session</literal> of the current thread:"msgstr ""#: tutorial.xml:1455(para) msgid "That's it, the servlet is complete. A request to the servlet will be processed in a single <literal>Session</literal> and <literal>Transaction</literal>. As earlier in the standalone application, Hibernate can automatically bind these ojects to the current thread of execution. This gives you the freedom to layer your code and access the <literal>SessionFactory</literal> in any way you like. Usually you'd use a more sophisticated design and move the data access code into data access objects (the DAO pattern). See the Hibernate Wiki for more examples."msgstr ""#: tutorial.xml:1469(title) msgid "Deploying and testing"msgstr ""#: tutorial.xml:1471(para) msgid "To deploy this application you have to create a web archive, a WAR. Add the following Ant target to your <literal>build.xml</literal>:"msgstr ""#: tutorial.xml:1486(para) msgid "This target creates a file called <literal>hibernate-tutorial.war</literal> in your project directory. It packages all libraries and the <literal>web.xml</literal> descriptor, which is expected in the base directory of your project:"msgstr ""#: tutorial.xml:1509(para) msgid "Before you compile and deploy the web application, note that an additional library is required: <literal>jsdk.jar</literal>. This is the Java servlet development kit, if you don't have this library already, get it from the Sun website and copy it to your library directory. However, it will be only used for compliation and excluded from the WAR package."msgstr ""#: tutorial.xml:1517(para) msgid "To build and deploy call <literal>ant war</literal> in your project directory and copy the <literal>hibernate-tutorial.war</literal> file into your Tomcat <literal>webapp</literal> directory. If you don't have Tomcat installed, download it and follow the installation instructions. You don't have to change any Tomcat configuration to deploy this application though."msgstr ""#: tutorial.xml:1525(para) msgid "Once deployed and Tomcat is running, access the application at <literal>http://localhost:8080/hibernate-tutorial/eventmanager</literal>. Make sure you watch the Tomcat log to see Hibernate initialize when the first request hits your servlet (the static initializer in <literal>HibernateUtil</literal> is called) and to get the detailed output if any exceptions occurs."msgstr ""#: tutorial.xml:1538(title) msgid "Summary"msgstr ""#: tutorial.xml:1540(para) msgid "This tutorial covered the basics of writing a simple standalone Hibernate application and a small web application."msgstr ""#: tutorial.xml:1545(para) msgid "If you already feel confident with Hibernate, continue browsing through the reference documentation table of contents for topics you find interesting - most asked are transactional processing (<xref linkend=\"transactions\"/>), fetch performance (<xref linkend=\"performance\"/>), or the usage of the API (<xref linkend=\"objectstate\"/>) and the query features (<xref linkend=\"objectstate-querying\"/>)."msgstr ""#: tutorial.xml:1553(para) msgid "Don't forget to check the Hibernate website for more (specialized) tutorials."msgstr ""#. Put one translator per line, in the form of NAME <EMAIL>, YEAR1, YEAR2.#: tutorial.xml:0(None) msgid "translator-credits"msgstr ""
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -