Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用struts2从hibernate获取数据时出现问题_Java_Sql_Hibernate_Struts2 - Fatal编程技术网

Java 使用struts2从hibernate获取数据时出现问题

Java 使用struts2从hibernate获取数据时出现问题,java,sql,hibernate,struts2,Java,Sql,Hibernate,Struts2,我正在尝试从表中获取数据,但除了日志文件中的一些错误外,我什么都没有得到…请有人帮助我 catalina.2013-11-15.log 2013年11月15日11:13:32.521信息[http-apr-8080-exec-2]HCANN000001:Hibernate Commons注释{4.0.2.Final} 2013年11月15日11:13:32.557信息[http-apr-8080-exec-2]null.null HH000412:Hibernate核心{4.2.7.Final}

我正在尝试从表中获取数据,但除了日志文件中的一些错误外,我什么都没有得到…请有人帮助我

catalina.2013-11-15.log

2013年11月15日11:13:32.521信息[http-apr-8080-exec-2]HCANN000001:Hibernate Commons注释{4.0.2.Final}

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

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

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

2013年11月15日11:13:32.693信息[http-apr-8080-exec-2]null.null HH000043:从资源配置:/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>
2013年11月15日11:13:32.696信息[http-apr-8080-exec-2]null.null HH000040:配置资源:/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>
GetAllusAction.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;
}
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 ); 
    }   

}
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>
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>
表名称:用户

尝试使用

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 users=session.createQueryfrom**user**.list; 您可以将用户更改为用户并重试吗

2 UserServiceDao UserServiceDao=新的userserviceinpl

我认为这不是正确的做法。。这是命名约定的问题吗

试一试


您的hibernate.cfg.xml是否在类路径中?是的,它在类路径中,恰好在/src或WEB-INF/classes文件夹中?在C:\apache-tomcat-8.0.0-RC5\webapps\HibernateExample1\WEB-INF\classes\hibernate.cfg.xml中默认池大小为20,没有限制,因此,在您的情况下,这似乎没有用。关于您的疑问:1我改为用户是我自己犯的错误。2这也是我自己犯的错误命名约定…非常感谢。。但同样的问题仍然存在,这可能是个愚蠢的问题,但问题来了。。如何确保您没有从查询中获取任何数据?我还注意到您没有提到User.hbm.xml中属性的数据类型。如果我没有错的话,我觉得这是强制属性。
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);
        }
    }
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);
        }
    }