简体   繁体   中英

Hibernate can't find existing string

I have a jetty server with hibernate connection to mysql.

When I try to get record, which was added to database before server start - it returns it successfully. But when I insert record to database from mysql console when server works and try to get this record with hibernate - it returns null.

public Book getBook(int id, Session session) throws SQLException {
    Book result = null;
    try {
        result = (Book) session.get(Book.class, id);
    }catch(Exception e) {
        e.printStackTrace();
    }finally {
        if((session != null) && session.isOpen())session.close();
    }
    return result;
}

I realy don't know how to solve it.

Open another MySQL session from a different terminal and check if you can see the data that is inserted in the database. If you cannot see it then the problem is with your insert operation not being committed.

If you can see it then Hibernate should also be able to see it since it should be issuing a similar query. To be certain you can turn on Hibernate logging to check what query is actually sent

log4j.logger.org.hibernate.SQL=DEBUG

log4j.logger.org.hibernate.type=TRACE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM