Reputation: 1404
I want to get the record from database by id. Below you can see my query:
public StudentEntity getStudentById(int id) {
Session session = sessionFactory.openSession();
Query query = session.createQuery("from StudentEntity where idstudent = :id").setParameter("id", id);
StudentEntity studentEntity = (StudentEntity) query.uniqueResult();
session.close();
return studentEntity;
}
My StudentEntity class you can see here
My stack trace you can see here With other tables I don't get such error. That's why I suppose that the mistake is in StudentEntity class. What can be reason for this error? How can I solve it? Github page of project here
Upvotes: 0
Views: 734
Reputation: 108370
Q: What can be reason for this error?
A: The stack trace reveals that MySQL is returning error 1064
You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version
for the right syntax to use near
'as docExpir4_3_, studentent0_.docIssueOrgnazation as docIssue5_3_, studentent0_.'
at line 1
This means that Hibernate has attempted to prepare or execute a SQL statement that MySQL doesn't understand. There's a syntax error of some kind. We're going to rule out the use of a reserved word as a column name, as Hibernate should be qualifying column references in SELECT statements. And I think we can rule out an invalid column reference, because MySQL would return a different error (1054) for that condition.
Q: How can I solve it?
A: Capture the SQL being executed.
Enable either DEBUG or TRACE for Hibernate, so it logs the SQL statements it is executing... in the log4j configuration, either
log4j.logger.org.hibernate.SQL=DEBUG
log4j.logger.org.hibernate.type=TRACE
There really is no substitute for learning how to debug programs that you write.
With that said, MySQL column names that include a space need to be properly escaped. For example, if we execute this SQL statement:
SELECT foo.fee fi AS fo FROM fum foo
MySQL server throws a 1064 syntax error "near 'AS fo '
"
We don't get that syntax error if we enclose the column name in backtick characters.
SELECT foo.`fee fi` AS fo FROM fum foo
^ ^
With Hibernate native API, we can get the column name enclosed in single backticks.
@Column(name = "`fee fi`")
Using JPA, we can enclose in double quotes
@Column(name = "\"fee fi\"")
Upvotes: 2
Reputation: 19956
The reason from the stack trace
'as docExpir4_3_, studentent0_.docIssueOrgnazation as docIssue5_3_, studentent0_.'
One of the columns doesn't have a name studentent0_.
. Please, check StudentEntity
class.
At least this is incorrect
@Column(name = "Payment type", nullable = true, length = 45)
public String getPaymentType() {
return paymentType;
}
@Column(name = "docExpire time", nullable = true)
public Date getDocExpireTime() {
return docExpireTime;
}
Upvotes: 1