Hiberante is a mechanism for dustries of database persistence, and Hibernate applications allow programmers to focus on business logic implementation. The principle of Hibernate is to encapsulate the database structure so that the programmer can call the database's associated interface to implement the relevant operations of the database. The results of the following examples, the environment J2SDK1.4.2_04, Tomcat4.1.27, Eclipse3.0, Hibernate2.1 database is as follows: Create Table Users (Logonid Varchar (255) Not Null, Emailaddress Varchar (255), Lastlogon Date, Password VARCHAR (255), Name Varchar (255), Primary Key (Logonid);
Create Table Address2 (ID VARCHAR (255) NOT NULL, City VARCHAR (255), State Varchar (255), Zip Varchar (255), Primary Key (ID)); Create Table Contacts (ID Bigint Not Null, EmailAddress Varchar (255 ), Name VARCHAR (255), User_ID VARCHAR (255), primary key (ID)); alter table Contacts add constraint FKE207C4735A7381EF foreign key (User_ID) references Users; create index IXE207C4735A7381EF on Contacts (User_ID);
After Eclipse installed the Hibernate plugin, many more cumbersome, easy error-in configuration files, Eclipse can help you have a good automatic generation, your main task is to implement DAO, that is, Eclipse's data access objects generated for each table. For example, this example's baseaddress2dao, contactsdao, usersdao, you can modify these three objects to implement the relevant operations of Contacts, Users, Address2. The structure of this example is as follows: where address2, contacts, and users are equivalent to entity beans, implement mapping of data tables. Since there is an ONE-TO-MANY relationship due to Table Users and Contacts, Contacts is also an object property in the implementation of users.java. Private java.util.set _Contactsset; / ** * Return the value associated with the column: contactsset * / public java.util.set getContactsset () {Return this._contactsset;
/ ** * Set the value related to the column: ContactsSet * @param _contactsSet the ContactsSet value * / public void setContactsSet (java.util.Set _contactsSet) {this._contactsSet = _contactsSet;} public void addToContactsSet (Object obj) {if (null == this._contactsset) this._contactsset = new java.util.hashset (); this._contactsset.add (}); The connection pool of the server is an configured connection parameter file.