【问题标题】:issue in fetching data from hibernate using struts2使用struts2从hibernate获取数据的问题
【发布时间】:2013-11-15 05:57:52
【问题描述】:

我正在尝试从表中获取数据,除了日志文件中的一些错误之外我没有得到任何东西......请有人帮助我

catalina.2013-11-15.log

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

2013 年 11 月 15 日 11:13:32.557 信息 [http-apr-8080-exec-2] null.null HHH000412:休眠核心 {4.2.7.Final}

2013 年 11 月 15 日 11:13:32.568 信息 [http-apr-8080-exec-2] null.null HHH000206:找不到 hibernate.properties

2013 年 11 月 15 日 11:13:32.577 信息 [http-apr-8080-exec-2] null.null HHH000021:字节码提供程序名称:javassist

2013 年 11 月 15 日 11:13:32.693 信息 [http-apr-8080-exec-2] null.null HHH000043:从资源配置:/hibernate.cfg.xml

2013 年 11 月 15 日 11:13:32.696 信息 [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;

}

用户.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>

数据库连接.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>

表名:用户

【问题讨论】:

  • 你的 hibernate.cfg.xml 在类路径中吗?
  • 是的,它在类路径中
  • 是否在 /src 或 WEB-INF/classes 文件夹中?
  • 在 C:\apache-tomcat-8.0.0-RC5\webapps\HibernateExample1\WEB-INF\classes\hibernate.cfg.xml
  • 默认池大小为 20,并且没有限制,因此在您的情况下似乎没用。

标签: java sql hibernate struts2


【解决方案1】:

尝试使用

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);
        }
    }

【讨论】:

    【解决方案2】:

    我对你的代码没有什么疑问..

    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);
            }
        }
    

    【讨论】:

    • 关于您的疑问:1)我更改为用户,这是我自己犯的错误。2)我自己也犯了错误的命名约定......非常感谢......但仍然是同样的问题坚持
    • 这可能是个愚蠢的问题,但它来了......你如何确保你没有从查询中获得任何数据?
    • 我还注意到您没有提到 User.hbm.xml 中属性的数据类型。我觉得这是强制性属性(如果我没记错的话)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    • 1970-01-01
    • 2019-02-15
    • 1970-01-01
    • 2011-06-19
    • 2011-11-16
    相关资源
    最近更新 更多