Hibernate study Notes (i)

Source: Internet
Author: User
Tags count implement key sql uuid



2005-3-3 learning Hibernate in learning hibernate, I found that for database sub fields in SQL server2000, the configuration file was implemented using the following methods: <id name= "id" type= "long" unsaved -value= "0" > <column name= "id" sql-type= "numeric" not-null= "true"/> Class = "Identity"/> <!--increment--> </id> here is mainly: identity: Represents a child-increment field provided by the SQL server2000 database itself. If you want to hibernate yourself For, then use the Increment keyword to achieve

If the primary key in the table is a string type: You can implement a primary key unique by Hibernate the method that you provide: <id name= "id" type= "string" unsaved-value= "null" > <column NA           Me= "CID" sql-type= "char" not-null= "true"/> <generator class= "Uuid.hex"/> </id> The Uuid.hex policy is used

Important Knowledge points:   1. If there are departmental tables, there are employee tables, and there are dep_id in the Employee table, the table department class and employee class are one-to-many relationships:   can use:  ( Use the following in departmental class department    department:       all employees in/**  department    */     Private Set staffs = new TreeSet ();        XML file:   <set name= " Staffs ">            <key column=" dep_id "/>              <one-to-many class= "Hbp.sys.data.Staff"/ >      </set>      If it is a list, you need to use the index <index> </ Index&gt, the meaning of the concrete, is not very clear. Pending research            2. If the department has a person in charge, that is, there is a field in the departmental table (tb_department):staff_id.     so that the relationship between the Department and the person in charge is many-to-one relationship       Department Class:      /** department head ID */    private Staff staff;& nbsp;   &NBsp;   XML file      <many-to-one name= "Staff" class= "Hbp.sys.data.Staff"   column= "staff_id"/>        2005-3-4    Learning hibernate         3. Multi-many-to-many relationship, we usually do an intermediate correlation table. I made an example with roles and privileges.,      Right (Id,name)       role (Id,name)    Intermediate table: Tb_role_right (role_id,right_id)       There is a set of role in the right class: Private set Roles=new TreeSet ();      The role class also has a right collection: private set Rights=new TreeSet ();      The two are obviously many-to-many relationships. Use Many-to-many to implement;       XML file       right.hbm.xml::      <set name= "Roles" below Table= "Tb_role_right" cascade= "All" >           <key Column= "right_id"/>           <many-to-many column= "role_id"class= "Hbp.sys.data.Role"/>         </set>     Role.hbm.xml file similar to:     <set name= "rights" table= "Tb_role_right" cascade= "All" >           <key column= "role_id"/>           <many-to-many column= "right_id" class= "Hbp.sys.data.Right"/>          </set>

2005-3-5    Learning hibernate      4. Several noteworthy issues:  a) in the XML? Mapping file, Write the name of the class must use the full name of the class: that is: Package + class name such as: (Hbp.sys.data.Staff), this error makes me a long time effort.:( &NBSP;B) When I wrote the basic DAO operation, I wrote the                  Session.delete ("from right as right where right.id=" +id);                   program Anyway, I toss a half-day, tracking in the end, just suddenly dawned, hibernate in parsing SQL statement when the               right, as the database in the right-hand link ("reserved word"), alas, this keyword, can not be used casually ah,:)                2005-3-7     The query in the HQL language in 5.hibernate returns different object types depending on your SQL .         if you use Session.find ( String hql)          typically returns a list, such as: From Staff Staff, which returns a collection of all Employee objects           as your hql: SELECT COUNT (*) from StaFF staff; Returns an integer object          if you are using the HQL: SELECT COUNT (Distinct Staff.name), COUNT (*) from staff staff, then returns a object         that is object[], Need to convert him to object[], and then in the [0],[1].         design I don't know how hibernate is handled, It feels both good and bad. It's OK to use a find to get any queries          bad is to hql to deal with return results, error prone .                


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.