There is HQL query syntax in Hibernate. But what we are more familiar with is counting SQL statements. So how should we make Hibernate support SQL? We don't need to consider this, the Hibernate team has already done it.
Let’s not talk nonsense, just take an example.
select * from t_user usr
The above is an SQL statement, and it is nonsense, which everyone knows. What should we do if Hibernate wants to execute this statement? Look at the code:
Query query = session.createSQLQuery("select * from t_user usr"); That's it, everyone should know what's left, and what's going on inquiries are normal.
Then what is returned after the query?
while(iter.hasNext()){ Object[] objs = (Object[])iter.next(); for (int i = 0; i < objs.length; i++) { System.out.print(objs[i]); } System.out.println(); } Each result returned is an Object[] array.
At this time, someone came out and said it was object-oriented. Yes, it's object-oriented, alas, there's no way.
Let's continue to watch:
select {usr.*} from t_user usr Seeing this, I guess some children's shoes are starting to move. What's the braces?
Don't worry, take your time. Let's continue reading the code first.
The code copy is as follows:
Query query = session.createSQLQuery("select {usr.*} from t_user usr").addEntity(TUser.class);
addEntitySQLQuery addEntity(String tableAlias, Class entityType)Declare a "root" entityParameters:tableAlias - The SQL table aliasentityType - The java type of the entity to add as a root
It's the same as it is, it's a slight suck. You can only use it yourself.
The first parameter refers to the alias of the table. Just like the statement above, the alias of our table is usr, so the first parameter is usr, and the second refers to which class the query results need to be mapped to. Here, since we map to the t_user table through TUser in the mapping file, we are of course TUser here. Then after checking, there are SQL statements, and the result is of TUser type.
The results we found are:
org.hibernate.tutorial.domain6.TUser@198cb3d
Of course, yours must be different from mine. Don't move the chicken.
Maybe we don't need to find out everything, at this time, all we need is to set the alias:
select u.id as {usr.id},u.name as {usr.name},u.age as {usr.age} from t_user u We see that we used as to specify the alias for the field, and the same is true in the program:
The code copy is as follows:
Query query = session.createSQLQuery("select u.id as {usr.id},u.name as {usr.name},u.age as {usr.age} from t_user u").addEntity("usr",TUser.class);
<sql-query name="queryTUser"> <return alias="usr" entity-name="org.hibernate.tutorial.domain6.TUser" /> select {usr.*} from t_user usr where name=:name </sql-query> Note that the entity-name here needs to be written with the complete package name, otherwise an error will be reported. Here we have the subtitle return, which specifies the alias and class name of the table, so that we don't need to addEntity at runtime.
Look at the code:
Query query = session.getNamedQuery("queryTUser"); query.setParameter("name","shun"); List list = query.list(); Iterator iter = list.iterator();
We are OK just like this. Note that we have not added addEntity, mainly due to the configuration in the configuration file.
Note that if configured in the configuration file, you must have the return subtag to specify the table alias and class name. This mainly avoids repeated judgments when reading statements.
After talking about it for so long, we have been talking about tables with alias. So what should we do if our table has no alias but we want to encapsulate the result in the object?
select * from t_user usr
It's very simple. Just call addEntity's overloaded method addEntity(Class clazz) and you only need to provide a class name, without the table alias.
Of course, hibernate also supports stored procedures. You only need to set the callable property of SQL-query to true in the configuration file to indicate that the currently called stored procedures. Since I don’t have much contact with stored procedures, I will study them more in the future and then study them with you.
When we call corresponding methods of data operations such as session.save, it will be converted into hibernate's built-in SQL statement, but what if we want to control the format of the SQL statement ourselves?
Hibernate actually thought of it too.
We directly add it to the mapping file:
<sql-insert> INSERT INTO T_USER (NAME,AGE) values (?,?) </sql-insert> <sql-update> UPDATE USER SET USER_NAME=?,AGE=? WHERE uSER_ID=? </sql-update>
Note that this needs to be added in the class tag as a subtitle. We are all capital letters here, to distinguish them from the default statements of hibernate, and have no other meaning.
Let's first look at the call to insert:
User user = new User(); user.setName("shun123123"); user.setAge(23); When we call save, the hibernate statement is:
Hibernate:
INSERT INTO USER(USER_NAME,AGE) values(?,?)
It calls the statements in the SQL-insert tag we configured, let's take a look at the call to update:
User user = (User)session.get(User.class,new Long(29)); user.setName("shun123123"); user.setAge(23); session.save(user); We call save, it will automatically call update, and the statement at this time is:
Hibernate:
UPDATE USER SET USER_NAME=?,AGE=? WHERE uSER_ID=?We see that the output statement is capitalized, which means that the statement we configured is called.