Release Date: 2004-08-24 Author: Umbrellas (geezer) reading people: 1351 Hibernate entry (original) 2004/08/22 Author: Umbrellas (geezer) ---------------- ----------------------------- This article Configuration Environment: JBuilder XJDK 1.4.2Mysql 4.0.11 Driver: mm.mysql-2.0. 4-bin.jar (org.gjt.mm.mysql.driver) Hibernate 2.1 Unzip Hibernate Opens JB, New Engineering, Name: Hibernate Join Hibernate Need Package with MySQL Drive Steps: File-> New Project-> Name Enter Hibernate, Directory Select the path you want to store the project -> Next-> Required libraries-> add-> new-> name Enter the hibernate package name you want to set -> Add-> Select the directory that you hibernate is decompressed, check Hibernate2.jar in this directory and all JAR packages in the lib directory, then add your MySQL driver package and then press OK, Next. A new class, called Hello_Bean.java, the following code: package hibernate; import java.io.Serializable; / *** @author geezer * QQ: 9986986 MSN: geezer_hot@hotmail.com*/public class Hello_Bean implements Serializable {private String name; // Here Name and Address and ID name can be fixed, there will be no effect. But the GET and SET methods are not available. Because it is to correspond to the database and the configuration file.
Private string address; private int id; public hello_bean () {} public hello_bean (string name, string address) {// constructor, after reading this chapter, I believe you will understand this.name = name; this.address = address PUBLIC STRING GETNAME () {// This method name must be the same as the corresponding name in the Hello_Bean.hbm.xml file, the following will be detailed with Return Name;} public void setname (String name) {this.name = name; Public string getaddress () {return address {this.address = address;} public int GETID () {// must-only Return ID;} public void setId (int ID) //// Master's method {this.id = id;}} Complete this step after compiling hibernate.properties in the SRC folder in the Directory after Hibernate, copy the log4j.properties file to the CLASSES directory of your project directory (for example Hibernate / classes / directory, open hibernate.properties files,
Found ## HypersonicSQLhibernate.dialect net.sf.hibernate.dialect.HSQLDialecthibernate.connection.driver_class org.hsqldb.jdbcDriverhibernate.connection.username sahibernate.connection.passwordhibernate.connection.url jdbc: hsqldb: hsql: //localhosthibernate.connection.url jdbc: hsqldb: testhibernate.connection.url jdbc: hsqldb :. changed to ## HypersonicSQL # hibernate.dialect net.sf.hibernate.dialect.HSQLDialect # hibernate.connection.driver_class org.hsqldb.jdbcDriver # hibernate.connection.username sa # hibernate.connection.password # hibernate.connection.url jdbc: hsqldb: hsql: //localhost#hibernate.connection.url jdbc: hsqldb: test # hibernate.connection.url jdbc: hsqldb :. to find ## MySQL # hibernate .dialect net.sf.hibernate.dialect.MySQLDialect # hibernate.connection.driver_class org.gjt.mm.mysql.Driver # hibernate.connection.driver_class com.mysql.jdbc.Driver # hibernate.connection.url jdbc: mysql: / //test#hibernate.connection.username root # hibernate.connection.password change to ## mysqlhibernate.dialect net.sf.hibernate.diaalect.mysqldial ecthibernate.connection.driver_class org.gjt.mm.mysql.Driverhibernate.connection.url jdbc: mysql: // localhost: 3306 / testhibernate.connection.username roothibernate.connection.password above URL Please change your own after the completion of creation An empty file, saved in the same folder in your project class file (such as hibernate / class / hibernate / directory), the file name is: Hello_Bean.hbm.xml content is as follows: XML Version = "1.0" Encoding = "UTF-8"?> < Hibernate-mapping>
Generator class = "ident" /> id>
Finally, in JB, I will build a class in JB. I will explain step by step. The code is as follows: package hibernate; import net.sf.hibernate.cfg.configuration; import net.sf.hibernate.SessionFactory; import Net.sf .hibernate.tool.hbm2ddl.schemaExport; import net.sf.hibernate.Session; import net.sf.hibernate.query; import net.sf.hibernate.hibernate; import net.sf.hibernate.Type.longType; import Net. sf.hibernate.Transaction; import net.sf.hibernate.ScrollableResults; import java.util *; / *** @author geezer * QQ:. 9986986 MSN: geezer_hot@hotmail.com*/public class Hello {public Hello () {} public static void main (String [] args) throws Exception {Configuration cfg = new Configuration () addClass (Hello_Bean.class);. // class initialization with Hello_Bean.class SessionFactory sessions = cfg.buildSessionFactory (); // with BuildSessionFactory method gets a session processory object session session = sessions.opensession (); // Review a session new schemaExport (cfg) .create (true, true) .create (true, true); // This sentence means creating a table, first After running, you will then add this line after the creation is created. If you don't comment out later, you will delete the previously created table and re-build one. Hello_bean my_hibernate = new hello_bean (); // Get a Hello_Bean object my_hibernate.setName ("my_name"); // Set the Name value of the Hello_Bean object is my_name, which is actually the value of the string my_name as the database field name. The database field name is corresponding to the getName in the Hello_Bean class, the setName method is corresponding. Form a mapping relationship. My_HIBERNATE.SETADDRESS ("my_address"); // is as high as session.save (my_hibernate); // This sentence is important, write my_hibernate object into the database (My_Hibernate object) (Name in my_hibernate) is just set up, it has already been set, it will be directly Write Name, Address's value into the database) session.flush (); session.close (); // is a simple insertion data and the first running metrics, here I introduce delete and modified Method, the following code I added a note, what method I need (delete, modify, loop database value) will remove the comment to the HSQL is relatively simple, let's take a look at the example, you should understand, you will not talk about it. .
There are three ways to traverse the database, named Query, Find, Iterate, Query, and Find return a list interface, Iterate returns an Iterator, and the specific method can view these classes. // Delete data / * int a = session.delete ("from hello_bean where id = 1); // If you do not find the ID of 1, return 0, if you find a return 1, here's Hello_Bean is our Hello_Bean class He corresponds to the database table, so we are here to code database tables directly with Hello_Bean. System.out.println (a); session.flush (); session.close (); * /// Query method query data / * hello_bean my_hibernate = null; query q = session.createQuery ("from hello_bean"); / / Query = session.createQuery ("from hello_bean where name =?"); // Here? About the JDBC's PreparedStatement method is similar, but here is starting with 0, JDBC is 1 start. // q.setstring (0, "my_name"); // q.setfirstResult (0); // This sentence means that the query results are listed from the first few rows to the data // q.setMaxResults (3); // This sentence means how many data is taken, just like the SQL Server's TOP method and the mysql's Limit method. // scrollableresults sc = q.scroll (); // Get a scrollableresults, can be scrolled, if your database supports the free movement of the cursor, that is, if you can determine whether the query results are values, or move to the next line. Record, etc..
// if (! Sc.next ()) // {// system.out.println ("Did not find the data you need"); //} session.flush (); // If you use scrollableresults, please This line comes out of session.close (); // If you use scrollableresults, comment this line out of List L = Q.List (); // Return to a list interface, used to traverse the result set for (INT i = 0 i net.sf.hibernate.MappingException: Error reading resource: hibernate / Hello_Bean.hbm.xml If this line appears to show that you are wrong hibernate XML configuration file is wrong net.sf.hibernate.MappingException: Resource: hibernate / Hello_Bean.hbm. XML NOT Found If this line error shows that Hibernate's XML configuration file is not found, you should put the XML file in the same directory with your class file, this article is placed in hibernate / class / hibernate / directory, that is Together with the Hello_Bean.class class file. Net.sf.hibernate.propertyNOTFOUNDEXCEPTION: COULD NOT FIND A SETER for Property Name In Class Hibernate.hello_bean If this line error shows the value of the field name Name in your XML file with the Getxxx or setxxx method in the Hello_Bean.java class Inconsistency. Net.sf.hibernate.HibernateException: JDBC Driver Class Not Found: Org.gjt.mm.mysql.driver If this line error shows that your MySQL driver does not add JB Current or not in ClassPath. Forum discussion connection: http://www.cnjbb.org/thread.jsp? BoardId = 3 & threadID = 41890 finishing release: soliciographic winning from: cnjsp.org Post Reply: Re: Hibernate entry (original) Author: Blue Posted: 2004-09-30 14:45 Contact the author via Email Java.lang.noclassdefounderror: ORG / DOM4J / Attribute At hibernatetetest.test.main (Test.java: 17) Exception in three / "main /" What is the reason? Re: Hibernate entry (original) Author: anchor Posted: 2004-08-26 11:22 Contact the author via Email I would like to ask your friends, I have recently developed a mail system with JBuilder9 JSP, where to download I want to use JSPSMARTupload, but I don't know how to use JSPSMARTUPLOAD below, which directory below? This person just came up, please praise prawn