2013-11-15 57 views
1

我試圖來從表,我沒有得到任何東西,但在日誌文件中一些錯誤的數據取出由休眠數據......請幫助我的人問題struts2的使用

catalina.2013-11-15。日誌

15-Nov-2013 11:13:32.521 INFO [http-apr-8080-exec-2] < unknown>。 <未知> HCANN000001:Hibernate Commons Annotations {4.0.2.Final}

2013年11月15日11:13:32.557 INFO [http-apr-8080-exec-2] null.null HHH000412:Hibernate Core {4.2 .7.Final}

15-NOV-2013 11:13:32.568 INFO [HTTP-APR-8080-EXEC-2] null.null HHH000206:找不到

hibernate.properties

15-NOV-2013 11:13:32.577 INFO [http-apr-8080-exec-2] null.null HHH000021:字節碼提供程序名稱:javassist

15-Nov-2013 11:13:32.693 INFO [http-apr-8080-exec -2] null.null HHH000043:從資源配置:/ hibernate.cfg.xml中

15-NOV-2013 11:13:32.696 INFO [HTTP-APR-8080-EXEC-2] null.null HHH000040:配置資源:/hibernate.cfg.xml

GetAllUserAction.java

public String execute() { 
    UserServiceDao userServiceDao = new UserServiceImpl(); 
    User user = new User(); 
    users = new ArrayList<User>(); 

    try { 
     users = userServiceDao.fetchService(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    return "SUCCESS"; 
} 

public List getUsers() { 
    return this.users; 
} 

public void setUsers(List users) { 
    this.users = users; 
} 

public int getId() { 
return id; 
} 

public void setId(int id) { 
this.id = id; 
} 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public String getUserName() { 
    return userName; 
} 

public void setUserName(String userName) { 
    this.userName = userName; 
} 

public String getPassword() { 
    return password; 
} 

public void setPassword(String password) { 
    this.password = password; 
} 

public String getAddress() { 
    return address; 
} 

public void setAddress(String address) { 
    this.address = address; 
} 

public String getCity() { 
    return city; 
} 

public void setCity(String city) { 
    this.city = city; 
} 

public String getEmail() { 
    return email; 
} 

public void setEmail(String email) { 
    this.email = email; 
} 

public int getContactNumber() { 
    return contactNumber; 
} 

public void setContactNumber(int contactNumber) { 
    this.contactNumber = contactNumber; 
} 

UserServiceImpl.java

public List fetchService() throws Exception { 
    UserDao userImpl ; 
    List userList; 

    try { 
     userImpl = new UserImpl(); 
     userList = new ArrayList(); 
     userList = userImpl.getAllUser(); 
    } catch (Exception e) { 
     throw new Exception("\nexception in user fetch service\n"+e); 
     } 

    return userList; 
} 

UserImpl.java

public List getAllUser() throws ClassNotFoundException,Exception{ 
    Session session = DataBaseConnection.getSessionFactory().openSession(); 
    Transaction transaction = null; 
    List users = null; 

    try { 
     transaction = session.beginTransaction(); 
     users = session.createQuery("from user").list(); 
     transaction.commit(); 
    } catch (HibernateException e) { 
     transaction.rollback(); 
     throw new Exception("Exception in UserImpl " + e); 
    } finally { 
     session.close(); 
    } 

    return users; 

} 

User.hbm.xml

<hibernate-mapping> 
<class name="com.ecommerce.hibernate.model.User" table="user"> 
<meta attribute="class-description"> 
    This class contains the user details. 
</meta> 
<id name="id" type="int" column="id"> 
    <generator class="increment"/> 
</id> 
<property name="name"> 
    <column name="name" /> 
</property> 
<property name="userName"> 
    <column name="username"/> 
</property> 
<property name="password"> 
    <column name="password"/> 
</property> 
<property name="phone"> 
    <column name="phone"/> 
</property> 
</class> 
</hibernate-mapping> 

struts.xml中

<action name="GetAllUserAction" class="com.ecommerce.action.GetAllUserAction"> 
<result name="SUCCESS">/GetUser.jsp</result> 
</action> 

的hibernate.cfg.xml

<hibernate-configuration> 
<session-factory> 
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
<property  name="hibernate.connection.url">jdbc:mysql://localhost:3306/ecommerce</property> 
<property name="hibernate.connection.username">root</property> 
<property name="connection.password"></property> 
<property name="connection.pool_size">10</property> 
<property name="hibernate.dialect">org.hibernate.dialect.MySqlDialect</property> 
<property name="show_sql">true</property> 
<mapping resource="com/ecommerce/model/User.hbm.xml"/> 
</session-factory> 
</hibernate-configuration> 

DatabaseConnection.java

public static SessionFactory getSessionFactory() throws HibernateException { 

    try{ 
     Configuration configuration = new Configuration(); 
     configuration.configure(); 
     serviceRegistry = new serviceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();  
     sessionFactory = configuration.buildSessionFactory(serviceRegistry); 
     return sessionFactory; 
    } catch(HibernateException e) { 
     throw new HibernateException(" \nSession factory"+e); 
    } 

} 

GetUser.jsp

<s:iterator value="users"> 
<tr> 
      <td><s:property value="id"/></td> 
      <td><s:property value="name"/></td> 
      <td><s:property value="userName"/></td> 
      <td><s:property value="password"/></td> 
      <td><s:property value="contactNumber"/></td> 
      <td><s:property value="address"/></td> 
      <td><s:property value="city"/></td> 
      <td><s:property value="email"/></td> 
</tr> 
</s:iterator> 

表名:用戶

+0

在classpath把hibernate.cfg.xml? –

+0

是的,它正好在/ src或WEB-INF/classes文件夾中的類路徑 – Pravin

+0

中? –

回答

0

嘗試使用

private static SessionFactory getSessionFactory() { 
     try { 
      SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); 
      return sessionFactory; 
     } catch (Throwable ex) { 
      System.err.println("Initial SessionFactory creation failed." + ex); 
      throw new ExceptionInInitializerError(ex); 
     } 
    } 
0

我對你的代碼幾個疑惑..

1)users = session.createQuery("from **user**").list(); 可以更改用戶到用戶並嘗試?

2)UserServiceDao userServiceDao = new UserServiceImpl();

我不認爲這是做的正確的方式..這是命名規則的問題?

嘗試

private static SessionFactory getSessionFactory() { 
     try { 
      SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); 
      return sessionFactory; 
     } catch (Throwable ex) { 
      System.err.println("Initial SessionFactory creation failed." + ex); 
      throw new ExceptionInInitializerError(ex); 
     } 
    } 
+0

關於你的疑惑:1)我改變爲用戶,這是我自己犯的一個錯誤.2)這也是錯誤的命名約定我自己做的...非常感謝..但仍然存在相同的問題 – Pravin

+0

這可能是一個愚蠢的問題但在這裏呢..你如何確保你沒有從查詢中獲取任何數據? – BDR

+0

我也注意到你沒有提到User.hbm.xml.I中屬性的數據類型,我覺得這是強制性屬性(如果我沒有錯的話)。 – BDR