我只是在学习如何使用 Hibernate 进行数据库建模/映射。我正在使用以下代码片段来获取用户模型。
Session session = HibernateUtil.getSessionFactory().openSession();
User user = (User) session.get(User.class, id);
session.close();
用户的配置文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="c3.data.User">
<id name="id" type="java.lang.Integer">
<generator class="identity"/>
</id>
<property name="username" type="java.lang.String"/>
<property name="password" type="java.lang.String"/>
<property name="email" type="java.lang.String"/>
<property name="salt" type="java.lang.String"/>
<one-to-one name="authSession" property-ref="user" class="c3.data.AuthSession"/>
<set name="projects" inverse="true">
<key column="projectLeadId"/>
<one-to-many class="c3.data.Project"/>
</set>
</class>
</hibernate-mapping>
如您所见,它与称为 AuthSession 和 Project 的其他模型有关系。如有必要,我可以发布他们的配置文件。
我遇到的问题是当我调用 Session.get() 时,它永远不会返回;它只是挂起。Hibernate 向我展示了查询,它为用户、AuthSession 和项目运行查询。除了没有确认参数的值之外,查询看起来是正确的。在查询项目之后,没有任何反应。我在调用 get 之后立即放置了一个 print 语句,但它永远不会被打印出来。
我不知道还有什么要检查的,看看可能出了什么问题。有什么想法吗?
编辑: 有一个例外,这是堆栈跟踪:
java.lang.NullPointerException
at org.hibernate.persister.entity.AbstractEntityPersister.loadByUniqueKey(AbstractEntityPersister.java:2314)
at org.hibernate.type.EntityType.loadByUniqueKey(EntityType.java:664)
at org.hibernate.type.EntityType.resolve(EntityType.java:444)
at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:168)
at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:134)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:999)
at org.hibernate.loader.Loader.doQuery(Loader.java:878)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:293)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:263)
at org.hibernate.loader.Loader.loadCollection(Loader.java:2094)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:61)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:678)
at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:82)
at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1801)
at org.hibernate.collection.internal.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:661)
at org.hibernate.engine.internal.StatefulPersistenceContext.initializeNonLazyCollections(StatefulPersistenceContext.java:1014)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:298)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:263)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1977)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:82)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:72)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3821)
at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:458)
at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:427)
at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:204)
at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:260)
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:148)
at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1075)
at org.hibernate.internal.SessionImpl.access$2000(SessionImpl.java:175)
at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.load(SessionImpl.java:2421)
at org.hibernate.internal.SessionImpl.get(SessionImpl.java:971)
at c3.data.UserManager.getUser(UserManager.java:22)
at c3.data.AuthSessionManager.sessionExists(AuthSessionManager.java:14)
at c3.console.Server.logon(Server.java:289)
at c3.console.Server.onToken(Server.java:242)
at tokenwebsocket.server.TokenWebSocketServer.onMessage(TokenWebSocketServer.java:37)
at org.java_websocket.server.WebSocketServer.onWebsocketMessage(WebSocketServer.java:457)
at org.java_websocket.WebSocketImpl.deliverMessage(WebSocketImpl.java:561)
at org.java_websocket.WebSocketImpl.decodeFrames(WebSocketImpl.java:328)
at org.java_websocket.WebSocketImpl.decode(WebSocketImpl.java:149)
at org.java_websocket.server.WebSocketServer$WebSocketWorker.run(WebSocketServer.java:593)