Notes about Hibernate and ADO. NET-Exercises

Source: Internet
Author: User
Tags call back configuration settings
Recently, I am working on a Java Web program, which is developed using MyEclipse and Struts (*. do) as a development framework, Hibernate as a database proxy class, and is responsible for database operations to maintain data persistence and data access. Spring is used for integration and injection. Well, Ajax is also used, the implementation of Javascript in Java is really annoying. What Ajax is basically writing JS scripts (but it is also the most primitive pull), and I wrote my own JS scripts, I haven't written so much for a long time. I also need to practice XMLHTTPRequest.

Reference

If. net, it's much more convenient. We can drag the AjaxControlToolkit control, five core AJAX Extensions components, dozens of ajax Toolkit controls, and advertisement on both sides, there is a smart prompt like Google (speaking of this, every time I do this, there is a problem that after specifying the Css style for each row of items in the returned record, when you move the selected entry with the arrow keys on or off the keyboard, a script error occurs, and you cannot move the selected entry with the arrow keys. I tried it, as long as the class is not set, there is no problem. It can move the selection and smart prompts normally, but it can be seen if the style is not set? He is a black-white background by default, but he does not know whether the DIV layer is used. I used to return <span style = 'color: red'> Jonllen </span> for testing, he failed to return Jonllen, And I tried Html encoding and anti-encoding for him, if the Html display effect can be obtained on the page (speaking of this, I want to open a discussion, and I will talk about it later ), if this works, you can also set its Css style, but it just doesn't work. I don't know if you 've ever encountered this problem, please know) there are also the implementation of the page to be frozen, and the discussion... what do you need? Depends on what functions you want to implement. But I think there are too many Ajax controls called WebServer, and sometimes it is difficult to write a new WebMethod, however, this is in line with the essence of Ajax, that is, to call back the Xml of WebServer. However, I think the previous Ajax is okay. He didn't make the control, but he just wanted to reference the dll, the callback is an Ajax method. In the past, AjaxPro. dll was used in VS2003. It still works. Although this method has many limitations, it is quite convenient to use. MS is really human-friendly, and Sun has to learn something, But Sun is still doing its J2EE specifications.

At the beginning, it was okay, that is, if Javascript is not easy to debug, alert could only do it. Now, we can see that the JS has been executed, the open url I call in JS is a servlet class, and then Hibernate is called here to perform database operations to achieve asynchronous Ajax processing, because I used to connect to SQL Server 2000 in this project, but now I want to use the MYSQL database, I deleted the three driver packages of SQL Server 2000 for Hibernate in the previous project, and then chose DB Browser to reset the Hibernate data connection, edit: Change the port, enter the database password, and add an MQSQL database connection driver package (jar file). Find the package and select, configure OK, Next, on the E disk, finish, Open Connection, a long string of progress bars to the end, OK Connection is successful, smooth sailing, click one to see a few databases under several tables are logarithm, I Deleted some data ing objects under the hibr package under the previous project and the corresponding DAO data access, and then Hibernate Reverse Engineering re-added the deleted Hibernate database ing objects, using assigned (with a primary key) use assigned, use native (automatically increasing the primary key) use native, OK, Hibernate proxy class, now you only need to look at the servlet ,... DAO dao = new... DAO ()... Database Operation completed! Now let's look at Ajax. How can it be great? In the case of no refresh, asynchronous CallBack Server, configure WebLogic, Redeploy to Redeploy, Run, open the Brower key, such as the URL, to see the effect, and look forward to it, thinking: onclick submit, addshow () calls a JS function, and sends it back to the server, result · The returned result is an alert ('exception occurred on the page you requested ~ Why? I closed the debugging and restarted the system. Why? I carefully checked my JS Code, carefully passed the functions in JS, checked one by one, and defined the XMLHttpRequest object, sendRequest (url) to send the function request, processResponse () the function that processes the returned information, and so on... sendRequest (url) sends a function request. For the URL, this should be the problem. My url is "servlet/FindServlet? Sn = "+ ss; FindServlet is the servlet class of FindServlet. java under my project ss package. ss is a variable of var in JS. After thinking about it, servlet/FindServlet? Sn = "+ ss, not ss/FindServlet? Sn = "+ ss? No. I also tested it with ss later. No, what is the problem? Isn't the problem above the URL? The url is correct. After thinking for a while, you just need to test it! I quickly opened the FindServlet class, but I am still very confused about my old spoon: You are really stupid! Go to FindServlet, and in the doGet method, String shen = request. getParameter ("sn"); System. out. println (shen); obtain the sn query string (haha. net learning too much), and then output in the console, save and run, ah, really output, the console outputs "Hunan", I am querying the string value of the sn, then in this case, there is no problem with the URL. It has sent the request to my FindServlet class. Here I can accept it, and then let's look at the FindServlet class, add System. out. println ("variable value"); I am used to debugging Java methods, debugging and running. If it is not completed, an exception is reported, the console has no output after outputting the variable values in front of DAO. It must be a problem with DAO! I turn my eyes to DAO. The first line of AddrtableDAO dao = new AddrtableDAO (); it is absolutely no problem to instantiate List = dao. findAll (shen); System. out. print (list. size (); this row has no output results in the console, List list = dao. findAll (shen) and findAll (shen) are the methods for returning the List set by running the query on the database based on the specified condition shen. This is a problem with the database query (I feel really amazing at a time, I am more proud and complacent, and I can't stand myself.) I immediately wrote a static void main () method test, which is indeed a question for the Hibernate proxy database operation class, the prompt "Can not Connection" cannot be connected. Why is the class? Unable to connect. I have configured the MySQl connection settings in the DB Browser, and connected the database and table, how to Build the Hibernate database proxy class can not connect to the class, I immediately transferred to hibernate. cfg. the xml Hibernate configuration file, at first glance, is dizzy. How can I use the previous configuration settings in the configuration file? Didn't I change it to MySql? I know, you have selected a database connection object in the DB Browser, but haven't changed the Hibernate database connection configuration. It's dizzy. How is MyEclipse so stupid? I don't know how to automatically change the Hibernate configuration file, after manual modification, debug and run the program. I thought it would be okay this time, but the result is always unexpected. The old one is contrary to what I think, and there is a long exception: what Error Creating SessionFactory and other large pieces of English make me head big, click exception to see, it is to those Hibernate those hit seven hit eight of those packages in the original type, class N multi-generation Code, really annoying to read, think of it is depressing, this MyEclipse debugging is really JB trouble, and all the English version (think. net debugging, alas ''' I personally think that there are too many jar packages in Java, and some important settings are saved in the Xml configuration file, I feel like getting angry with the xml configuration file! Such as page navigation, but the content is complicated, you can only manually modify the Source), the heart of the Java spam, can not help but hold the back of the old spoon back to the back, looking at this depressing ceiling ING. Looking back at the MyEclipse tool, I looked down on the project and returned to the computer. I looked at it and looked at the packages that hit me. Ah, what seems to be missing, by intuition, I went through the jar package files again. I thought about it and read the packages carefully. My MySql database driver jar package file class! Why not? I did not see it again! I deleted the three packages that I previously connected to SQL Server 2000, but why didn't I import the MySql package? Why? Is it a DB Browser problem, where you choose Add JARS as the physical path of the drive package file, that is to say, he didn't automatically add the package you selected to your project. The damn DB Browser makes me unable to connect to Hibernate, this problem seems to be only when you used a data source and now change a new database connection, I open the project file directory, enter the WebRoot \ WEB-INF \ lib directory, manually Copy the MySQL database connection driver Jar package under my e drive to this directory, and Refresh it in the project. The MySql package will come out, run WebLogic to open the browser, onclick, ah, '''' finally came out, and the data in the database server was queried through JS. I implemented Ajax in Java! After reading so many articles, I think it's not funny. Do you think I am a spam, A cainiao? I am also a cainiao, if you are an expert, you may know that the error is there. You don't want to think about me like this! Did I mention some common sense classes that are very low-level in Java? But I am not innocent! I hope you will not laugh at me. I wrote this Hibernate knowledge for the first time. I have made some mistakes. Maybe some Java friends haven't encountered such a problem in some Hibernate, in fact, I am more interested in Java, but because I have learned too little Java knowledge, I do not know how to do something difficult, that's because I personally learned Java too superficial. I know it is actually in. net can basically be implemented in Java, but it may be better than in. net. On the Internet, many of my friends have obtained Hibernate and JAVA.. NET. NET comparison, many people say that the two cannot be compared, just as SQL statements cannot be compared with stored procedures, as long as they can be used properly, I agree with their views and opinions, but I personally think it is good to have my own views on things. I am a relatively talkative person. I think you only need to think about and analyze one thing, no matter what your conclusion is, whether or not you have learned something in this process. I don't like things that do not like to talk at ordinary times, because at that time, we didn't know what he thought about the things, it's hard to guess what he is thinking. Since Hibernate and ADO are mentioned above. net, so I added this paragraph again. Oh, well, I should review some of my previous knowledge. If I can't summarize it, I should just make a note, I hope the experts will not laugh at us. Next I will discuss Hibernate and ADO. NET comparison: in Hibernate, we first need to add a reference to the Hibernate tag library in the JAVA Project (I use Hibernate 3.1 Core Libraries), in. net, I think it is the process of adding an assembly in using, import namespace SqlClient, OleDb, and so on. Then we need to open DB Brower to establish a database connection string, that is, name and password of the server, etc, then Hibernate saves the database connection information in hibernate. cfg. in xml (default), when connecting to the database, it is worth mentioning that the database connection driver package is required. Different databases have different driver packages, I personally think that different driver packages are used in And, because different databases may use different connection operations, such as the existence of a database in the form of a tablespace in Oracle, while others may be slightly different. After the connection is correctly configured, we can see the table corresponding to the database in the DB Brower. When you add Hibernate before connecting to the database, it will ask you to select the package, after the package is added, there will be three more classes. IBaseHibernateDAO, BaseHibernateDAO, and HibernateSessionfactory can be seen as an interface, an interface implementation class (implements), and a factory, how to implement it and its principles and mechanisms can be clearly explained by the younger generation. I am still self-aware, it's not good to wait for the hacker to be distorted by my Java knowledge, but I think the code in it is rarely simple and it's not hard to understand, after creating these three classes, I need to select the corresponding table Hibernate Reverse Engineering in DB Brower to add the corresponding DAO and Model. The Model is the database table object ing instance, each column in the database table acts as an attribute field of the Model class and adds the get and set rules Attribute accessors, while the DAO class encapsulates some common data access operations (insert, delete, update, select) on the table ), it has its own HibernateSQL Statement (not a general SQL statement), which is rarely written manually, but similar to the general SQL statement syntax, if a List set with a strongly typed Model is returned for query by attribute name, the table name is automatically added for each added table. hbm. xml file, which stores the specific information mapped from the database to the Hibernate table field, such as the primary key information and the Java type corresponding to the column length, note that when adding a table, it will ask you to select the ID Generator table information. Here, assigned (with the table primary key) and native (automatically increasing the primary key) are common ), because it will create different DAO and Model based on the information you select when creating the daos, for example, you may need an ID primary key in the constructor and some data access methods. For auto-growth, you do not need to enter an ID Primary Key. You can perform the Hibernate Reverse Engineering ing operation on multiple potentially used tables, you can complete most common operations on the database without writing a single piece of code, which is convenient to call in other places. I think this is the biggest benefit of Hibernate. net class, compared with Hibernate, I think it is a step by step, what DAO method and other code line writing, but I think this is nothing bad, we usually use a SqlHelper class in the project, which includes some addition, deletion, modification, and query methods for the database. It is also useful to dynamically return data sets (lists) or records based on incoming SQL statements, parameter groups, stored procedures, etc, at most, we also add the corresponding DAL to different instances, which basically satisfies our basic operations on the database. However, I think this can also be implemented in Java. I used to write a SqlHelper class in Java to replace Hibernate and use the JdbcOdbc database connection, which is also very practical, simple and clear, but we cannot ignore the advantages of Hibernate in some aspects such as performance. At least I think there is always a value for it, I don't know why I'm not making people feel depressed. I simply reviewed Yifan. I think there will certainly be many mistakes when I write this theory for the first time. Please criticize and correct me, I will be taught with humility!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.