⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tutorial.po

📁 hibernate-distribution-3.3.1.GA-dist.zip源码
💻 PO
📖 第 1 页 / 共 5 页
字号:
"}]]>"#: index.docbook:419msgid """What does <literal>sessionFactory.getCurrentSession()</literal> do? First, ""you can call it as many times and anywhere you like, once you get hold of ""your <literal>SessionFactory</literal> (easy thanks to ""<literal>HibernateUtil</literal>). The <literal>getCurrentSession()</""literal> method always returns the \"current\" unit of work. Remember that ""we switched the configuration option for this mechanism to \"thread\" in ""<literal>hibernate.cfg.xml</literal>? Hence, the current unit of work is ""bound to the current Java thread that executes our application. However, ""this is not the full picture, you also have to consider scope, when a unit ""of work begins and when it ends."msgstr """Leemos algunos argumentos de la l&#x00ed;nea de comandos, y si el primer ""argumento es \"store\", creamos y almacenamos un nuevo Event:"#: index.docbook:430msgid """A <literal>Session</literal> begins when it is first needed, when the first ""call to <literal>getCurrentSession()</literal> is made. It is then bound by ""Hibernate to the current thread. When the transaction ends, either through ""commit or rollback, Hibernate automatically unbinds the <literal>Session</""literal> from the thread and closes it for you. If you call ""<literal>getCurrentSession()</literal> again, you get a new ""<literal>Session</literal> and can start a new unit of work. This ""<emphasis>thread-bound</emphasis> programming model is the most popular way ""of using Hibernate, as it allows flexible layering of your code (transaction ""demarcation code can be separated from data access code, we'll do this later ""in this tutorial)."msgstr """<![CDATA[private void createAndStoreEvent(String title, Date theDate) {\n""    Session session = HibernateUtil.currentSession();\n""    Transaction tx = session.beginTransaction();\n""\n""    Event theEvent = new Event();\n""    theEvent.setTitle(title);\n""    theEvent.setDate(theDate);\n""\n""    session.save(theEvent);\n""\n""    tx.commit();\n""    HibernateUtil.closeSession();\n""}]]>"#: index.docbook:441msgid """Related to the unit of work scope, should the Hibernate <literal>Session</""literal> be used to execute one or several database operations? The above ""example uses one <literal>Session</literal> for one operation. This is pure ""coincidence, the example is just not complex enough to show any other ""approach. The scope of a Hibernate <literal>Session</literal> is flexible ""but you should never design your application to use a new Hibernate ""<literal>Session</literal> for <emphasis>every</emphasis> database ""operation. So even if you see it a few more times in the following (very ""trivial) examples, consider <emphasis>session-per-operation</emphasis> an ""anti-pattern. A real (web) application is shown later in this tutorial."msgstr """Creamos un nuevo objeto <literal>Event</literal>, y se lo damos a Hibernate. ""Hibernate cuida ahora del SQL y ejecuta <literal>INSERT</literal>s en la ""base de datos. Echemos una mirada al c&#x00f3;digo de manejo de ""<literal>Session</literal> y <literal>Transaction</literal> antes de ""ejecutar esto."#: index.docbook:452msgid """Have a look at <xref linkend=\"transactions\"/> for more information about ""transaction handling and demarcation. We also skipped any error handling and ""rollback in the previous example."msgstr """Una <literal>Session</literal> es una sola unidad de trabajo. Podr&#x00ed;a ""sorprenderte que tengamos una API adicional, <literal>Transaction</literal>. ""Esto implica que una unidad de trabajo puede ser \"m&#x00e1;s larga\" que ""una sola transacci&#x00f3;n de base de datos; imagina una unidad de trabajo ""que se abarca varios ciclos petici&#x00f3;n/respuesta HTTP (por ejemplo, un ""di&#x00e1;logo asistente) en una aplicaci&#x00f3;n web. Separar las ""transacciones de base de datos de \"las unidades de trabajo de la ""aplicaci&#x00f3;n desde el punto de vista del usuario\" es uno de los ""conceptos b&#x00e1;sicos de dise&#x00f1;o de Hibernate. Llamamos una unidad ""de trabajo larga <emphasis>Transacci&#x00f3;n de Aplicaci&#x00f3;n</""emphasis>, usualmente encapsulando varias transacciones de base de datos ""m&#x00e1;s cortas. Por ahora mantendremos las cosas simples y asumiremos una ""granularidad uno-a-uno entre una <literal>Session</literal> y una ""<literal>Transaction</literal>."#: index.docbook:458msgid """To run this first routine we have to add a callable target to the Ant build ""file:"msgstr """¿Qu&#x00e9; es lo que hacen <literal>Transaction.begin()</literal> y ""<literal>commit()</literal>? ¿D&#x00f3;nde est&#x00e1; el rollback en caso ""que algo vaya mal? La API de <literal>Transaction</literal> de Hibernate es ""opcional realmente, pero la usamos por conveniencia y portabilidad. Si ""manejases la transacci&#x00f3;n de base de datos por ti mismo (por ejemplo, ""llamando a <literal>session.connection.commit()</literal>), ligar&#x00ed;as ""el c&#x00f3;digo a un entorno de despliegue particular, en este JDBC directo ""no manejado. Estableciendo la f&#x00e1;brica de <literal>Transaction</""literal> en tu configuraci&#x00f3;n de Hibernate puedes desplegar tu capa de ""persistencia en cualquier sitio. Echa una mirada al <xref linkend=""\"transactions\"/> para m&#x00e1;s informaci&#x00f3;n sobre manejo y ""demarcaci&#x00f3;n de transacciones. Hemos saltado tambi&#x00e9;n cualquier ""manejo de excepciones y rollback en este ejemplo."#: index.docbook:462msgid """<![CDATA[<target name=\"run\" depends=\"compile\">\n""    <java fork=\"true\" classname=\"events.EventManager\" classpathref=""\"libraries\">\n""        <classpath path=\"${targetdir}\"/>\n""        <arg value=\"${action}\"/>\n""    </java>\n""</target>]]>"msgstr """Para ejecutar la primera rutina tenemos que agregar un objetivo llamable al ""fichero de construcci&#x00f3;n de Ant:"#: index.docbook:464msgid """The value of the <literal>action</literal> argument is set on the command ""line when calling the target:"msgstr """<![CDATA[<target name=\"run\" depends=\"compile\">\n""    <java fork=\"true\" classname=\"EventManager\" classpathref=\"libraries""\">\n""        <classpath path=\"${targetdir}\"/>\n""        <arg value=\"${action}\"/>\n""    </java>\n""</target>]]>"#: index.docbook:469msgid "<![CDATA[C:\\hibernateTutorial\\>ant run -Daction=store]]>"msgstr """El valor del argumento <literal>action</literal> es establecido por l&#x00ed;""nea de comandos al llamar al objetivo:"#: index.docbook:471msgid """You should see, after compilation, Hibernate starting up and, depending on ""your configuration, lots of log output. At the end you will find the ""following line:"msgstr "<![CDATA[C:\\hibernateTutorial\\>ant run -Daction=store]]>"#: index.docbook:476msgid """<![CDATA[[java] Hibernate: insert into EVENTS (EVENT_DATE, title, EVENT_ID) ""values (?, ?, ?)]]>"msgstr """Debes ver, despu&#x00e9;s de la compilaci&#x00f3;n, a Hibernate arrancando ""y, dependiendo de tu configuraci&#x00f3;n mucha salida de registro (log). Al ""final encontrar&#x00e1;s la siguiente l&#x00ed;nea:"#: index.docbook:478msgid """This is the <literal>INSERT</literal> executed by Hibernate, the question ""marks represent JDBC bind parameters. To see the values bound as arguments, ""or to reduce the verbosity of the log, check your <literal>log4j.properties</""literal>."msgstr """<![CDATA[[java] Hibernate: insert into EVENTS (EVENT_DATE, title, EVENT_ID) ""values (?, ?, ?)]]>"#: index.docbook:484msgid """Now we'd like to list stored events as well, so we add an option to the main ""method:"msgstr """Esta es la <literal>INSERT</literal> ejecutada por Hibernate, los signos de ""preguntas representan par&#x00e1;metros de ligado JDBC. Para ver los valores ""ligados como argumentos, o para reducir la verborragia del registro, chequea ""tu <literal>log4j.properties</literal>."#: index.docbook:488msgid """<![CDATA[if (args[0].equals(\"store\")) {\n""    mgr.createAndStoreEvent(\"My Event\", new Date());\n""}\n""else if (args[0].equals(\"list\")) {\n""    List events = mgr.listEvents();\n""    for (int i = 0; i < events.size(); i++) {\n""        Event theEvent = (Event) events.get(i);\n""        System.out.println(\"Event: \" + theEvent.getTitle() +\n""                           \" Time: \" + theEvent.getDate());\n""    }\n""}]]>"msgstr """Ahora quisi&#x00e9;ramos listar acontecimientos almacenados tambi&#x00e9;n, ""as&#x00ed; que agregamos una opci&#x00f3;n al m&#x00e9;todo principal:"#: index.docbook:490msgid "We also add a new <literal>listEvents() method</literal>:"msgstr """<![CDATA[if (args[0].equals(\"store\")) {\n""    mgr.createAndStoreEvent(\"My Event\", new Date());\n""}\n""else if (args[0].equals(\"list\")) {\n""    List events = mgr.listEvents();\n""    for (int i = 0; i < events.size(); i++) {\n""        Event theEvent = (Event) events.get(i);\n""        System.out.println(\"Event: \" + theEvent.getTitle() +\n""                           \" Time: \" + theEvent.getDate());\n""    }\n""}]]>"#: index.docbook:494msgid """<![CDATA[private List listEvents() {\n""\n""    Session session = HibernateUtil.getSessionFactory().getCurrentSession""();\n""\n""    session.beginTransaction();\n""\n""    List result = session.createQuery(\"from Event\").list();\n""\n""    session.getTransaction().commit();\n""\n""    return result;\n""}]]>"msgstr """Agregamos tambi&#x00e9;n un nuevo m&#x00e9;todo <literal>listEvents()</""literal>:"#: index.docbook:496msgid """What we do here is use an HQL (Hibernate Query Language) query to load all ""existing <literal>Event</literal> objects from the database. Hibernate will ""generate the appropriate SQL, send it to the database and populate ""<literal>Event</literal> objects with the data. You can create more complex ""queries with HQL, of course."msgstr """<![CDATA[private List listEvents() {\n""    Session session = HibernateUtil.currentSession();\n""    Transaction tx = session.beginTransaction();\n""\n""    List result = session.createQuery(\"from Event\").list();\n""\n""    tx.commit();\n""    session.close();\n""\n""    return result;\n""}]]>"#: index.docbook:503msgid "Now, to execute and test all of this, follow these steps:"msgstr """Lo que hacemos aqu&#x00ed; es usar una consulta HQL (Lenguaje de Consulta de ""Hibernate o Hibernate Query Language) para cargar todos los objetos ""<literal>Event</literal> existentes de la base de datos. Hibernate ""generar&#x00e1; el SQL apropiado, lo enviar&#x00e1; a la base de datosy ""poblar&#x00e1; los objetos <literal>Event</literal> con datos. Puedes, por ""supuesto, crear consultas m&#x00e1;s complejas con HQL."#: index.docbook:509msgid """Run <literal>ant run -Daction=store</literal> to store something into the ""database and, of course, to generate the database schema before through ""hbm2ddl."msgstr "Now, to execute and test all of this, follow these steps:"#: index.docbook:515msgid """Now disable hbm2ddl by commenting out the property in your ""<literal>hibernate.cfg.xml</literal> file. Usually you only leave it turned ""on in continous unit testing, but another run of hbm2ddl would ""<emphasis>drop</emphasis> everything you have stored - the <literal>create</""literal> configuration setting actually translates into \"drop all tables ""from the schema, then re-create all tables, when the SessionFactory is build""\"."msgstr """Run <literal>ant run -Daction=store</literal> to store something into the ""database and, of course, to generate the database schema before through ""hbm2ddl."#: index.docbook:525msgid """If you now call Ant with <literal>-Daction=list</literal>, you should see ""the events you have stored so far. You can of course also call the ""<literal>store</literal> action a few times more."msgstr """Now disable hbm2ddl by commenting out the property in your ""<literal>hibernate.cfg.xml</literal> file. Usually you only leave it turned ""on in continous unit testing, but another run of hbm2ddl would ""<emphasis>drop</emphasis> everything you have stored - the <literal>create</""literal> configuration setting actually translates into \"drop all tables ""from the schema, then re-create all tables, when the SessionFactory is build""\"."#: index.docbook:531msgid """Note: Most new Hibernate users fail at this point and we see questions about ""<emphasis>Table not found</emphasis> error messages regularly. However, if ""you follow the steps outlined above you will not have this problem, as ""hbm2ddl creates the database schema on the first run, and subsequent ""application restarts will use this schema. If you change the mapping and/or ""database schema, you have to re-enable hbm2ddl once again."msgstr """If you now call Ant with <literal>-Daction=list</literal>, you should see ""the events you have stored so far. You can of course also call the ""<literal>store</literal> action a few times more."#: index.docbook:544msgid "Part 2 - Mapping associations"msgstr """Note: Most new Hibernate users fail at this point and we see questions about ""<emphasis>Table not found</emphasis> error messages regularly. However, if ""you follow the steps outlined above you will not have this problem, as ""hbm2ddl creates the database schema on the first run, and subsequent ""application restarts will use this schema. If you change the mapping and/or ""database schema, you have to re-enable hbm2ddl once again."#: index.docbook:546msgid """We mapped a persistent entity class to a table. Let's build on this and add ""some class asso

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -