Hibernate common needs, problems, abnormal finishing (continually updated)

1.org.hibernate.hql.internal.ast.QuerySyntaxException: Path expected for join!

wrong reason

Hibernate does not know join, replaced by "," on it

Examples of errors

String sql="select a,b.userlogo from AnswerPost  a left join User b where  a.answer_name=b.username and a.post_id=?";

Good example

String sql="select a,b.userlogo from AnswerPost  a , User b where  a.answer_name=b.username and a.post_id=?";

//==================================================

2.Unable to locate appropriate constructor on class

Check for full participation in the entity class constructor, each attribute name, type matches

//==================================================

3.hibernate hql query specified field / object and get the result set, multi-table query result set

In hibernate, the query statement with hql entity class, the method returns the result list using a List, a List of the encapsulated object divided into the following three cases: 
the case 1. The entire field of the query, such as "from the entity class" list encapsulated object for the entity class itself, it will get filled each attribute. 
2. Only a query field, by default, List Object object is encapsulated
3. two or more query fields, by default, List is encapsulated in the Object [] , the number of matches of the query length fields. 
For the latter two cases, when traversing the label is less convenient because the object can not be directly converted to the entity classes. The solution is relatively simple: 
の: hql in the select new package name class name (attribute 1, property 2 ......) from the entity class, the parameterized constructor simultaneously added in the entity class, and the number of parameters. order to keep the (attribute 1, attribute 2 ......) agreement , list so that we get the object is still stored in the entity class, the queries to the property have been filled, more convenient to use. 
の: hql query multiple tables some fields, SELECT package names new new entity class names in Table 1 (. Table 1. Properties 1, Table 2. Properties 2 ......) entity classes from Table 1, Table 2 where the entity class table 1.ID = table 2.ID (i.e., associated with the fields) , while the properties in table 2 was added and the constructor that takes a parameter, the number and order of the parameters (table 1. table 1 properties entity class to return 1, table 2 property 2 ......) consistent 

//==================================================

4.Hibernate query part field (containing the foreign key) error message NullPointerException

Suppose the current table is structured as follows:

  Field food has foodid, name, foreign key businessid, foreign key type

  Field have business, name, type foreign key

Table section need, food field, such as a name and a foreign key businessid

You can add only constructor corresponding member variables, Food (String name, Business business) in the Food category

select new Food(name,business) from Food where foodid=1

Above can successfully query, but if the exchange name and the order of business, the use of Food (Business business, String name)

select new Food(business, name) from Food where foodid=1

Null pointer exception will be reported

In fact, this is because the foreign key table Business also contains a field called name, so the error occurs, then just use an alias table to the query can be resolved.

select new Food(f.business, f.name) from Food f where foodid=1

Another: null pointer statement:

select new Food(name,business,type) from Food where foodid=1

Correct statement ( f. ):

select new Food(f.name,f.business,f.type) from Food f where f.foodid=1

The native SQL mapping properties given: the ResultSet and setter given:

1.
org.hibernate.exception.SQLGrammarException: could not extract ResultSet

Database field names do not correspond (i.e. sql property name should be used instead of the database field names and the like)

2.
org.hibernate.PropertyNotFoundException: Could not find setter for business_line on class com.

Lack setter methods:. .CreateSQLQuery (.. as alias ...) addScalar ( "alias") or an alias as (user_name as 'userName')

6. native SQL mapping table names being given (e.g., table name t_sys_user, the entity class name SysUser):

1. entity class name: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'library name table. (Sysuser: automatically transfer lowercase)' does not exist

2.使用表名:org.hibernate.hql.internal.ast.QuerySyntaxException: t_pm_project_approval is not mapped [select  p.id as id, ...

A hesitation: Since it is the native SQL, do not hesitate to use the entity class name or table name, uniform use of table fields, said the use of some online entity class name solution to the problem is most likely situation is different, there may be just coincidence, not really solve the problem of understanding (such as the entity class name User table name and user, then use the entity class name in lowercase automatically transferred exactly match the table name as user)!

3. If the error, see if it is followed by other operations the same sql, such as paging and paging data query query is a query, in other words, to go with a single point of commissioning to confirm the error location!

//==================================================

Continually updated

 

reference:

https://blog.csdn.net/qq_37591637/article/details/88910067

https://blog.csdn.net/leandzgc/article/details/21190953

https://www.cnblogs.com/QQParadise/articles/3437940.html

Published 65 original articles · won praise 8 · views 160 000 +

Guess you like

Origin blog.csdn.net/u012382791/article/details/99671292