How to use Hibernate JAVA SE
Currently it is easy to find the Hibernate is quickly becoming popular J2EE O / R mapping tools and data integration framework (if not the most popular). Hibernate for application developers operation of the relational database and provides a powerful tool for clarity. However, if you need to visit those in the external packaging in the J2EE web application in the entities, the situation how? You with the development of an independent separate application, but the same entities to access your data? Or you have to prepare additional web components of the data to the internal visit?
In many cases, these problems will arise. My situation is my company needs to be from a number of vendors, a variety of file formats imported into database records. I think I have frequently used method, which is prepared by Shell and SQL Jiaoben (and even stored procedures) to import data. However, because our data model too complex, I decided to web applications, as far as possible, the use of the existing entities, Spring DAO and services and the development of a custom J2SE command line data loading tools.
Big problem: how do you?
Now many Hibernate documentation and examples are bundled in containers. Whether or internal web application is a large application, the use of the Vessel Association. People have good reason to use it. Containers are designed to provide support for a variety of features, such as transaction processing, as well as threads. Today, these characteristics are developed medium-scale enterprise applications and the tools necessary. However, when you need to visit in containers outside entities, you know what? You are using the existing structure and code it, or from a different point of view to solve the problems, for instance, a completely separate development language? Of course, we do not have the right answer. In the remainder of this paper, I will describe my method: Spring is in reusable containers from the existing entities / POJO.
Initially, the scripting languages, such as Perl, Python, Ruby or even Tcl (Yes, I have done this before) appears to be many advantages. They can save a lot of time, the initial results can easily be, but also to avoid many potential complexity of Hibernate. It is completely possible that people only a few lines of code can be connected to the database, query results has been Print terminal screen or output to the log file. However, depending on your data model, things (always) will become quite complicated. For example, say you have a person table, which are a key form of address when we add the time of data, table address a lack of a correct insertion of data will lead to Table person can not be inserted. This is a very typical transaction processing aspects. Perhaps some people will say that the scripting language it is not difficult to resolve this problem, as in the main program, you like. But problems still exist, and why? Business logic is not in your application inside? Why is it necessary to write code again? Besides, this is not the only case, you have to repeat your work and business logic, this will bring the possibility of mistakes.
However, some people will think that also, they use their own feel that the most suitable tool. Perhaps you have already procedures outside the reasons with some kind of an independent structure, perhaps you will be independent of the database load and test data, and then through a variety of tests later moved to the products database; and perhaps you database maintenance outsourcing, you need only to relevant documents distributed to partners to enable them to deal with those issues. In short, there is always a lot of reasons not to use the existing Hibernate data layers. No Who is right or wrong, but if you can and are willing to use your application from the existing code, please up. I will tell you some ways, you can solve a lot of problems Oh.
If you think that can be configured in containers from the use of existing Hibernate object, then you begin to do things in their own hand is the management of all configuration items in the remainder of this paper I, the method used was based on the use of the command line JAVA program. Since you have already configured Hibernate XML configuration file, you should be aware of the need to provide the parameters, such as JNDI DataSource name, entity mapping file, and there are other ways of handling SQL log attributes. If you want to use a command line application, then you need to address how XML documents and analytical add it to the configuration of these issues. Although it is not difficult analytical XML documents, but that in itself is not our focus. I propose, therefore, propetries document properties file comparison intuitive and easy loading and obtain data. Below are the Hibernate configuration required by the smallest attribute set (not including any entity mappings).
List 1:
Hibernate.dialect = net.sf.hibernate.dialect.PostgreSQLDialect
Hibernate.connection.driver_class = org.postgresql.Driver
Hibernate.connection.url = jdbc: postgresql: / / devserver / devdb
Hibernate.connection.username dbuser =
Hibernate.connection.password = dbpassword
Hibernate.query.substitutions yes' Y '
As you can see, the above specified attribute value of the database dialect, JDBC driver, url database, user name, user passwords, as well as whether or not to use Find replacement. As long as the above definition of values and the preservation of several hibernate.properties, in the document (to be placed in the path of your inside oh), it should be easy loading, filling inside to Hibernate Configuation category.
List 2:
Properties props = new Properties ();
Try (
Props.load (props.getClass (). GetResourceAsStream ( "hibernate.properties"));
) Catch (Exception e) (
System.out.println ( "Error loading hibernate properties.");
Tags: hibernate






