'java.lang.NoClassDefFoundError: org/hibernate/Session' ...
'A JNI error has occurred, please check your installation and try again
Exception in thread "main"'
[Error in both Git Bash and Eclipse-jee-neon-R-win32-x86_64]
$ java -version
java version "1.8.0_102"
Java(TM) SE Runtime Environment (build 1.8.0_102-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)
$ mvn -version
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T22:11:47+05:30)
Maven home: C:\Program Files\Apache Software Foundation\apache-maven-3.3.9
Java version: 1.8.0_102, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.8.0_102\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 8.1", version: "6.3", arch: "amd64", family: "dos"
DROP DATABASE IF EXISTS hibernatedb;
CREATE DATABASE hibernatedb;
USE hibernatedb;
DROP TABLE IF EXISTS `stock`;
CREATE TABLE `stock` (
`STOCK_ID` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`STOCK_CODE` VARCHAR(10) NOT NULL,
`STOCK_NAME` VARCHAR(20) NOT NULL,
PRIMARY KEY (`STOCK_ID`) USING BTREE,
UNIQUE KEY `UNI_STOCK_NAME` (`STOCK_NAME`),
UNIQUE KEY `UNI_STOCK_ID` (`STOCK_CODE`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
<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>com.sample.common</groupId>
<artifactId>HibernateMavenMySQLXMLSample</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>HibernateMavenMySQLXMLSample</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- JUnit 4.12 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- MySQL database driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>
<!-- Hibernate framework -->
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>3.2.3.ga</version>
</dependency>
<!-- Hibernate library dependecy start -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
<!-- Hibernate library dependecy end -->
</dependencies>
</project>
<?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="com.sample.common.Stock" table="stock" catalog="sample">
<id name="stockId" type="java.lang.Integer">
<column name="STOCK_ID" />
<generator class="identity" />
</id>
<property name="stockCode" type="string">
<column name="STOCK_CODE" length="10" not-null="true" unique="true" />
</property>
<property name="stockName" type="string">
<column name="STOCK_NAME" length="20" not-null="true" unique="true" />
</property>
</class>
</hibernate-mapping>
package com.sample.common;
/**
* Model class for Stock
*/
public class Stock implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private Integer stockId;
private String stockCode;
private String stockName;
public Stock() {
}
public Stock(String stockCode, String stockName) {
this.stockCode = stockCode;
this.stockName = stockName;
}
public Integer getStockId() {
return this.stockId;
}
public void setStockId(Integer stockId) {
this.stockId = stockId;
}
public String getStockCode() {
return this.stockCode;
}
public void setStockCode(String stockCode) {
this.stockCode = stockCode;
}
public String getStockName() {
return this.stockName;
}
public void setStockName(String stockName) {
this.stockName = stockName;
}
}
<?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.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">***********</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatedb</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<mapping resource="com/sample/common/Stock.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
package com.sample.persistence;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new Configuration().configure().buildSessionFactory();
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void shutdown() {
// Close caches and connection pools
getSessionFactory().close();
}
}
# src/main/java/com/sample/common/App.java
package com.sample.common;
import org.hibernate.Session;
import com.sample.persistence.HibernateUtil;
public class App
{
public static void main( String[] args )
{
System.out.println("Maven + Hibernate + MySQL");
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Stock stock = new Stock();
stock.setStockCode("4715");
stock.setStockName("GENM");
session.save(stock);
session.getTransaction().commit();
}
}
$java-cp target/hibernateMavenmysqlxmlsample-1.0-snapshot.jar com.sample.common.app
java.lang.NoClassDefFoundError: org/hibernate/Session
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.privateGetMethodRecursive(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.hibernate.Session
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 7 more
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main"
$ mvn eclipse:eclipse
[INFO] Scanning for projects...
[INFO] Not writing settings - defaults suffice
[INFO] File E:\Working\Java\Hibernate\HibernateMavenMySQLXML\HibernateMavenMySQLXMLSample\.project already exists.
Additional settings will be preserved, run mvn eclipse:clean if you want old settings to be removed.
[INFO] Wrote Eclipse project for "HibernateMavenMySQLXMLSample" to E:\Working\Java\Hibernate\HibernateMavenMySQLXML\HibernateMavenMySQLXMLSample.
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 05:30 min
[INFO] Finished at: 2016-11-04T01:07:24+05:30
[INFO] Final Memory: 13M/247M
[INFO] ------------------------------------------------------------------------
$ mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building HibernateMavenMySQLXMLSample 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ HibernateMavenMySQLXMLSample ---
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.sample.common.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.086 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ HibernateMavenMySQLXMLSample ---
Downloading: https://repo.maven.apache.org/maven2/commons-lang/commons-lang/2.1/commons-lang-2.1.pom
Downloaded: https://repo.maven.apache.org/maven2/commons-lang/commons-lang/2.1/commons-lang-2.1.pom (10 KB at 8.9 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/classworlds/classworlds/1.1-alpha-2/classworlds-1.1-alpha-2.jar
Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-interpolation/1.15/plexus-interpolation-1.15.jar
Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-archiver/2.1/plexus-archiver-2.1.jar
Downloading: https://repo.maven.apache.org/maven2/commons-lang/commons-lang/2.1/commons-lang-2.1.jar
Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-io/2.0.2/plexus-io-2.0.2.jar
Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-interpolation/1.15/plexus-interpolation-1.15.jar (60 KB at 45.6 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0/plexus-utils-3.0.jar
Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-io/2.0.2/plexus-io-2.0.2.jar (57 KB at 43.6 KB/sec)
Downloaded: https://repo.maven.apache.org/maven2/classworlds/classworlds/1.1-alpha-2/classworlds-1.1-alpha-2.jar (37 KB at 26.6 KB/sec)
Downloaded: https://repo.maven.apache.org/maven2/commons-lang/commons-lang/2.1/commons-lang-2.1.jar (203 KB at 91.7 KB/sec)
Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0/plexus-utils-3.0.jar (221 KB at 87.6 KB/sec)
Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-archiver/2.1/plexus-archiver-2.1.jar (181 KB at 51.5 KB/sec)
[INFO] Building jar: E:\Working\Java\Hibernate\HibernateMavenMySQLXML\HibernateMavenMySQLXMLSample\target\HibernateMavenMySQLXMLSample-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:52 min
[INFO] Finished at: 2016-11-04T01:09:28+05:30
[INFO] Final Memory: 19M/268M
[INFO] ------------------------------------------------------------------------
谢了。
这里需要的是生成一个可执行JAR。这个jar应该主要包含三个内容,即
1。您的类。
2。这些类存在于您作为依赖项添加的JAR中。
3。指定主类的清单文件。
使用当前的pom.xml,工件将只包含类。要添加上面提到的另外两个东西,可以使用Maven Assembly插件。
在pom.xml中的
标记中添加下面的代码。
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.sample.common.App</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
这将在target
文件夹中生成两个jar。其中一个只有您的类,另一个jar将有我上面提到的所有东西。第二个jar将在其名称后面附加jar-with-dependencies
。
您应该使用这个jar来运行您的应用程序。
问题内容: 在我的applicationContext.xml中,这就是将xml映射到POJO的方式。如何将目录映射到类文件而无需创建xml? 问题答案: 您可以通过转换进一步简化操作 至 现在您的包中所有带有注释的类都将自动被拾取。
问题内容: 我有一个带有集合的类,它们是Hibernate管理的POJO。我也在使用 hbm.xml 文件映射我的实体。当用户转到管理屏幕时,我希望他将数据查看到表中,该表还将包含最近完成的工作。但是,只有访问可检测内容的详细信息页面,才能使用完整的修订集。 我的机会是显示最后的修订日期,该日期将作为每个实例的属性单独加载。所以我有这样的事情: detectable.hbm.xml 这是行不通的,
问题内容: 我有一个带有集合的类,它们是Hibernate管理的POJO。我还使用 hbm.xml 文件映射了我的实体。当用户转到管理屏幕时,我希望他将数据查看到表中,该表还将包含最近完成的工作。但是,只有访问可检测内容的详细信息页面,才能使用完整的修订集。 我的机会是显示最后的修订日期,该日期将作为每个实例的属性单独加载。所以我有这样的事情: detectable.hbm.xml 这是行不通的,
我试图在Hibernate中的两个表之间创建一对一的单向主键关系。我使用xml Maaping,下面是我的java POJO类和它们各自的。hbm文件。 储物柜POJO类 locker.hbm.xml 下面是我用来保存student和locker对象的主类。
问题内容: 有人可以给我一个Hibernate中单向@OneToOne主键映射的示例吗?我已经尝试了多种组合,到目前为止,我得到的最好的东西是: 每当Hibernate尝试自动为上述映射生成模式时,它都会尝试将主键创建为blob(而不是long)(即PaperCheque的id类型)。有人能帮帮我吗 ?如果我无法找到确切的解决方案,那么可以采取一些措施,但我希望得到任何答复。 问题答案: 您的意图
问题内容: 我尝试创建本机sql查询并使用参数映射到实体类,但是失败。 请提供一个例子。谢谢。 问题答案: 您可以尝试以下代码。 另外,您可以尝试为各个字段指定别名。