当前位置: 首页 > 知识库问答 >
问题:

使用struts2从hibernate获取数据时的问题

钱志强
2023-03-14

我正试图从表中获取数据,但我没有得到任何东西,只是日志文件中的一些错误...请有人帮助我

Catalina.2013-11-15.log

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

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;

}
<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>
<action name="GetAllUserAction" class="com.ecommerce.action.GetAllUserAction">
<result name="SUCCESS">/GetUser.jsp</result>
</action>
<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>
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 ); 
    }   

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

共有1个答案

殳阳飙
2023-03-14

尝试使用

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);
        }
    }
 类似资料:
  • 我正在尝试使用Hibernate以便从MySQL数据库中获取数据。为了实现这样一个目标,我创造了: > Hibernate配置文件:

  • 我们有自定义密钥类型的ignite缓存,即带有属性的Person key、人名和人姓以及自定义值类型的Person Info、带有属性的Person Address和Person Age等。这些类是在Java中定义的,缓存是在Bean文件中配置的,并使用CacheJDBCPOJO Store加载的。 由于这些类在节点js中不可用,我们如何使用cahe.put/cache.get.从节点js加载/提

  • 我有一个从甲骨文获取数据并上传到谷歌云存储的需求。 我正在使用executeSql proecssor,但是对于大表来说它是失败的,甚至对于有大约45MB大小的100万条记录的表来说,它需要花费2小时来提取。 使用restapi将表名传递给listenHttp,Listenhtp将表名传递给ExecutesQL。我不能使用QueryDatabase,因为表的数量是动态的,启动获取的调用也是动态的,

  • 我正在创建一个JavaFX应用程序,我已经很好地连接到了数据库。然而,当我从表中获取数据时,我得到了一个错误 组织。h2.jdbc。JdbcSQLException:未找到表“touch”;SQL语句:从讲座[42102-192]中选择名称 我100%确定我连接到数据库并且表肯定在那里,对为什么会这样有任何建议吗? hear是我的连接代码和我正在运行的代码,以便您可以看到 和正在运行的查询

  • 问题内容: 我有一个实体,该实体具有必须从序列中设置的NON-ID字段。目前,我获取序列的第一个值,将其存储在客户端,然后根据该值进行计算。 但是,我正在寻找一种“更好”的方法。我实现了一种获取下一个序列值的方法: 但是,这种方式会大大降低性能(〜5000个对象的创建速度降低了3倍-从5740ms到13648ms)。 我试图添加一个“假”实体: 但是,该方法也不起作用(返回的所有ID为0)。 有人

  • 我正在测试一个基于酒店搜索的场景。所以我做了3个模型类,分别是酒店、房间和可用性 下面是我们正在使用的三个模型类。 Hotel.java @文档(集合="酒店")公共类酒店{ } Room.java @文件(收集=“房间”)公共教室{ } 可利用性Java语言 @文件(收集=“房间”)公共类可用性{ } 这些存储在两个mongodb集合中,一个用于酒店,另一个用于房间和可用性。 下面是我们从中提取