嗨我有记录问题,首先我在数据库中注册用户,这个工作正常!然后,当我尝试使用该用户(用户名和密码)进行登录时,应用程序会转发异常:
` [ERROR] ioc.Registry id to load is required for loading
[ERROR] ioc.Registry Operations trace:
[ERROR] ioc.Registry [ 1] Triggering event 'action' on Login:loginform
[ERROR] ioc.Registry [ 2] Triggering event 'validate' on Login:username
[ERROR] TapestryModule.RequestExceptionHandler Processing of request failed with uncaught exception: id to load is required for loading
org.apache.tapestry5.runtime.ComponentEventException: id to load is required for loading [at classpath:com/fit/pages/Login.tml, line 7]
at org.apache.tapestry5.internal.structure.ComponentPageElementImpl.processEventTriggering(ComponentPageElementImpl.java:1152)
at org.apache.tapestry5.internal.structure.ComponentPageElementImpl.access$3000(ComponentPageElementImpl.java:72)
at org.apache.tapestry5.internal.structure.ComponentPageElementImpl$7.invoke(ComponentPageElementImpl.java:1077)
at org.apache.tapestry5.internal.structure.ComponentPageElementImpl$7.invoke(ComponentPageElementImpl.java:1074)
at org.apache.tapestry5.ioc.internal.OperationTrackerImpl.invoke(OperationTrackerImpl.java:65)
at org.apache.tapestry5.ioc.internal.PerThreadOperationTracker.invoke(PerThreadOperationTracker.java:68)
at org.apache.tapestry5.ioc.internal.RegistryImpl.invoke(RegistryImpl.java:1063)
at org.apache.tapestry5.internal.structure.ComponentPageElementResourcesImpl.invoke(ComponentPageElementResourcesImpl.java:141)
at org.apache.tapestry5.internal.structure.ComponentPageElementImpl.triggerContextEvent(ComponentPageElementImpl.java:1073)
at org.apache.tapestry5.internal.services.ComponentEventRequestHandlerImpl.handle(ComponentEventRequestHandlerImpl.java:81)
at org.apache.tapestry5.internal.services.ImmediateActionRenderResponseFilter.handle(ImmediateActionRenderResponseFilter.java:42)
at $ComponentEventRequestHandler_13194654f69.handle($ComponentEventRequestHandler_13194654f69.java)
at org.apache.tapestry5.internal.services.AjaxFilter.handle(AjaxFilter.java:42)
at $ComponentEventRequestHandler_13194654f69.handle($ComponentEventRequestHandler_13194654f69.java)
at org.apache.tapestry5.services.TapestryModule$39.handle(TapestryModule.java:2583)
at $ComponentEventRequestHandler_13194654f69.handle($ComponentEventRequestHandler_13194654f69.java)
at $ComponentEventRequestHandler_13194654e1b.handle($ComponentEventRequestHandler_13194654e1b.java)
at org.apache.tapestry5.internal.services.ComponentRequestHandlerTerminator.handleComponentEvent(ComponentRequestHandlerTerminator.java:43)
at org.apache.tapestry5.services.InitializeActivePageName.handleComponentEvent(InitializeActivePageName.java:39)
at $ComponentRequestHandler_13194654e1d.handleComponentEvent($ComponentRequestHandler_13194654e1d.java)
at $ComponentRequestHandler_13194654e01.handleComponentEvent($ComponentRequestHandler_13194654e01.java)
at org.apache.tapestry5.internal.services.ComponentEventDispatcher.dispatch(ComponentEventDispatcher.java:46)
at $Dispatcher_13194654e03.dispatch($Dispatcher_13194654e03.java)
at $Dispatcher_13194654dfe.dispatch($Dispatcher_13194654dfe.java)
at org.apache.tapestry5.services.TapestryModule$RequestHandlerTerminator.service(TapestryModule.java:321)
at org.apache.tapestry5.internal.services.RequestErrorFilter.service(RequestErrorFilter.java:26)
at $RequestHandler_13194654dff.service($RequestHandler_13194654dff.java)
at org.apache.tapestry5.services.TapestryModule$4.service(TapestryModule.java:984)
at $RequestHandler_13194654dff.service($RequestHandler_13194654dff.java)
at org.apache.tapestry5.services.TapestryModule$3.service(TapestryModule.java:974)
at $RequestHandler_13194654dff.service($RequestHandler_13194654dff.java)
at org.apache.tapestry5.internal.services.StaticFilesFilter.service(StaticFilesFilter.java:90)
at $RequestHandler_13194654dff.service($RequestHandler_13194654dff.java)
at org.apache.tapestry5.internal.services.CheckForUpdatesFilter$2.invoke(CheckForUpdatesFilter.java:90)
at org.apache.tapestry5.internal.services.CheckForUpdatesFilter$2.invoke(CheckForUpdatesFilter.java:80)
at org.apache.tapestry5.ioc.internal.util.ConcurrentBarrier.withRead(ConcurrentBarrier.java:85)
at org.apache.tapestry5.internal.services.CheckForUpdatesFilter.service(CheckForUpdatesFilter.java:103)
at $RequestHandler_13194654dff.service($RequestHandler_13194654dff.java)
at $RequestHandler_13194654df5.service($RequestHandler_13194654df5.java)
at org.apache.tapestry5.services.TapestryModule$HttpServletRequestHandlerTerminator.service(TapestryModule.java:272)
at org.apache.tapestry5.internal.gzip.GZipFilter.service(GZipFilter.java:53)
at $HttpServletRequestHandler_13194654df7.service($HttpServletRequestHandler_13194654df7.java)
at org.apache.tapestry5.internal.services.IgnoredPathsFilter.service(IgnoredPathsFilter.java:62)
at $HttpServletRequestFilter_13194654df4.service($HttpServletRequestFilter_13194654df4.java)
at $HttpServletRequestHandler_13194654df7.service($HttpServletRequestHandler_13194654df7.java)
at org.apache.tapestry5.services.TapestryModule$2.service(TapestryModule.java:928)
at $HttpServletRequestHandler_13194654df7.service($HttpServletRequestHandler_13194654df7.java)
at $HttpServletRequestHandler_13194654df2.service($HttpServletRequestHandler_13194654df2.java)
at org.apache.tapestry5.TapestryFilter.doFilter(TapestryFilter.java:147)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1148)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:387)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:417)
at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230)
at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:324)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:535)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:880)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:747)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:520)
Caused by: org.apache.tapestry5.ioc.internal.util.TapestryException: id to load is required for loading [at classpath:com/fit/pages/Login.tml, line 15]
at org.apache.tapestry5.corelib.components.Form.executeStoredActions(Form.java:681)
at org.apache.tapestry5.corelib.components.Form._$advised$onAction(Form.java:542)
at org.apache.tapestry5.corelib.components.Form$onAction$invocation_13194657e97.invokeAdvisedMethod(Form$onAction$invocation_13194657e97.java)
at org.apache.tapestry5.internal.services.AbstractComponentMethodInvocation.proceed(AbstractComponentMethodInvocation.java:77)
at org.apache.tapestry5.ioc.internal.services.LoggingAdvice.advise(LoggingAdvice.java:37)
at org.apache.tapestry5.internal.transform.LogWorker$1.advise(LogWorker.java:54)
at org.apache.tapestry5.internal.services.AbstractComponentMethodInvocation.proceed(AbstractComponentMethodInvocation.java:86)
at org.apache.tapestry5.corelib.components.Form.onAction(Form.java)
at org.apache.tapestry5.corelib.components.Form$MethodAccess_onAction_13194654e6d.invoke(Form$MethodAccess_onAction_13194654e6d.java)
at org.apache.tapestry5.internal.transform.BaseEventHandlerMethodInvoker.invokeEventHandlerMethod(BaseEventHandlerMethodInvoker.java:52)
at org.apache.tapestry5.internal.transform.OnEventWorker$4.invokeEventHandlers(OnEventWorker.java:157)
at org.apache.tapestry5.internal.transform.OnEventWorker$4.advise(OnEventWorker.java:136)
at org.apache.tapestry5.internal.services.AbstractComponentMethodInvocation.proceed(AbstractComponentMethodInvocation.java:86)
at org.apache.tapestry5.corelib.components.Form.dispatchComponentEvent(Form.java)
at org.apache.tapestry5.internal.structure.ComponentPageElementImpl.dispatchEvent(ComponentPageElementImpl.java:950)
at org.apache.tapestry5.internal.structure.ComponentPageElementImpl.processEventTriggering(ComponentPageElementImpl.java:1132)
... 65 more
Caused by: org.apache.tapestry5.ioc.internal.OperationException: id to load is required for loading [at classpath:com/fit/pages/Login.tml, line 15]
at org.apache.tapestry5.ioc.internal.OperationTrackerImpl.logAndRethrow(OperationTrackerImpl.java:102)
at org.apache.tapestry5.ioc.internal.OperationTrackerImpl.invoke(OperationTrackerImpl.java:69)
at org.apache.tapestry5.ioc.internal.PerThreadOperationTracker.invoke(PerThreadOperationTracker.java:68)
at org.apache.tapestry5.ioc.internal.RegistryImpl.invoke(RegistryImpl.java:1063)
at org.apache.tapestry5.internal.structure.ComponentPageElementResourcesImpl.invoke(ComponentPageElementResourcesImpl.java:141)
at org.apache.tapestry5.internal.structure.ComponentPageElementImpl.triggerContextEvent(ComponentPageElementImpl.java:1073)
at org.apache.tapestry5.internal.structure.ComponentPageElementImpl.triggerEvent(ComponentPageElementImpl.java:1046)
at org.apache.tapestry5.internal.structure.InternalComponentResourcesImpl.triggerEvent(InternalComponentResourcesImpl.java:277)
at org.apache.tapestry5.internal.services.FieldValidationSupportImpl.validate(FieldValidationSupportImpl.java:164)
at $FieldValidationSupport_13194654e8f.validate($FieldValidationSupport_13194654e8f.java)
at org.apache.tapestry5.corelib.base.AbstractTextField.processSubmission(AbstractTextField.java:211)
at org.apache.tapestry5.corelib.base.AbstractField.processSubmission(AbstractField.java:190)
at org.apache.tapestry5.corelib.base.AbstractField.access$100(AbstractField.java:38)
at org.apache.tapestry5.corelib.base.AbstractField$ProcessSubmission.execute(AbstractField.java:95)
at org.apache.tapestry5.corelib.base.AbstractField$ProcessSubmission.execute(AbstractField.java:89)
at org.apache.tapestry5.corelib.components.Form.executeStoredActions(Form.java:666)
... 80 more
Caused by: org.apache.tapestry5.runtime.ComponentEventException: id to load is required for loading [at classpath:com/fit/pages/Login.tml, line 15]
at org.apache.tapestry5.internal.structure.ComponentPageElementImpl.processEventTriggering(ComponentPageElementImpl.java:1152)
at org.apache.tapestry5.internal.structure.ComponentPageElementImpl.access$3000(ComponentPageElementImpl.java:72)
at org.apache.tapestry5.internal.structure.ComponentPageElementImpl$7.invoke(ComponentPageElementImpl.java:1077)
at org.apache.tapestry5.internal.structure.ComponentPageElementImpl$7.invoke(ComponentPageElementImpl.java:1074)
at org.apache.tapestry5.ioc.internal.OperationTrackerImpl.invoke(OperationTrackerImpl.java:65)
... 94 more
Caused by: java.lang.IllegalArgumentException: id to load is required for loading
at org.hibernate.event.LoadEvent.<init>(LoadEvent.java:74)
at org.hibernate.event.LoadEvent.<init>(LoadEvent.java:56)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:840)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:836)
at $Session_13194654f6e.get($Session_13194654f6e.java)
at $Session_13194654dd6.get($Session_13194654dd6.java)
at com.fit.hibernate.UsersServiceHibernate.retriveSingleUser(UsersServiceHibernate.java:41)
at $Users_13194654f6a.retriveSingleUser($Users_13194654f6a.java)
at $Users_13194654f6d.retriveSingleUser($Users_13194654f6d.java)
at $Users_13194654e62.retriveSingleUser($Users_13194654e62.java)
at com.fit.util.Security.authenticate(Security.java:11)
at com.fit.pages.Login.onValidate(Login.java:50)
at com.fit.pages.Login$MethodAccess_onValidate_13194654e64.invoke(Login$MethodAccess_onValidate_13194654e64.java)
at org.apache.tapestry5.internal.transform.BaseEventHandlerMethodInvoker.invokeEventHandlerMethod(BaseEventHandlerMethodInvoker.java:52)
at org.apache.tapestry5.internal.transform.OnEventWorker$4.invokeEventHandlers(OnEventWorker.java:157)
at org.apache.tapestry5.internal.transform.OnEventWorker$4.advise(OnEventWorker.java:136)
at org.apache.tapestry5.internal.services.AbstractComponentMethodInvocation.proceed(AbstractComponentMethodInvocation.java:86)
at com.fit.pages.Login.dispatchComponentEvent(Login.java)
at org.apache.tapestry5.internal.structure.ComponentPageElementImpl.dispatchEvent(ComponentPageElementImpl.java:942)
at org.apache.tapestry5.internal.structure.ComponentPageElementImpl.processEventTriggering(ComponentPageElementImpl.java:1132)
... 98 more
这是我的代码:
@Entity
public class UserBean implements User {
@Id @GeneratedValue
private long id;
@Basic
private String userName;
@Basic
private String fullName;
@Basic
private String password;
@Basic
private String email;
@Basic
private Gender gender;
@Basic
private AccountRole role;
public UserBean() {
}
public UserBean( String userName, String password
) {
this.userName = userName;
this.password = password;
}
/* (non-Javadoc)
* @see com.fit.beans.User#getId()
*/
public long getId() {
return id;
}
/* (non-Javadoc)
* @see com.fit.beans.User#setId(long)
*/
public void setId(long id) {
this.id = id;
}
/* (non-Javadoc)
* @see com.fit.beans.User#getUserName()
*/
@Validate("required")
public String getUserName() {
return userName;
}
/* (non-Javadoc)
* @see com.fit.beans.User#setUserName(java.lang.String)
*/
public void setUserName(String userName) {
this.userName = userName;
}
/* (non-Javadoc)
* @see com.fit.beans.User#getFullName()
*/
@Validate("required")
public String getFullName() {
return fullName;
}
/* (non-Javadoc)
* @see com.fit.beans.User#setFullName(java.lang.String)
*/
public void setFullName(String fullName) {
this.fullName = fullName;
}
/* (non-Javadoc)
* @see com.fit.beans.User#getPassword()
*/
@Validate("required")
public String getPassword() {
return password;
}
/* (non-Javadoc)
* @see com.fit.beans.User#setPassword(java.lang.String)
*/
public void setPassword(String password) {
this.password = password;
}
/* (non-Javadoc)
* @see com.fit.beans.User#getEmail()
*/
@Validate("required")
public String getEmail() {
return email;
}
/* (non-Javadoc)
* @see com.fit.beans.User#setEmail(java.lang.String)
*/
public void setEmail(String email) {
this.email = email;
}
/* (non-Javadoc)
* @see com.fit.beans.User#getGender()
*/
public Gender getGender() {
return gender;
}
/* (non-Javadoc)
* @see com.fit.beans.User#setGender(com.fit.enums.Gender)
*/
public void setGender(Gender gender) {
this.gender = gender;
}
/* (non-Javadoc)
* @see com.fit.beans.User#getRole()
*/
public AccountRole getRole() {
return role;
}
/* (non-Javadoc)
* @see com.fit.beans.User#setRole(com.fit.enums.AccountRole)
*/
public void setRole(AccountRole role) {
this.role = role;
}
}
登录页面:
public class Login {
private String userName;
@Property
private String password;
@Inject
@Property
private Users users;
@SessionState
private User user;
@Component(id="loginForm")
private Form loginForm;
@Inject
private Messages messages;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
void onValidate(){
User authenticatedUser = Security.authenticate(userName, password , users);
if(authenticatedUser != null){
user = authenticatedUser;
}else{
loginForm.recordError(messages.get("authentication-failed"));
}
}
Object onSuccess()
{
return Index.class;
}
security.java
public class Security {
public static User authenticate(String userName,String password, Users users ){
User login = null;
login = users.retriveSingleUser(userName);
if(login == null) return null;
if(login.getPassword().equals(password)){
return login;
}
return null;
}
}
usersBean.java
public class UsersBean implements Users {
private Map<String, User> users;
public UsersBean(Map<String, User> users) {
this.users = users;
}
public UsersBean() {
users = new HashMap<String, User>();
}
/* (non-Javadoc)
* @see com.fit.service.Users#save(com.fit.interfaces.User)
*/
@CommitAfter
public User save(User user){
return users.put(user.getUserName(), user);
}
/* (non-Javadoc)
* @see com.fit.service.Users#retrive()
*/
public Collection<User> retrive(){
return users.values();
}
/* (non-Javadoc)
* @see com.fit.service.Users#retriveSingleUser(java.lang.String)
*/
public User retriveSingleUser(String userName){
return users.get(userName);
}
/* (non-Javadoc)
* @see com.fit.service.Users#retriveSingleUser(java.io.Serializable)
*/
// public User retriveSingleUser(Serializable id){
// return users.get(id);
// }
}
usersServiceHibernate.java
public class UsersServiceHibernate implements Users {
private Session session;
public UsersServiceHibernate(Session session) {
this.session = session;
}
@Override
public User save(User user) {
// TODO Auto-generated method stub
return (User) session.get(UserBean.class, session.save(user));
}
@Override
public Collection<User> retrive() {
Criteria criteria = session.createCriteria(UserBean.class);
return criteria.list();
}
@Override
public User retriveSingleUser(String userName) {
// TODO Auto-generated method stub
return (User) session.get(UserBean.class, userName);
}
// public User retriveSingleUser(Serializable id) {
// // TODO Auto-generated method stub
// return (User) session.get(UserBean.class, id);
// }
}
的hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="">
<property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="hibernate.connection.url">jdbc:hsqldb:mem:name</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_value">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping class="com.fit.beans.UserBean"/>
<mapping package="com.fit.beans"/>
</session-factory>
</hibernate-configuration>
如何使hibernate方法从数据库中读取数据???
答案 0 :(得分:4)
我错过了你的用户的代码,很可能会在你的异常中出现一堆“由”引起的堆栈跟踪,这会引发你的问题(检查你的日志,而不仅仅是tapestry错误页面)。您似乎将服务层,DAO层和模型层全部混合在一起。你也在地图中缓存你的用户,我似乎找不到实际的hibernate会话调用的跟踪。我试图追踪那些非常肯定你的问题就在那里。
session.get(UserBean.class, userName);
接受UserBean的主键,但在您的情况下,UserBean似乎是一个没有任何映射关联的服务。此外,通常主键是Long
。要通过用户名检索UserBean,请查看hiberante criteria documentation。
当您使用null主键调用get时,可能会发生异常。但是你需要为我发布完整的堆栈跟踪以确定。
我建议你好好看看tapestry hibernate例子,因为你正在为这样一个难以理解的项目做好准备。
祝你好运, Joost的