Hibernate Query sort and federated primary key mappings

Source: Internet
Author: User

1. Query Ordering

(1) database sorting (recommended)

<order-by= "name ase">   <!--name Ascending, descending desc   -
Session.createquery ("  "). Uniqueresult ()  // returns a unique object with only one foreground object
<order-by= "name ASC">

(2) Memory sequencing

<sort= "natural">

There are three kinds of values for the sort attribute:

    • Natural, ascending
    • Unsorted, not sorted
    • The class name, the custom collation, implements the comparator interface, and implements the Compare method in the interface, and the name of the class is the Sort property.

string comparison: O1.compareto (O2);

2. Federated primary Key mapping rules:

Each primary key attribute in a class corresponds to each primary key column in the data table, and hibernate requires an entity class with a federated primary key:

    • Serializable interface must be implemented
    • Implement Hashcoad (), Equals (), overriding reason hibernate depends on the Federated primary key of the database to determine whether a two-row record is the same, or, if it is the same, a different object. This reaction to the field of procedure is based on the Hashcoad and Equals method to determine whether a two object can be placed in a set such as set. The Federated primary Key implements the interface reason, when using get (), load (), the object of the entity needs to be constructed, and the query is still set in (the Federated primary key) and then passed in as a second parameter of get () or load ().
 Public classStudent implements serializable{PrivateString name; PrivateString cardId; Private intAge ; @Override Public inthashcode () {Final intPrime = 31; intresult = 1; Result= Prime * result + ((cardId = =NULL) ? 0: Cardid.hashcode ()); Result= Prime * result + ((name = =NULL) ? 0: Name.hashcode ()); returnresult; } @Override Public Booleanequals (Object obj) {if( This==obj)return true; if(obj = =NULL)            return false; if(GetClass ()! =Obj.getclass ())return false; Student Other=(Student) obj; if(CardId = =NULL)        {            if(Other.cardid! =NULL)                return false; }        Else if(!cardid.equals (other.cardid))return false; if(Name = =NULL)        {            if(Other.name! =NULL)                return false; }        Else if(!name.equals (other.name))return false; return true; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString Getcardid () {returncardId; }     Public voidSetcardid (String cardId) { This. CardId =cardId; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }    }
<hibernate-mapping>    <classname= "Com.liule.hibernate.Student"Table= "Student">        <Composite-id>            <Key-propertyname= "CardId"column= "CardId"type= "string"></Key-property>            <Key-propertyname= "Name"column= "Name"type= "string"></Key-property>        </Composite-id>                < Propertyname= "Age"column= "Age"type= "int"></ Property>                    </class></hibernate-mapping>

Insert data:

 Public Static void Main (string[] args)    {        new  Student ();                Student.setage (ten);        Student.setcardid ("123");        Student.setname ("Zhangsan");                                 = sf.opensession ();         null;

Inquire:

 Public Static voidMain (string[] args) {Student Studentprimarykey=NewStudent (); Studentprimarykey.setcardid (10); Studentprimarykey.setname ("Zhangsan"); Session Session=sf.opensession (); Transaction TS=NULL; Try{Student Student= (Student) session.get (Student.class, Studentprimarykey);            System.out.println (Student.getage ());            System.out.println (Studentprimarykey.getage ()); TS=session.begintransaction ();        Ts.commit (); }

Hibernate Query sort and federated primary key mappings

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.