Suppose the current table structure is as follows:
Food table field has foodid,name, foreign key businessid, foreign key type
Business table field has, name, foreign key type
The Type table field has Id,name,foodid
The corresponding Pojo generated by Hibernate are Food,business,type, respectively.
You need to query Food table section fields, such as name and foreign key Businessid
You can add a constructor for the food class with only the corresponding member variable, food (String name,business business)
Using the HQL statement
Select from where foodid=1
The above can be queried smoothly, but if the name and order are reversed
Use food (business business,string name)
HQL statements
Select from where foodid=1
It will report a null pointer exception.
This is because the Foreign Key association table business also contains a field called name, so an error occurs, as long as the query table using an alias can be resolved.
The correct statement:
Select from where foodid=1
Similarly, if you also query the foreign key type, then:
The wrong statement:
Select from where foodid=1
The correct statement:
Select from where f.foodid=1
[Workaround] Hibernate query Partial field (with foreign key) error, report null pointer exception