Hibernate's HQL syntax

Hibernate database operations:

First get the session object,

Session se = HibernateSessionFactory.getSession();

get the user object,

User user=new User();

user.setName("Zhang San");

Insert : se.save(user);

Delete : se.delete(user);

Modification : se.update(user);

Query :

   a) se.get(User.class,id);

   Query the data in the database table through the specified ID, parameter 1 refers to the entity that needs to execute the query; parameter 2 refers to the ID content of the query condition

   b) se.load(User.class,id);

   Query the data in the database table through the specified ID, parameter 1 refers to the entity that needs to execute the query; parameter 2 refers to the ID content of the query condition

The difference between get and load is that load belongs to a query operation of delayed loading. When the operation of the load method is obtained, the application will not directly access the database. Only when the object returned by the method operates the member will it interact with the data, while get belongs to An even-loaded query operation that interacts directly with the database when calling the get method.

   c) CreateSQLQuery (parameter)

    Perform database interactive operations through specified SQL statements

    Parameters: SQL statement

    String sql="select * from user";

    se.creatSQLQuery(sql).list();

   d)CreatQuery(parameter)

    Perform database interactive operations through specified HQL statements

    Features: Use object mode to complete database operations

    grammar:

            SQL: select * from table name where field name = value;

            HQL: from object name where attribute name = value;

    Query all: se.creatQuery("from user");

    Conditional query:

    方式一:  Query q = se.creatQuery("from user where UId = ?");

                     q.setParameter(0,1);//Set the value of UId;

    方式二:   Query q = se.creatQuery("from user where UId = id");

                     q.setParameter("id",1);//Set the value of UId;

   d)se.creatCriteria(User.class);

    An application-based query method that does not support SQL statements.

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325340383&siteId=291194637