<?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>
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.password">****</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/mkastrive</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver </property>
<property name="hibernate.default_schema">mkastrive</property>
<property name="hibernate.connection.username">****</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">false</property>
<mapping resource="n/a/mkastrive/UserPojoMap.hbm.xml" />
</session-factory>
</hibernate-configuration>
UserPojoMap.hbm.xml - 位于n.a.mkastrive包内。
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="n.a.mkastrive.UserPojo" table="user" lazy="false">
<id name="id" type="integer">
<column name="id" />
</id>
<property name="firstName" type="string">
<column name="firstName" length="50" not-null="true" />
</property>
<property name="lastName" type="string">
<column name="lastName" not-null="true" />
</property>
<property name="email" type="string">
<column name="email" length="50" not-null="true" />
</property>
</class>
</hibernate-mapping>
Struts2的动作从execute()
中调用此函数private List<UserPojo> userPojo = null;
private void listUser() throws HibernateException {
SessionFactory sessionFactory = (SessionFactory) ServletActionContext.getServletContext().getAttribute(HibernateListener.KEY_NAME);
Session session = sessionFactory.openSession();
Query query = session.createQuery("from " + UserPojo.class.getSimpleName() + " where id = :id ");
query.setParameter("id", "1");
List list = query.list();
for(int i = 0; i < list.size(); i++ ) {
System.out.println(list.get(i));
}
userPojo = session.createCriteria(UserPojo.class).list();
// System.out.println("userPojo.size: " + userPojo.size());
// for (int i = 0; i < userPojo.size(); i++) {
// System.out.println(userPojo.get(i).getEmail());
// }
}
我没有得到任何例外;它的列表是空的,0个元素。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>n.a.mkastrive</groupId>
<artifactId>MkaStrive</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>MkaStrive Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.10.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.8.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-json-plugin</artifactId>
<version>2.5.10.1</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpasyncclient</artifactId>
<version>4.1.3</version>
</dependency>
<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-admin</artifactId>
<version>5.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.10.Final</version>
</dependency>
<!-- Hibernate library dependecy start -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
</dependencies>
<build>
<finalName>MkaStrive</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat8-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/CounterWebApp</path>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
实际上,这确实给了我一个例外:
Query query = session.createQuery("from " + UserPojo.class.getSimpleName() + " where id = :id ");
java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: UserPojo is not mapped [from UserPojo where id = :id ]
更新1:HibernateListener
package n.a.mkastrive.listner;
import java.net.URL;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class HibernateListener implements ServletContextListener {
private Configuration config;
private SessionFactory factory;
private String path = "/hibernate.cfg.xml";
private static Class clazz = HibernateListener.class;
public static final String KEY_NAME = clazz.getName();
@Override
public void contextDestroyed(ServletContextEvent sce) {
SessionFactory sf = (SessionFactory) sce.getServletContext().getAttribute("SessionFactory");
sf.close();
}
@Override
public void contextInitialized(ServletContextEvent event) {
Configuration config = new Configuration();
/**
* Connection Information..
*/
config.setProperty("hibernate.connection.driver_class", "com.mysql.cj.jdbc.Driver");
config.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/mkastrive");
config.setProperty("hibernate.connection.username", "****");
config.setProperty("hibernate.connection.password", "****");
config.setProperty("hibernate.show_sql", "true");
/**
* Mapping Resources..
*/
config.addResource("n/a/mkastrive/User.hbm.xml");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();
SessionFactory sf = config.buildSessionFactory(serviceRegistry);
event.getServletContext().setAttribute(KEY_NAME, sf);
}
}
答案 0 :(得分:1)
您可以通过编程方式构建配置。至少这对我有用。
// build the config
cfg = new Configuration();
/**
* Connection Information..
*/
cfg.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
cfg.setProperty("hibernate.connection.url", ">jdbc:mysql://127.0.0.1:3306/mkastrive");
cfg.setProperty("hibernate.connection.username", "mkastrive");
cfg.setProperty("hibernate.connection.password", "****");
cfg.setProperty("hibernate.show_sql", "true");
/**
* Mapping Resources..
*/
cfg.addResource("n/a/mkastrive/UserPojoMap.hbm.xml");
sessionFactory = cfg.buildSessionFactory();